Please use partials in your ZF apps
Wouldn’t you agree that using exactly the same markup in 15 views is a little bit redundant? Now imagine doing that 10 times in your application (that’s 15*10-15 which equals to 135 reduntant repetitions of the same piece of XHTML). Well, that’s the easier part. Now imagine you need to make few changes in the markup after few days. Guess what – you are going to do those worthless 135 repetitions again. And surely you might need more changes in the markup two weeks later… I could go on. There’s nothing easier than avoiding this problem in Zend Framework.
The solution is to use partial view helper which allows you to put the repetitive XHTML into a single file. For example, this could be your partial:
-
<?php // saved in views/scripts/index/partials/partial.phtml ?>
-
<h3><?php echo $this->escape($this->heading); ?></h3>
-
<p>
-
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
-
Mauris porta, sapien at accumsan venenatis, neque velit
-
sodales augue, sit amet posuere odio enim quis risus.
-
Aliquam semper, ipsum porttitor ultricies bibendum, sem
-
sapien tincidunt purus, eget sollicitudin felis eros
-
fermentum felis. Praesent vehicula varius est at semper.
-
Nam nulla augue, fermentum in varius vel, tincidunt non
-
nibh. In vitae neque vitae metus dignissim hendrerit.
-
Nulla eget massa nec sapien tempor ultricies a et tellus.
-
Sed imperdiet, risus quis vehicula ornare, justo lorem
-
iaculis sapien, id semper massa nisi eu purus.
-
Suspendisse sed urna eu risus rhoncus bibendum. Cras
-
feugiat consectetur nisi id dictum. Pellentesque quis
-
mauris a risus dapibus dictum nec sed nisl.
-
</p>
-
<ul>
-
<li>List Item 1: <?php echo $this->escape($this->li1); ?></li>
-
<li>List Item 2: <?php echo $this->escape($this->li2); ?></li>
-
<li>List Item 3: <?php echo $this->escape($this->li3); ?></li>
-
</ul>
And this is how you would include it in all views where the same piece of markup occurs:
-
<?php echo $this->partial('index/partials/partial.phtml', array(
-
'heading' => 'Lorem Ipsum',
-
'li1' => 'Hello',
-
'li2' => 'World',
-
'li3' => '!!!')); ?>
It’s really easy and it saves a huge amount of time both to you and to a developer that might work on your application in the future so please, all of you Zend Framework developers, use partial helpers wherever there is a repetitive XHTML markup in your application.
