Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am currently learning symfony and going through the book A Gentle Introduction

ID: 648849 • Letter: I

Question

I am currently learning symfony and going through the book A Gentle Introduction to symfony and came across this section in "Chapter 4: The Basics of Page Creation" on creating templates (or views):

"If you need to execute some PHP code in the template, you should avoid using the usual PHP syntax, as shown in Listing 4-4. Instead, write your templates using the PHP alternative syntax, as shown in Listing 4-5, to keep the code understandable for non-PHP programmers."

Listing 4-4 - The Usual PHP Syntax, Good for Actions, But Bad for Templates

<p>Hello, world!</p>
<?php
if ($test) {
   echo "<p>".time()."</p>";
}
?>
(The ironic thing about this is that the echo statement would look even better if time was a variable declared in the controller because then you could just embed the variable in the string instead of concatenating)

Listing 4-5 - The Alternative PHP Syntax, Good for Templates

<p>Hello, world!</p>
<?php if ($test): ?>
    <p><?php echo time(); ?>
</p><?php endif; ?>
I fail to see how listing 4-5 makes the code "understandable for non-PHP programmers", and its readability is shaky at best. 4-4 looks much more readable to me. Are there any programmers who are using symfony that write their templates like those in 4-4 rather than 4-5? Are there reasons I should use one over the other? There is the very slim chance that somewhere down the road someone less technical could be editing it the template, but how does 4-5 actually make it more understandable to them?

Explanation / Answer

The second example uses plain html and I think it is more understandable if you include large blocks of html (for example a login form if the user is not logged in) and only very few values from php-functions.

In this very smallscale example it doesn't show, I think, and I would prefer pure html for smaller things like individual cells with hardly any html.