0

I really don't see the difference between these. How is the templating any better? I don't just don't understand it or how I could convince someone to use it I work with. I understand "separation" of concerns, but I haven't had huge issues. Isn't the first example generating multiple <li> tags just like an echo would? And, I would use php as the "templating language," so I'm not concerned with Smarty or some other system. Thanks.

Why should I use templating system in PHP?

<h1><?=$title?></h1>
<ul>
  <?php foreach ($items as $item) {?>
  <li><?=$item?></li>
  <?php } ?>
</ul>

and (some snippet I found in a forum for an example):

echo "<table>";

for ($i = 0; $i < $largestArray; $i++)
{
        echo "<tr>";

        if ($i < $array1Size)
        {
                echo "<td>";
                echo $array1[$i];
                echo "</td>";           
        }
        else
        {
                echo "<td>";
                echo "null";
                echo "</td>";           
        }

        if ($i < $array2Size)
        {
                echo "<td>";
                echo $array2[$i];
                echo "</td>";           
        }
        else
        {
                echo "<td>";
                echo "null";
                echo "</td>";           
        }

        echo "</tr>";
}

echo "</table>";

or something like this:

<table id="working" border="0" cellspacing="1" cellpadding="3">
    <tr>
        <?php foreach ($csv->titles as $value): ?>
            <td><?php echo $value; ?></td>
        <?php endforeach; ?>
    </tr>

    <?php foreach ($csv->data as $key => $row): ?>
        <tr>
            <?php foreach ($row as $value): ?>
                <td><?php echo $value; ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

EDIT: Should this be a community wiki question?

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • 1
    Not sure how this isn't just a duplicate of http://stackoverflow.com/questions/436014/why-should-i-use-templating-system-in-php – peacemaker Aug 02 '12 at 17:05
  • @peacemaker It didn't really answer the question. It didn't address emitted html. – johnny Aug 02 '12 at 17:08

3 Answers3

1

Your identation seems a bit messed up, but the main point is that the second version is far mor readable (you can easily read tag hierarchy, for example). And a readable code is a code which could be easily maintained or refactored.

Writing the code below using only PHP's echo keyword wouldn't be very readable. You would also had concern about quotes and double quotes. Using a template-like approach, you end up with a code which look similar to an HTML document.

<table id="working" border="0" cellspacing="1" cellpadding="3">
    <tr>
        <?php foreach ($csv->titles as $value): ?>
            <td><?php echo $value; ?></td>
        <?php endforeach; ?>
    </tr>
    <?php foreach ($csv->data as $key => $row): ?>
        <tr>
            <?php foreach ($row as $value): ?>
                <td><?php echo $value; ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>
Maël Nison
  • 7,055
  • 7
  • 46
  • 77
  • The second version with the emitted html? – johnny Aug 02 '12 at 17:07
  • @johnny I've edited my answer, hope it's clearer. Basically, having an HTML document style is suitable when you're generating an HTML document (it doesn't break the flow). If you try to use the PHP classical way, you will end up with a lot of textual overhead. – Maël Nison Aug 02 '12 at 17:13
  • Thanks. This looks horrible to me versus the emitted one. I cannot imagine how this is better. I don't mean that against you, just how this looks in comparison. What is textual overhead? – johnny Aug 02 '12 at 17:15
  • 2
    Well for example, you will end up with at least eight 'echo' statement (which won't serve any design purpose). Your strings will have to be quoted, and your text editor will be unable to highlight the html elements (so if you forgot an attribute quote somewhere, it will be a pain to detect). – Maël Nison Aug 02 '12 at 17:20
  • That's why some people have wrote template engines such as Twig or Smarty, which are much easier to maintain than raw php files : ) – Maël Nison Oct 21 '12 at 22:16
0

Templating systems such as cakephp and codeignitor use MVC which keeps code cleaner and more standards compliant. You could also write your own MVC.

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • Firstly, those aren't "templating systems," they're *frameworks*. Secondly, neither of those frameworks use MVC. They both claim to, but are actually an implementation of MVA, or some subtype of an ORM-template-adapter pattern. Traditional MVC frameworks are **very** few in number in PHP – orourkek Aug 02 '12 at 17:07
  • This really didn't attempt to address my concerns. I am thankful for the effort, though. – johnny Aug 02 '12 at 17:08
  • @orourkek, that's interesting. Whats an example of a real mvc in php? – user1477388 Aug 02 '12 at 17:35
  • @user1477388 [Start with what a "Model" is](http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000), that's usually the largest difference between proper MVC and what frameworks claim is MVC (which is usually MVA or MVP) – orourkek Aug 02 '12 at 17:44
  • @orourkek Is this just a technicality or is there a profound difference between the way cakephp would operate vs. asp.net mvc? It seems to be the same as far as routing and business logic is concerned. – user1477388 Aug 02 '12 at 17:50
  • We're talking about differences between design patterns, which are inherently "profound." Within the scope of proper MVC vs [most] PHP frameworks, the differences are massive, especially in the model layer, but also in a more general sense regarding development styles, logic handling, presentation, and internal data handling. – orourkek Aug 02 '12 at 17:56
  • Good info. So to my original question, what is a real mvc php framework in your opinion? Are we talking about Zend here? – user1477388 Aug 02 '12 at 17:58
0

Template system(s) are not the end-all-be-all solution to separation of presentation logic. They are a solution, and one used by many PHP frameworks. In general "MVC"/MVA/MVP/MVVM PHP development, you do not want any business logic in the presentation layer. Echoing out HTML is a (usually) direct violation of this practice; With templates, business logic is not present in the presentation layer, and vice versa.

Skipping back to the first point, templates are a solution, not the solution. There are a ton of options available for the presentation layer, depending largely on your coding style and/or framework/library usage.

On a personal level I prefer templates (for most small-med projects) for a few reasons beyond what's outlined above:

  1. Makes "theming" and swapping site styles a lot easier. Simply modify the template or implement a theme switcher that can load different templates

  2. Going from mockups to templates is a breeze, because templates are (usually) just HTML with some php snippets echoing out data.

  3. Templates are "dumb." They don't need to know about anything that's happening with the business logic, all they care about is echoing out some data into the HTML.

  4. Readability. This one is almost entirely personal preference, especially considering the wide range of template syntaxes out there.

tl;dr - Templating vs. [insert other solution here] is almost entirely subjective, unless you have constraints based on hosting, customer requirements, traffic levels, etc. If that's the case, then find benchmarks and more about efficiency and scalability and make an informed decision based on the requirements of the project.

orourkek
  • 2,091
  • 15
  • 22