3

I am deciding if to use some template engine in php code or no. Now I take close look to Smarty.

To be honest, it is not clear for me its benefits.

My impressions are not very good from the very beginning. I tried to go through the demo application: http://www.smarty.net/sampleapp1. And from the very beginning I obtained notices . I fixed this by passing values to forms. I changes the code from

// adding a guestbook entry
$guestbook->displayForm();

to

// adding a guestbook entry
$guestbook->displayForm(array('Name' => '', 'Comment' => ''));

While guessing this I understand that it complicates debugging the code. And this also makes me to learn one more language - it is easy, but most easy is not to learn something new at all if there is no needed. Alternative is to use PHP itself.

Syntax comparison http://www.smarty.net/syntax_comparison is most funny for me: calculate number of symbols.... But I am familiar with PHP and this is easy for me to write on it, and Smarty is new for me. And I don't think that for Web Designers are so difficult to learn some simple php constructs, and Smarty is so simpler in this respect. If I am wrong it would be good to hear web designer here.

Smarty parses the code and this is in fact time overhead.

But at the same time many people use it. And that is why I would like to understand the reasons for this? Where are the benefits? Am I missing something?

If you know good alternative to Smarty, it would be good to know about. Thanks!

UPDATE:

I have found also the question on subject: How to use Smarty better with PHP?. In general it is also about not to use Smarty. But this is 1.5 years old. Possibly something changed?

Community
  • 1
  • 1
sergtk
  • 10,714
  • 15
  • 75
  • 130

4 Answers4

4

I agree that it causes overhead, I worked in a project that used Smarty and it was a nightmare. The two benefits you can get are

1: designers that are not familiar with PHP can work out with your HTML without coding skills required. But doesn't that mean they will eventually need to study Smarty first? I really don't see any point.

2: For template separation: you can use PHP alternative syntax the same way you would use Smarty, without losing the power of that giant programming language. Example of alternative syntax:

<div>Product list</div>
<?php foreach($products as $product): ?>

<div>Product: <?php echo $product->name; ?> </div>
<div>Price: <?php echo $product->price; ?> </div>
<?php if($product->discountPercentage): ?>
<div>
Discount: <?php echo $product->price * $product->discountPercentage / 100; ?> off! </div>
<?php endif; ?>

<?php endforeach; ?>
doc_id
  • 1,363
  • 13
  • 41
  • 1
    On the you point I fully agree with. But you points are very valuable for because you have experience in real projects with Smarty. Thanks! And I thing exactly in the same way concerning alternative syntax. Moreover now we can frequently replace – sergtk Jan 22 '11 at 02:23
  • I totally agree! Template engines have no pointer at all. You use them to make web designer able to avoid the learning of a new language. However they have to Study smarty first. Also using a template engine will make your site so heavy you will need of caching! – Shoe Jan 22 '11 at 07:42
  • 1
    @Charlie Pigarelli Agree with you for now if language allows to introduce tags inside html code, and PHP is such language. Just looking some arguments of other point of view from other experienced people. – sergtk Jan 22 '11 at 08:32
  • 1
    And remember that "Smarty helps you to separate logic from view" is not a point. PHP do it already. I'm using a template class i create which use only PHP and separate very well logic and view as Smarty do. It is faster than Smarty and don't use any caching system (which i personally really hate). – Shoe Jan 22 '11 at 13:05
  • 1
    Oh and i totally forgot to point out that PHP is way more safer than any Template Engine you could ever use! – Shoe Jan 22 '11 at 13:10
  • Yeah... Add to this the overhead caused by parsing the template file! – doc_id Jan 22 '11 at 19:53
  • @rahmanisback Accepted. Thank you for sharing your experience, I decided to implement pure PHP template engine - its really simple. I even already found links to the articles where people share their experience to solve such task. – sergtk Jan 22 '11 at 21:04
2

I use it in a production environment, and I would say: Not Very Many.

If you're concerned with the look of your code (not an unimportant concern), smarty can make things look a little bit cleaner:

<?php echo $some_var; ?>

vs.:

{$some_var}

this can make your template files much more readable.

It also enforces a stricter separation between your controller and view logic. (since you have to make sure to pass all the relevant variables into the smarty object)

The syntax is straight-forward enough (more so for later versions), so that's not much of a problem.

It can complicate debugging, if only because it is a magical separation layer between your control and view, but in a perfect world you would be verifying each separately anyways.....

Drawbacks? I think there's a couple:

