I am working on a website with 2 other developers. I am only responsible to creating the views.
The data is available in an object, and I have getters to read the data then create XHTML pages.
What is the best practice to do this, without using any template engine?
Thanks a lot.
If you don’t want to use a templating engine, you can make use of PHP’s basic templating capabilities.
Actually, you should just write the HTML, and whenever you need to output a variable’s value, open a PHP part with
<?phpand close it with?>. I will assume for the examples that$datais your data object.For example:
Please note that, all PHP control structures (like
if,foreach,while, etc.) also have a syntax that can be used for templating. You can look these up in their PHP manual pages.For example:
If you know that short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with
<?and close it with?>. Also,<?=$var?>is a shorthand for echoing something.First example with short tags:
You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:
Writing this code:
is not equivalent to this one:
Because in the second one you have an actual
\nbetween theimgelements, which will be translated by the browser as a space character and displayed as an actual space between the images if they are inline.