Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Php ... same !@#$, different pile. –  Job Jan 8 '11 at 5:27
    
Have you considered the case where there are quotes in the code that would have to be escaped and other challenges with encoding code within code? This reminds me of classic ASP challenges with HTML in a Response.Write that could get ugly fast. –  JB King Apr 13 '12 at 21:30

7 Answers 7

up vote 6 down vote accepted

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.

share|improve this answer
    
The alternative syntax makes it also easier to find where code blocks end. Having to find a pair for <?php } ?> is much more difficult, than a pair for <?php endif; ?> or <?php endwhile;?> (Given that your templates actually do NOT need too many levels of nested code blocks). –  Mchl Jan 7 '11 at 22:41
1  
+1 for "if you include large blocks of html" - that's really the only time the advice would be good. –  Tom Anderson Jan 7 '11 at 22:48

That's very weird advice. What the heck are "non-PHP programmers" doing reading your code?

share|improve this answer
    
Sorry for playing devil's advocate here, but I now understand the purpose of Listing 4-5. If you have designers working on your site you will save a lot of time if you show them the views that they have to edit and have them do it themselves, rather than getting their requirements and coding it yourself. All designers know (or should know) HTML, so making your PHP in your views mimic HTML is a way of making it readable for them. –  AndrewKS Jan 9 '11 at 23:02
    
@AndrewKS: ah, ya tricked me! Fair enough, i see the benefit of making things more readable for HTML-only authors. I don't know anything about Symfony - are these templates things that will be exposed to HTML editors? –  Tom Anderson Jan 10 '11 at 17:04
    
From my limited experience with designers: they all seem to use PHP editors, but their knowledge in PHP is limited to frameworks like Wordpress and Drupal and they don't understand the backend code like databases and programmer logic. –  AndrewKS Jan 10 '11 at 17:54

This is completely subjective. 'More understandable' depends on the person doing the reading. Use whatever form you think is best.

share|improve this answer
    
Which is why I posted this question on programmers and not stackoverflow. –  AndrewKS Jan 7 '11 at 20:43
    
... and its why I answered it here. –  GrandmasterB Jan 7 '11 at 21:21
    
Oh, sorry! I misunderstood your answer as a criticism of the question rather than a response. And I see what you mean now. –  AndrewKS Jan 7 '11 at 21:56
1  
correct - I was saying the document writers were making a claim that is purely subjective, and that you shouldnt feel compelled to use their style if you dont agree with them. –  GrandmasterB Jan 7 '11 at 22:22

NO, you are not wrong to disagree. Writers of books are not gods! At the time, the author might have thought that that was a better convention, but obviously that just his opinion.

share|improve this answer
1  
Not just his :P –  Mchl Jan 7 '11 at 22:37

I use the second one simply because text editors and IDEs understand the html. If I miss a closing tag, the editor will inform me. If I just print the html as a string it will not.

I don't really have an opinion on it other than that.

share|improve this answer

I find the syntax in Figure 4-5 to be far superior to the first. When you consider larger, more complicated examples, the first syntax will get pretty messy. The syntax of Figure 4-5 allows designers familiar with html and not with php can to easily work with your code.

A designer does not need to know how you get your data from your database and how you modify it for display, they just want to see your html tags. This goes beyond any personal preference about which syntax is easier to understand. If following the MVC architecture, your application should display its content independently from where the data comes from and how it is processed.

Listings 2-1 through 2-5 in chapter two of The Definitive Guide to symfony show this very well.

Listing 2-3 is a lot easier for a designer to work with than Listing 2-1. It does not contain any code used to retrieve that data that is displayed.

Listing 2-3:

<html>
  <head>
    <title>List of Posts</title>
  </head>
  <body>
    <h1>List of Posts</h1>
      <table>
        <tr><th>Date</th><th>Title</th></tr>
         <?php foreach ($posts as $post): ?>
           <tr>
            <td><?php echo $post['date'] ?></td>
            <td><?php echo $post['title'] ?></td>
           </tr>
         <?php endforeach; ?>
     </table>
   </body>
 </html>

Listing 2-1:

<?php

// Connecting, selecting database
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);

// Performing SQL query
$result = mysql_query('SELECT date, title FROM post', $link);

?>

<html>
  <head>
    <title>List of Posts</title>
  </head>
 <body>
   <h1>List of Posts</h1>
     <table>
       <tr><th>Date</th><th>Title</th></tr>
        <?php
          // Printing results in HTML
          while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
          {
            echo "\t<tr>\n";
            printf("\t\t<td> %s </td>\n", $row['date']);
            printf("\t\t<td> %s </td>\n", $row['title']);
            echo "\t</tr>\n";
          }
        ?>
      </table>
    </body>
  </html>

// Closing connection
mysql_close($link);

?>

share|improve this answer

There is a third general syntax, which I have always found the most readable:

<p>Hello, world!</p> 
<?php if ($test) { ?>
    <p><?= time(); ?></p>
<?php } ?>
  • Don't mix echo and printf with turning php on/off by way of ?>foo<?php - it's much harder to mentally separate output from code.
  • Contrary to popular advice, keep the { curly braces }. I find them much easier to see when looking for the start and end of code blocks.
  • Use short tags if available: <?= is identical to <?php echo. It reduces the general amount of clutter, and starting in PHP 5.4, cannot be disabled.
    • Although echo isn't that difficult a concept, it also reduces the amount of PHP a template designer might have to know.
  • One issue I have with the HTML format: I moved the closing </p> to the line above it. Now the end of the if statement has the same indentation as the beginning.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.