-Obviously you now have a whole other library to deal with, include, etc.

-Some previous smarty versions had some ridiculous syntax for loops and other stuff that should have been simple.

-When you need to put a simple javascript statement somewhere in your template, like this:

<script type='text/javascript'>
    $(document).ready(function(){
        //foo
    });
</script>

ok, you might include that in a js file instead of inline, but there's a lot of cases where you just need those couple of lines of js. smarty doesn't like the curly braces and you have to wrap them in an ugly tag that looks like this:

{literal}{\literal}

in that case so much for making your code any cleaner....

If I had to choose, I probably would just use the non-standard short-form tag and be done with it.

awongh
  • 1,308
  • 12
  • 21
  • 1
    Thanks for answer. Your post make me even more convinced not to use any template engines :) Proc looks like "SMARTY is not worse then pure php", but I need something better :) And just small notes: could be rewritten as = $some_var ?>. – sergtk Jan 22 '11 at 08:28
  • 2
    If smarty only allows you to write {$some_var} instead = $some_var ?> is very pointless. You guys better understand that web designers are not as stupid as you all think. Teaching to a web designer that 'echo' means 'output' is not that hard. Give web designers a try... – Shoe Jan 22 '11 at 13:07
0

We extensively use Smarty.

I would say it is dis-advantageous in terms:

1. Parsing of smarty templates to PHP which make things slow
2. Your learning ablity to cope-up with smarty syntax.

It is advantageous in the sense:

  1. Separation of Business logic from the Presentational logic.
  2. If you need display helpers, it would be helpful towards DRY principle. (Just like helpers in erb - ROR)

Unless your product is going to be a large and ongoing product and if things are possible with PHP, avoid using Smarty.

Abhishek Mehta
  • 1,447
  • 1
  • 14
  • 16
0

I thought I'd give Smarty a try for my most recent project, just because some job postings I came across mentioned wanting experience in using it. I have not delved too deep into the documentation, just acquired a working knowledge to use on a rather simple site. Here are my impressions thus far-

The good

Most sites I have worked on, present project included, follow a fairly similar structure (template) from page to page. In the past, I have simply kept an updated blank template somewhere in the file structure and populated it with additional HTML or PHP on a case by case basis. One nice thing about Smarty is that you can store this general template in the Smarty Template directory and populate it using the {extends} function in the presentation page's template. For example, this would be the saved template (missing doctype, SO giving me a headache)-

`

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Description" content="{block name=meta_description}{/block}" />
    <title>My Smarty Site - {block name=pageTitle}{/block}</title>
    {block name=additionalHeaders}{/block}
 </head>
    <body>
        <div id="headerDIV">
            <div id="nav-box">
                <!-- Nav would go here -->
            </div>
        </div><!-- end #headerDIV -->
        <div id="mainContainer">
            <div id="generalContent">
                    <div id="pageTitle">
                        {block name=pageTitle}{/block}
                    </div>
                 {block name=pageText1}{/block}
                <div id="page-bottom">
                    {block name=pageText2}{/block}
                </div>
            </div> <!-- end #generalContent -->
        </div> <!-- end #mainContainer -->
    </body>

` and then populated like so:

{extends file="myTemplate.tpl"}
{block name=packageTitle}Simple Template{/block}
{block name=meta_description}This is a simple Smarty Template test{/block}
{block name=additionalHeaders}
    <link rel="stylesheet" type="text/css" href="/css/smarty_test.css"/>
{/block}

{block name=pageText1}
    <p>Hello, {$name}! This is a Smarty Test.</p>
{/block}
{block name=pageText2}
<p>This is some more text....</p>
{/block}

So, rather than copying my template over and over again for each similar page, I simply extend it using Smarty with the content for that particular page. Another advantage to that is when I need to change some aspect of that template; rather than changing multiple pages, I change just that one (yes, similar to using a lot of PHP includes).

The Bad

While I know that Smarty has a lot of built in functions and can do some cool things, to really get the most out of it you have to learn its syntax. And, if you already know PHP, you're learning something entirely new to accomplish the same end result. Separation of logic and design is great until you need to make very dynamic page with lots going on - then you find yourself adding a lot of conditional statements, loops, etc. to the template using Smarty syntax and you realize that you might as well just use PHP.

I do plan on learning a bit more about Smarty, simply because it's used in a lot of apps I have come across and has been listed as a requirement for jobs, but I do not see myself using it that much in future projects of my own.

NightMICU
  • 9,000
  • 30
  • 89
  • 121