Displaying contacts in PHP, with options to omit some information I am currently changing some PHP code. The old code looked like: 



&lt;div class="contact&lt;?php echo $this-&gt;pageclass_sfx; ?&gt;" itemscope itemtype="https://schema.org/Person"&gt;
 &lt;?php if ($tparams-&gt;get('show_page_heading')) : ?&gt;
  &lt;h1&gt;
   &lt;?php echo $this-&gt;escape($tparams-&gt;get('page_heading')); ?&gt;
  &lt;/h1&gt;
 &lt;?php endif; ?&gt;

 &lt;?php if ($this-&gt;contact-&gt;name &amp;&amp; $tparams-&gt;get('show_name')) : ?&gt;
  &lt;div class="page-header"&gt;
   &lt;h2&gt;
    &lt;?php if ($this-&gt;item-&gt;published == 0) : ?&gt;
     &lt;span class="label label-warning"&gt;&lt;?php echo JText::_('JUNPUBLISHED'); ?&gt;&lt;/span&gt;
    &lt;?php endif; ?&gt;
    &lt;span class="contact-name" itemprop="name"&gt;&lt;?php echo $this-&gt;contact-&gt;name; ?&gt;&lt;/span&gt;
   &lt;/h2&gt;
  &lt;/div&gt;
 &lt;?php endif; ?&gt;

 &lt;?php $show_contact_category = $tparams-&gt;get('show_contact_category'); ?&gt;

 &lt;?php if ($show_contact_category === 'show_no_link') : ?&gt;
  &lt;h3&gt;
   &lt;span class="contact-category"&gt;&lt;?php echo $this-&gt;contact-&gt;category_title; ?&gt;&lt;/span&gt;
  &lt;/h3&gt;
 &lt;?php elseif ($show_contact_category === 'show_with_link') : ?&gt;
  &lt;?php $contactLink = ContactHelperRoute::getCategoryRoute($this-&gt;contact-&gt;catid); ?&gt;
  &lt;h3&gt;
   &lt;span class="contact-category"&gt;&lt;a href="&lt;?php echo $contactLink; ?&gt;"&gt;
    &lt;?php echo $this-&gt;escape($this-&gt;contact-&gt;category_title); ?&gt;&lt;/a&gt;
   &lt;/span&gt;
  &lt;/h3&gt;
 &lt;?php endif; ?&gt;




I found this quite hard to follow and decided to try and rewrite the same piece of code. It ended up looking like this:



&lt;?php
echo '&lt;div class="contact'. $this-&gt;pageclass_sfx . '" itemscope itemtype="https://schema.org/Person"&gt;';

// show/hide heading  
if ($tparams-&gt;get('show_page_heading')){
 echo '&lt;h1&gt;'. $this-&gt;escape($tparams-&gt;get('page_heading')) . '&lt;/h1&gt;';
}
 
// show/hide contact name 
if ($this-&gt;contact-&gt;name &amp;&amp; $tparams-&gt;get('show_name')){
 echo '&lt;div class="page-header"&gt;';
 echo '&lt;h2&gt;';
 if ($this-&gt;item-&gt;published == 0){
  echo '&lt;span class="label label-warning"&gt;' . JText::_('JUNPUBLISHED') . '&lt;/span&gt;';
 }
 echo '&lt;span class="contact-name" itemprop="name"&gt;'. $this-&gt;contact-&gt;name . '&lt;/span&gt;';
 echo '&lt;/h2&gt;';
 echo '&lt;/div&gt;';
} ?&gt;




I think the logic is much easier to follow when written like this. 


I was wondering if anyone had some opinions / remarks / feedback on improvements for the second version of the code. 
If anyone sees some advantages of the first version over the second I would love to hear what they are.
The answers you'll get will be highly opinion-based, I guess. So, here a few points in favor of the first type of templating as the approach of inserting short PHP-elements into the template has some advantages.



Easier to read and follow

For a developer, the latter approach might be easier to follow. But often it's important to understand the structure and hierarchy of the markup. Your approach makes it quite hard:


echo '&lt;h1&gt;'. $this-&gt;escape($tparams-&gt;get('page_heading')) . '&lt;/h1&gt;';
echo '&lt;div class="page-header"&gt;';
echo '&lt;h2&gt;';
if ($this-&gt;item-&gt;published == 0){
    echo '&lt;span class="label label-warning"&gt;' . JText::_('JUNPUBLISHED') . '&lt;/span&gt;';
}
echo '&lt;span class="contact-name" itemprop="name"&gt;'. $this-&gt;contact-&gt;name . '&lt;/span&gt;';
echo '&lt;/h2&gt;';
echo '&lt;/div&gt;';



It looks like everything is on one level and only the span-element is nested. This might be ok for a short passage like we see here. But consider a complex component with lots of nested elements.

Somebody who understands HTML can maintain the template

The first point brings us straight to this: A fronted-developer, who is not familiar with PHP can maintain and update your template. Taking the example from above, the person knows easily where to adjust the code, without breaking anything, if it would be written like this:

&lt;h1&gt;&lt;?=$this-&gt;escape($tparams-&gt;get('page_heading'))?&gt;&lt;/h1&gt;
&lt;div class="page-header"&gt;
  &lt;h2&gt;
    &lt;span class="label label-warning"&gt;&lt;?=($this-&gt;item-&gt;published ? JText::_('JUNPUBLISHED') : '')?&gt;&lt;/span&gt;
    &lt;span class="contact-name" itemprop="name"&gt;&lt;?=$this-&gt;contact-&gt;name?&gt;&lt;/span&gt;
  &lt;/h2&gt;
&lt;/div&gt;


Also removing parent wrappers or inserting new elements is much easier.

Too much logic in the templates

I would say, that with the latter approach one might tend to include too much logic into the template. "Oh, I forgot to load the all products and categories from the database. Well, I'll just include it right here." And yes, this could happen with the first approach as well:


&lt;?php elseif ($show_contact_category === 'show_with_link') : ?&gt;
    &lt;?php $contactLink = ContactHelperRoute::getCategoryRoute($this-&gt;contact-&gt;catid); ?&gt;



This should happen somewhere in the controller or any other part before the template. However, I find it easier, to keep logic out of the presentation layer, if I'm filling and not creating a template.

Help from the IDE

Your IDE will find problems in the template, like unclosed elements, if you're actually writing HTML instead of creating strings containing HTML.

Switching to a different template engine or even language

At some point you might want to switch from PHP templating to a template engine  like Twig or Moustache. It's way easier when you have everything ready for this:

&lt;h1&gt;&lt;?=$this-&gt;escape($tparams-&gt;get('page_heading'))?&gt;&lt;/h1&gt;


Can easily become this in Twig – as it automatically escapse strings:

&lt;h1&gt;{{ page_heading }}&lt;/h1&gt;


Or if you switch to Moustache you aren't even coupled to PHP anymore. You could use your template with a lot of other languages. Or you might even want to include the template in a native APP in the future.