Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Smarty better with PHP?

I found that using Smarty with PHP, sometimes extra time will need to be used for

1) using quite different syntax than PHP itself
2) need to check small cases, because documentation doesn't give finer details, such as for "escape"

http://www.smarty.net/manual/en/language.modifier.escape.php

it doesn't say escape:"quotes" is for double quotes only or for single quotes as well, so you need to write code to test it. Also for the case of escape:"javascript" -- can't tell exactly what and how it is escaped.

3) for something complicated, need to write helper functions or modifiers, so it needs a creation of new files and ending up doing it in PHP again.

by the way, does using Smarty provide a good speed up over use PHP alone? thanks.

like image 782
nonopolarity Avatar asked May 17 '09 16:05

nonopolarity


4 Answers

First, PHP is a templating language. Keep that in mind when you talk about using a template system for your PHP-based web applications.

The only 'real' argument that I've ever heard for using ANY templating engine was that they provide a simpler language for template manipulation which can be handy if you have template designers who don't know PHP and whom you don't trust to learn to use PHP judiciously.

Regarding these arguments, I would argue that if your template designers are not competent to learn enough PHP for template design, you should probably consider finding new template designers. Additionally, PHP itself provides a different syntax for control statements that you might use in a template versus in code. For example:

<? foreach($array as $key => $val): ?>
    <?= $val ?>
<? endforeach; ?>

VS:

<?php
    foreach($array as $key => $val) {
        echo $val;
    }

?>

Personally, I believe that templating engines arose in PHP because:

  1. That's way that other languages do it
  2. Better PHP programmers realized that they needed a way to enforce separation between presentation and application logic and templates were an easy way to do this.

The first reason is just kinda silly. The second reason can be overcome with a little self-control and even a rudimentary understanding of the necessity of separating layers in an application. The MVC design pattern is one way of approaching this problem. As far as exercising some self-control, my rule is that only necessary loops and if statements get used as well as functions that filter, escape, format the output for the screen.

Having used Smarty extensively, I can honestly say that it always presented me with more hurdles to get over than it did solutions. If anything, switching to PHP-based templates is what has actually decreased development time for both templates and code.

like image 59
Noah Goodrich Avatar answered Sep 19 '22 07:09

Noah Goodrich


I don't like templating engines. I find them very lossy and resource-intensive for PHP.

With MediaWiki, around version 1.6.x we backed off using Smarty by default and just use PHP's built-in templating, with a great improvement in performance.

I've found that most of what people want to do with a templating system (add links, change colors, remove text or sections of the page) are better done with a simple system of event hooks.

Laconica, the open microblogging platform, doesn't do any templating by default. We have a plugin for people who are crazy for templating.

like image 44
Evan P. Avatar answered Sep 21 '22 07:09

Evan P.


Smarty is certainly one of the best template engines out there. In my experience though people would be well advised to think their use cases through more thoroughly before they use any templating engine on top of PHP at all.

First, PHP itself is perfect for templates. Pretty much the only justification for using another templating engine is if you're allowing untrusted users to create or edit templates, since they could execute all kinds of badness. So, if your project has user-editable templates, use Smarty. If not, stick with PHP.

If your problem is separation of code and layout, I suggest you look into implementing a lightweight MVC-style execution model. Or, to put it more condescendingly, if you have deeper logic code in your template, it's probably time to do some refactoring.

Performance is another consideration. Yes, rendering a Smarty template comes with a cost. But after it's done, the output should be cached, leading you to improved execution times. Same goes for PHP templates. PHP allows you to implement all kinds of granular caching models through the use of its output buffers. But beware of premature optimization: do that only after you're code complete and have identified what the actual bottlenecks are!

The biggest cost when using Smarty or any other engine comes in the form of developer time. It's another layer of complexity and inevitably you will find yourself in situations where you have to trick the engine into doing what you could have accomplished within pure PHP all along.

like image 41
Udo Avatar answered Sep 22 '22 07:09

Udo


I like template engines and think they should be used, but in the particular case of Smarty, I think it's waste of time, because it's not a significant improvement over PHP as templating language:

  • The new syntax is still based around old concept of special tags inserted in random places in the document.
  • Because Smarty does not understand HTML's syntax/structure, it cannot not help you to create valid/well-formed HTML. Smarty's tags violate HTML's syntax, so once you add them, other standard tools can't help you either.
  • Smarty's output, just like in PHP, is insecure (unescaped) by default and you have to remember to add |escape everywhere where you output data in HTML.

There's one particular PHP template engine that I've fallen in love with, which fixes all those problems: PHPTAL.

It's still something new you have to learn, and it's a depenency for your application, but I think having XSS and ill-formedness problems solved makes it worth the trouble.

PHPTAL just like Smarty is compiled once to PHP and cached, so performance is comparable to raw PHP.

like image 27
Kornel Avatar answered Sep 23 '22 07:09

Kornel