SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    All About Smarty - The PHP Template Engine

    These comments are in regards to the SitePoint.com article \'All About Smarty - The PHP Template Engine\'.

    Although it's an excellent article something doesn't feel quite right when i read about Smarty ( or any other template-engine for PHP )

    Because in essence PHP is already a template-language. The design of the language itself isn't that interesting ( flamewar alert ) it uses the best of all languages to pruduce the fastes results for its goal, which is to be the best webscripting language. What PHP makes interesting IMO are the modules, which allows you to do all sort of things.

    As noted in the FAQ of Smarty, most template-engines are nothing more than a wrapper around a filereading and string substitution wrapper. I guess most of you already tried to write a simple template-parser script in PHP. THere are some smart feattures that makes Smarty preferable compared to other solutions, one of the most significant ones is that it compiles to PHP code. But the question remains:

    Why would i want to have a PHP script that transforms my data into a presentation form using a language which goal is to do that.

    For example, i can use simple control structures in Smarty:

    PHP Code:
    {if $name eq "Fred"}
        
    You are not allowed here
    {else}
        
    Welcome, <font color="{#fontColor#}">{$name}!</font>
    {/if} 
    now what is the difference with the if/else structure written in PHP, and more important, what is the advantage?:

    PHP Code:
    if ( $name == "Fred ) {
        echo "
    You are not allowed here";
    } else {
        echo "
    Welcome, <font color='$fontColor'>$name!</font>";

    If you look at profesional template solutions, you can already use a scripting language like Perl or Python in your templates, so if your point is that it's easier to learn for your (template) designer..it's not.

    But enough about implementation, the real point why you would use a template-engine is that you wanna have a proper shifting of your data and your presentation. You want to
    transform your data into a view ( or presentation ) If you only need one view of your data, let's say a html presentation of your data like a website, a combination of PHP and a template-parser would be enough, but let's say i want different views on the same data. You'll soon hit some limitations.

    A website can profit from a framework that uses different layers. In most situations, your view is gonna be generated from a fixed part of your data. Per page, a template is gonna be made and filled with your data-set. Whenever your presentation is gonna be altered and more data is needed, your ****ed. The code to deliver that data has to be altered as well, because your template is generated.

    A solution would be to use an XML-XSL solution. First, your presentation is even more independent then your data. The presentation itself can use other data to make new combinations.

    For clarity, it's often better to setup your structure not as a collection of HTML files. You'll often see a 1-on-1 mapping from data producers to HTML pages. With XML-XSL that isn't the case at all.

    The beauty is that you concentrate on what data you have to offer instead what view you need. You don't have to worry on what presentation you gonna offer or the data the presentation might need. Your solution will be XML-based and with the help of XSLT you can transform data to (X)HTML. You could even combine differen data sources to achieve this.

    Major advantage: you're working on an atomic level instead of concentrating about delivering your data.

    I do have to agree it's an extra complicating factor. I'm certainly not the one to say webdesigners should write XSLT stylesheets. However, the framework laid out here using a clear seperation between the data you have to offer and the HTML presentation on the web certainly adds another level of professionalism and in the long run will be worthwile.

    How is that for brainfood?

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    thanks so much for sharing your opinions and comments... for one i would like to get feedback on the apparent failure of the article

    Why would i want to have a PHP script that transforms my data into a presentation form using a language which goal is to do that.
    exactly the point one of our other members, voostind (vincent) raised in this thread... and yes i admit that there is a point to that but from my own experience and viewpoint, PHP has evolved much further than that and an even higher level of abstraction wouldn't hurt anyone, especially when there isn't much of a speed bump abstracting higher.... anyway, most of my points are in that thread so i won't bother to repeat them...

    If you look at profesional template solutions, you can already use a scripting language like Perl or Python in your templates, so if your point is that it's easier to learn for your (template) designer..it's not.
    what do you mean? you mean that professional template solutions require you to use Perl or Python? (you mean something else i think but i don't get it)

    btw, my article's point is to bring across the features of Smarty and how those features make it 'outstanding' compared to the other PHP ones, like patTemplate, FastTemplate, phplib templates, etc.

    A website can profit from a framework that uses different layers. In most situations, your view is gonna be generated from a fixed part of your data. Per page, a template is gonna be made and filled with your data-set. Whenever your presentation is gonna be altered and more data is needed, your ****ed. The code to deliver that data has to be altered as well, because your template is generated.
    i disagree that you'd be ****ed (whatever that was )... if your presentation is gonna be altered, make the changes to your template (or create a new one); that part's no big deal admittedly... more data? that's exactly why you separate the business logic from the presentation... with templates, you just alter your code/script to reflect the changes to the business rules... i gotta admit i don't see your point...

    A solution would be to use an XML-XSL solution. First, your presentation is even more independent then your data. The presentation itself can use other data to make new combinations.

    // snip

    I do have to agree it's an extra complicating factor. I'm certainly not the one to say webdesigners should write XSLT stylesheets. However, the framework laid out here using a clear seperation between the data you have to offer and the HTML presentation on the web certainly adds another level of professionalism and in the long run will be worthwile.
    yes i appreciate the simplicity of using XML and XSL... that too in my opinion is the way to go... your point is one i agree with, but it is not something i'd throw to my template designers to do... perhaps when it catches on with more people, that day would come

    - Joel

  3. #3
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use smarty for two very big reasons.

    First, I like the fact that I can completely seperate my presentation from my processing. This not only saves me the headache of tromping through PHP code to make HTML changes, but it also allows me to change the look of the entire website on the fly. This became very very useful when I developed my companies Website templates. My customers are able to login and choose a different template just by clicking a button. the whole site layout is altered by just changing what template to load. You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.

    Second, the caching features of Smarty are amazing. I am able to cache my website (vanndata.com) Whenever I make changes I simply delete the cache files or just wait until the current cache dies.

    There is still alot I must learn about Smarty but I beleive it to be one of the strongest templating systems out there.

    just my 2 cents
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: All About Smarty - The PHP Template Engine

    Originally posted by oh,when?
    For example, i can use simple control structures in Smarty:

    PHP Code:
    {if $name eq "Fred"}
        
    You are not allowed here
    {else}
        
    Welcome, <font color="{#fontColor#}">{$name}!</font>
    {/if} 
    now what is the difference with the if/else structure written in PHP, and more important, what is the advantage?:

    PHP Code:
    if ( $name == "Fred ) {
        echo "
    You are not allowed here";
    } else {
        echo "
    Welcome, <font color='$fontColor'>$name!</font>";


    I completly agree with that, there is no point, nor is there a point in doing this:

    PHP Code:
    {$name
    That's just pure PHP inside tags! No point..

    In something like fast template, it looks like templating:

    {NAME}
    Defined into a variable, but it doestn' have ANY php in the template file tiself.


    Smarty... *sighs*

    Sowi

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My customers are able to login and choose a different template just by clicking a button. the whole site layout is altered by just changing what template to load. You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.
    You're absolutely right! That wheel has already been invented, but for you're information, it wasn't by the Smarty-developers...

    It's called Cascading Style Sheets or CSS for short.

    Vincent

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i won't use any of these template systems that are available. i guess just for the reason that they'd be too slow. the fact that Smarty has such a huge improvement with PHP/Zend Accelerator tells me that it must be really slow without an accelerator. every ounce of performance needs to be squeezed out of these things, but i don't think it's being done.

    also, i don't like the idea of caching script output in most cases. just use an Accelerator. even if you're caching output, the script has to go through the expensive compilation without an Accelerator.

    i've had to design my own templating system so that other users can change their templates. i've played around with different methods (eval(), str_replace(), preg_replace(), etc.) and i think i've figured out how to do it as fast as possible, but i still wish it would be much faster.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well...this was my first post here and i didn't know someone already replied to the Smarty article. And i agree with voostind for 100%. Or 200%.

    He hits the center of the 'problem' exactly and by the reactions of the posts i noticed lot alot of people fully understand his point. Like him, i studied Computer Science and have been working in the industry for awhile.

    There is no need to repeat things so i suggest all to read his excellent post if you want to know what my point exactly is.

    Originally posted by redemption

    what do you mean? you mean that professional template solutions require you to use Perl or Python? (you mean something else i think but i don't get it)
    nope. what i mean is already posted ( again by voostin so i''l quote him instead )

    The template engine-specific language is nothing more than a minimal programming language with an ugly syntax. It has variables, conditionals, loops, and maybe even functions. As PHP is a full programming language, it has all of these constructs, and more. But that doesn't mean you require your web designers to know all there is to know about PHP. The set of requirements doesn't change at all. Just the basics will be enough.
    Originally posted by redemption

    i disagree that you'd be ****ed (whatever that was )... if your presentation is gonna be altered, make the changes to your template (or create a new one); that part's no big deal admittedly... more data? that's exactly why you separate the business logic from the presentation... with templates, you just alter your code/script to reflect the changes to the business rules... i gotta admit i don't see your point...
    If you're gonna alter your presentation so, that you need more data, there's no doubt you'll have to change your code to deliver that data. Your template is gonna be 'filled'. With an XML-XSL solution your one step ahead because your presentation is even more independent then your data. It can use other sources to make new combinations.

    Originally posted by dkode


    First, I like the fact that I can completely seperate my presentation from my processing.

    [..]

    You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.

    [..]

    Second, the caching features of Smarty are amazing.

    [..]

    There is still alot I must learn about Smarty but I beleive it to be one of the strongest templating systems out there.
    You are not seperating the content from processing. In fact 90% of the php developers using some sort of template-engine are still busy with the presentation in the business-logic layer. Most of the scripts written in PHP consists of a few steps:

    - connect to database
    - get a dataset, maybe depending on your input
    - proces your dataset
    - output it ( using a template engine )

    The essential point here is the 4th step. What if you want to depend your output on the dataset you processed. Or the input. Most developers will happy code that in the business logic, thus eliminating the main point of using a template-engine. Because that is what should be in your presentation layer.

    And for the cache, what's the point of caching using a template-engine, and adding yet another step before actually displaying your content. PHP can do that and has all the functions for it ( filehandling, output caching ). Less code to execute...

  8. #8
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off the top of my head, here are some scenarios where a PHP template engine such as Smarty becomes useful:
    • When you want to have seperate PHP developers and HTML designers working on a project, and you don't want the designers messing around with the PHP code and potentially breaking it.
    • When you want to be able to quickly alter the HTML output of a script without having to delve in to the PHP code to find the HTMl you want to change.
    • When you want to provide alternative outputs for a script - miuch easier to just have different templates and let the application chose which one to execute.
    • When you want to keep your templates in a database for flexibility (makes it easier to write editing tools for them) - Smarty can load templates from a database rather than the file system.
    At the end of the day, a template system is all about separating your application logic from your presentation logic. Obviously it isn't necessary in smaller scripts which just grab some content from a database and display it, but as soon as you start writing complex applications with PHP a template system becomes pretty much essential.

    Be sure to read "What Is Smarty" in the documentation for more info: http://smarty.php.net/manual/en/what.is.smarty.html

    Bias disclaimer: I set up the Smarty Wiki In fact I'm considering changing this thread (or at least the points raised on it) in to a Wiki page.

  9. #9
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to spoil your fun, but all the points you mention here have already been examined (in that other post on Smarty) and questioned (by me)... You may want to look there, because it's little to no use to repeat that whole thread all over again here, now is it?

    Cheers!

    Vincent

  10. #10
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I apologise - I hadn't read that thread (this forum moves way too fast for me to keep up ). I'll go and read it now.

  11. #11
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    Bias disclaimer: I set up the Smarty Wiki In fact I'm considering changing this thread (or at least the points raised on it) in to a Wiki page.
    oh yeah? great Skunk! i liked that feature...

    what's your real name btw? (i don't mean to pry so if you don't feel comfortable ignore my curiousity )

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2001
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smarty "Why Use"

    Take a look at this link, it covers most of the arguments, and explains Smarty pretty well.

    http://smarty.php.net/whyuse.php

  13. #13
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at this link, it covers most of the arguments, and explains Smarty pretty well.
    I don't think that article explains Smarty at all. What it does is sum up a list of problems (all of which I completely agree with), and then it introduces Smarty as the solution to these problems.

    As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

    Vincent
    Last edited by voostind; Dec 11, 2002 at 01:31.

  14. #14
    SitePoint Enthusiast Remy's Avatar
    Join Date
    Oct 2002
    Location
    Amsterdam
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're absolutely right! That wheel has already been invented, but for you're information, it wasn't by the Smarty-developers...

    It's called Cascading Style Sheets or CSS for short.
    As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

    Vincent
    Vincent already give good arguments about template-engines in this other thread and gives another good solution for these problems without templates: CSS!.

    And yet none is taking into consideration to use CSS (in this thread), something I think is strange because it solves a lot (IMHO all of them) of the problems mentioned in this thread.

    I now use XHTML 1.1 what forces you to use CSS. It gives me a lot of benefits:
    - I can use different stylesheets for handheld, webtv, printing of a webpage, etc.
    - I can change the complete layout, only with replacing the stylesheet.
    - when the stylesheet can't be loaded\read (old browsers, missing stylesheet) the content can be still be read and still makes sense.
    - the HTML-code is more readable and makes more sense
    - no more tables for layout. I use DIV and SPAN to do this. This http://www.webmasterbase.com/article/379 explains how to begin with this.

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2001
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by voostind


    I don't think that article explains Smarty at all. What it does is sum up a list of problems (all of which I completely agree with), and then it introduces Smarty as the solution to these problems.

    As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

    Vincent
    Well of course there is no perfect one solution, and Smarty doesn't claim to be. Smarty is *a* solution. It is up to you to identify your requirements and use a solution that works best for you. The point of the article is to explain how Smarty might be able to help you, not that it is the answer to every problem.

  16. #16
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    Off the top of my head, here are some scenarios where a PHP template engine such as Smarty becomes useful:
    • When you want to have seperate PHP developers and HTML designers working on a project, and you don't want the designers messing around with the PHP code and potentially breaking it.
    • When you want to be able to quickly alter the HTML output of a script without having to delve in to the PHP code to find the HTMl you want to change.
    • When you want to provide alternative outputs for a script - miuch easier to just have different templates and let the application chose which one to execute.
    • When you want to keep your templates in a database for flexibility (makes it easier to write editing tools for them) - Smarty can load templates from a database rather than the file system.
    At the end of the day, a template system is all about separating your application logic from your presentation logic. Obviously it isn't necessary in smaller scripts which just grab some content from a database and display it, but as soon as you start writing complex applications with PHP a template system becomes pretty much essential.

    Be sure to read "What Is Smarty" in the documentation for more info: http://smarty.php.net/manual/en/what.is.smarty.html

    Bias disclaimer: I set up the Smarty Wiki In fact I'm considering changing this thread (or at least the points raised on it) in to a Wiki page.

    hmmm .. really i don`t know whay u need to use such templates such smarty , or templib .. etc ...

    am using template way coz of point which Skunk said, but in all templates you`ll use some commands of the tempate, which maybe the site designer know nothing about it !

    i think much better to keep all the for the programmer better, for this am using this way !

    PHP Code:
    //template1.php
    <?=$Content?>

    and the all the files will call this file !

    PHP Code:
    // index.php //
    <?
    $Content 
    "Say Hi <hr>";
    if ( 
    $name == "Fred" ) {
        
    $Content .= "You are not allowed here";
    } else {
        
    $Content .="Welcome, <font color='$fontColor'>$name!</font>";
    }

    include(
    "template.php");
    ?>

    in this way the designer need just to know where to show the $Content !

    also i think is much faster than normal template, coz all of this using *_replace , as i think !


    what u think ... ?!

  17. #17
    SitePoint Zealot
    Join Date
    Mar 2001
    Location
    North Wales, UK
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But then you're putting HTML in your PHP files, which defeats the purpose of using templating systems in the first place - to separate presentation from content. Your approach is fine if you only need templates to put some sort of header and footer on each page, but if you want more control over the presentation you need to be able to change the layout of the page content itself.

  18. #18
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So why not just teach the designer how to do this?
    PHP Code:
    <?php
    if ($name == 'Fred') {
    ?>
    <font color="<?=$fontcolor?>">Welcome Fred!
    <?php
    }
    ?>
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  19. #19
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by tfountain
    But then you're putting HTML in your PHP files, which defeats the purpose of using templating systems in the first place - to separate presentation from content. Your approach is fine if you only need templates to put some sort of header and footer on each page, but if you want more control over the presentation you need to be able to change the layout of the page content itself.

    in this case if u don`t wanna put some html code , u can use fopen, or include another html code, such tables and css or js code , and if use html u can give it css style, so when u change the template the html code which is in the php files will change as u change the css !

    and 1 more thing ... whay the smarty use cache file ?
    in the cashe file no need to use *_replace cmd , and this way is faster, that mean smarty is slow without cache files, with my way is more faster, without cache and no need to delete the old cashe files after each update !

  20. #20
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I can see, templates are about making sites managable and separating logic from design. Smarty, or other template engines do NOT do this. If/else statements are considered logic, so how can they be separated if you need them to display your design?

    As far as I know, XML is really the best templating as you specify tags, then in the xml definition to tell the browser what to do with each tag, but then still, some logic has to be built into the design.

    -Peter

  21. #21
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with the way what am using there no login (if/else) or any php cmd in the template !

  22. #22
    Anonymous
    SitePoint Community Guest
    I've used Smarty templates in the past and once the template is made, you can reuse it by modifying the index.php file to suit your site and load your html in the body. It enables you to have one file for navigation, header and footer (if you prefer) without using frames (I personally don't care for them). That seems very simple to me.

  23. #23
    Anonymous
    SitePoint Community Guest
    Template should be about seperating *business* logic from *design* logic.
    Things like looping to change colour of a row in a html page is perfectly acceptable use of logic in the presentation layer.
    Using smarty to check whether or not the value returned from the Database is greater than or less than $x would be incorrect use since this is business logic and belongs in the application layer.
    I am not a smarty user but this is how undertand it.

  24. #24
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, also, maybe even better than:

    PHP Code:
    {NAME
    would be

    PHP Code:
    <!--{NAME}--> 
    because then if the $name variable isn't set it will just make an HTML comment, which the regular user definitely won't see
    Last edited by Kilroy; May 22, 2004 at 03:50.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •