Antti A
Antti A

Reputation: 500

What is the best practice to create cakePHP template files?

Im using CakePHP framework version 3.6 and I'm wonder what is the best practice to create template/element (.ctp) files? Should I use plain HTML or should I use HtmlHelper (https://book.cakephp.org/3.0/en/views/helpers/html.html)?

Personnally I think HtmlHelper is useful when creating "global" items like:

echo $this->Html->charset('ISO-8859-1'); 

to create output like

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

and less when creating html tags like:

echo $this->Html->link(
    $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]),
    "recipes/view/6",
    ['escape' => false]
);

to crate output like

<a href="/recipes/view/6">
    <img src="/img/recipes/6.jpg" alt="Brownies" />
</a>

Upvotes: 0

Views: 67

Answers (1)

floriank
floriank

Reputation: 25698

The helpers. The reason for that is also explained in the book:

The role of the HtmlHelper in CakePHP is to make HTML-related options easier, faster, and more resilient to change. Using this helper will enable your application to be more light on its feet, and more flexible on where it is placed in relation to the root of a domain.

https://book.cakephp.org/3.0/en/views/helpers/html.html

Without the helpers routing won't work properly, especially when a route is changed. Same goes for images, CSS and JS when the domain or path of the site changes from /to /some/site.

Upvotes: 1

Related Questions