开发者

Which benefits Smarty gives when using with PHP or better to use pure PHP?

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 sub开发者_Go百科ject: 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?


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; ?>


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