SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 52
  1. #1
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP templating systems - what do you use and have you tried Smarty?

    Great to see we've finally got an advanced PHP forum (don't know why it took me so long to find it)

    I've always been a big fan of seperating content from presentation - I very rarely (if ever) actually combine PHP and HTML via the <?php ?> tags as I find this leads to much less maintainable code.

    Up until now I've been using my own very simple template system, after some failed experiments with the PHPlib template class. My system is implemented as a simple function which accepts two arguments - a filename and an array of replacements to make. The function returns the contents of the file with all replacements made via str_replace - for example:
    Code:
    $html = ss_template('newsitem.tmpl', array(
        '%headline%' => $headline,
        '%date%' => $formatted_date,
        '%story%' => $story
    ));
    This works fine for most purposes but has the downside of generating a very large number of template files (if you consider that many pages on a databased site are made up of lots of repeated elements e.g the posts in a forum thread and each of these elements requires a seperate file in my system). It also still requires some presentation logic to stay in the PHP scripts - for example the logic to display "No comments", "1 comment" or "X comments" depending on if a value is 0, 1 or more than 1.

    Enter Smarty. I can't remember where I first read about this (I think it was an article on Zend.com) but I've been reading up on it and it seems to be the most wel featured and cleverly implemented PHP template system available. Here's the feature list (ripped from the site):
    • it is extremely fast
    • it is relatively simple since the PHP parser does the dirty work.
    • no template parsing overhead, only compiles once.
    • it is smart about recompiling only the template files that have changed.
    • you can make custom functions and custom variable modifiers, so the template language is extremely extensible.
    • configurable template delimiter tag syntax, so you can use {}, {{}}, <!--{}-->, or whatever you like.
    • template if/elseif/else/endif constructs are passed to the PHP parser, so the if syntax can be as simple or as complex as you like.
    • unlimited nesting of sections, ifs, etc. allowed
    • it is possible to imbed PHP code right in your template files, although not recommended and doubtfully needed since the engine is so customizable.
    • built in caching
    • arbitrary template sources (flat files, databases, etc.)
    I've played around with the demo code a bit and it seems to deliver everything it promises - the "compiling" is particularly impressive (Smarty templates are converted into terrifyingly complex PHP files representing the logic from the Smarty template, which dramatically speed things up by eliminating the need to parse the template each time a page is viewed). I plan to use Smarty as the templating system for my next PHP project - for the moment though I thought I'd post here and see if anyone else has used this and if so what they thought of it?

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never tried it, but it sounds awesome! I normally rely on the really basic stuff (like writing a function for each component of the page) but this has its obvious drawbacks. Will definitely look into this one.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    never used a template system...much slower then just putting it in your php files...
    PHP-Webservices - Profesional Hosting and Programming of sites.

  4. #4
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would love to use it for my weblog script so that users actually use a template instead of php functions, but I'm wondering: is it possible to include PHP code right into the templates ? And I mean any PHP code...
    I'm scared that Smarty would parse it too

  5. #5
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think Smarty can handle PHP code in templates (you use a special delimiter to indicate that what follows is PHP code to be executed), but the whole idae of templates is you keep them seperate from your PHP as much as possible and Smarty's built in templating language should have enough power to save you from having to put PHP in your templates anyway (it suports if-thens, loops and various other conditional statements).
    ...much slower then just putting it in your php files...
    Here I have to disagree - that's true for small scripts but in my experience larger projects can actually be speeeded up by using a template system - for example, the following code displays a list of all of the users for my news script:
    Code:
    		// show list of all users on the site
    		$result = ss_query("SELECT * FROM ssusers WHERE user_deleted != 'yes' ORDER BY user_nick");
    		$output = '';
    		while ($row = mysql_fetch_array($result))
    		{
    			// If current user is an admin, show which other users are admins
    			if (($user->is_admin()) && ($row['user_admin'] == 'yes'))
    			{
    				$admin = ss_template('users_listuseradmin.tmpl');
    			} else {
    				$admin = '';
    			}
    			$output .= ss_template('users_listuser.tmpl', array(
    				'%userid%' => $row['user_id'],
    				'%nick%' => $row['user_nick'],
    				'%email%' => $row['user_email'],
    				'%desc%' => $row['user_desc'],
    				'%admin%' => $admin
    			));
    		}
    		if ($user->is_admin())
    		{
    			$admin_options = ss_template('users_adminoptions.tmpl', array('%self%' => $self));
    		} else {
    			$admin_options = '';
    		}
    		$html = ss_template('users_userlist.tmpl', array(
    			'%html%' => $output,
    			'%admin_options%' => $admin_options
    		));
    		ss_output('Site Staff', $html);
    		exit;
    Writing that code was greatly accelerated by the template system as I didn't have to keep breaking into and out of PHP with the <?php ?> tags, and it also freed me from worrying about the HTML (I could knock up a very basic template during development, then easily go back later and make it look pretty for the site without even having to open that script file in my text editor). TFC Central (my main website) uses this template system throughout and as a result I can completely change the "look" of virtually any part of the site just by altering a template file - much less work than searching through a huge PHP script and modifying embedded HTML.

  6. #6
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can write a whole script without going out of <?php...
    just use the print function...
    PHP-Webservices - Profesional Hosting and Programming of sites.

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not only is that a real pain in the backside, having to escape quotes and all the rest of it and writing EXTRA code like all the echo(""); or print() etc but it STILL embeds the HTML in your page. That means if you want to edit the look of something you have to trawl through lines and lines of PHP looking for your HTML tags.

    Skunks suggestion simply means you specify the name of the template you want and "include" it. So if you want to edit it, you just open that page up.

    It is SO much simpler, and I would assume a lot less resource hungry too.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  8. #8
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we should ask freddy to come up with an apache benchmark to see what's fastest
    PHP-Webservices - Profesional Hosting and Programming of sites.

  9. #9
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes Smarty is a winner!

    I've been using Smarty for awhile now with no problems. Before choosing Smarty I went through all other options at the time but no luck in getting to work in my coding style. One of my favorite features about Smarty is the template if/else constucts to include optional parts of a template. In stead of creating several template files, one for guests, one for registered users and one for admins, just create one template with all these features controlled by the if/else construct. Great.

    Is Smarty's faster them the other templating systems? I don't know, but the caching features work like a charm for me.




    Cheers.

  10. #10
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by =X=
    we should ask freddy to come up with an apache benchmark to see what's fastest
    Aaah, I thought you were talking aout speed of development rather than speed of execution. Smarty should compare very favourably in speed of execution as it "compiles" templates into raw PHP-plus-HTML before they are used, meaning that theoretically a Smarty template should provide the same performance as a normal PHP and HTML combined page (in practise there may be differences caused by the fact that the Smarty system still needs to include the template file, but I wouldn't imagine these differences would be noticable on anything but the most high traffic sites).

  11. #11
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually I don't really see the need for a template system?

    the only use I see is for open source projects like your news script, so users can easily change the design...but smarty seems WAY too complicated for the average computer user to install...(if he already can without the help of the server admin)

    most html in my own scripts are forms and tables (besides the include header and footer) and I just use CSS to change design of those...so it's all inside the php files in my scripts...think that's even faster to code then using those (to my sight complicated) template systems...
    Never really used one though - looked into them but never understood why that would be better then just simply putting html in php file - so can't really say what's fastest...
    PHP-Webservices - Profesional Hosting and Programming of sites.

  12. #12
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by =X=
    you can write a whole script without going out of <?php...
    just use the print function...
    freddy did provided benchmarks a while ago in another thread, and going in and out of <?php is faster than printing everything, not to mention the annoyance of escaping characters.

    Even if printing the whole thing is faster, you have to think about the extendability of the script. What if the you or the client wants the layout redesign a few months down the track? You could either open DW or EditPlus and edit the template and then it's done, or you could open your dozen PHP files, going through the code and changing all the little bits of HTML scattered around the place. I think the clean and maintainable code out weighs the little bit of speed difference, and will save loads of time when you need to change something later on.
    Last edited by Robo; Feb 5, 2002 at 15:43.

  13. #13
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Robo said. Template systems really come into their own when you start designing PHP applications as three tiered systems (where you have seperate storage, logic and presentation layers) - there's some good stuff on Zend about that at the moment. Sure for a small site it may seem like overkill but seperating logic from presentation has a huge array of advantages (like the ability for a non-programming web designer to collaborate with a PHP coder) which far outweighs any performance differences that a template system might introduce.

    Zend article on three tiered application structure: http://www.zend.com/zend/tut/tutsweatpart1.php

    As the article itself says, this kind of approach is "intended for PHP developers with a need to develop applications with goals of long-term flexibility and ease of maintenance".

    One quick example - lets say three months down the line you decide that you want your site to use XHTML rather than normal HTML. If you have a template system you will have a a few template files where you make the changes, letting you ignore the rest of the code. If you have your HTML mixed with your PHP you're facing a mammoth task sifting through every PHP file in your site trying to dig out the non-XHTML-compliant HTML tags.

  14. #14
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    Sure for a small site it may seem like overkill but seperating logic from presentation has a huge array of advantages...
    However, a small site might one day turn in to a big site, so in effect, using a templating system early on, would make expanding the site at a later stage easier.

    That also depends on how you define "small" for a site. As, when a "small" site grows larger different navigation schemes and such like would have to take effect, and you would probably be re-designing your site, and the logic behind it anyway.

    But yeah, a site without templating to me is just silly!
    Oh no! the coots are eating my nodes!

  15. #15
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Trust me, if you are doing teamwork with a designer, a template system will make things MUCH easier.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  16. #16
    SitePoint Zealot
    Join Date
    Aug 2001
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really wish I had understood the "templating system" more before I made my last too sites. One of which is due for a major overhaul. I would love to be able to just edit HTML for the redesign, as opposed to digging through PHP/HTML constructed pages and includes.

    In regard to this type of setup (templates) has anyone here ever worked with or heard of the methodolgy, Fusebox? (fusebox.org)

    Its seems really complicated at first, but i'm starting to understand how it works....

    If there is one thing i have learned in building two sites that are fully database-driven, is that the seperation of content from presentation is essential. If i had properly done so in teh past, i could easy change the design in probably half the time.

    ~out.
    __________________________
    http://www.michaelsica.com

  17. #17
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm... one thing i dont like about smarty is that, it requires all the templates to be re-compiled if any changes are pain... could turn out to be a pain...

    also, the comments on this link have a totally different story to say about speed...
    http://freshmeat.net/projects/smarty

    any other recommendations out there??
    i dunno...

  18. #18
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was under the impression that Smarty only recompiles the templates that have been altered - twhich would make sense as re-compilation is controlled by checking the "last modified" date on the compiled file and the template file to see if changes have been made since the template was last compiled.

    The freshmeat comments are interesting but don''t seem to be particularly damning - I'll have a dig around on the Smarty mailing list and see if performance is realyl a problem but to be honest 80 pages per second (more with the Zend cache) sounds pretty good to me

  19. #19
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just did some reading around and it seems that Smarty only recompiles templates when necessary, but recompiles cache files whenever a change has been made to any of the templates or config files. Cache files are Smarty's ultra smart caching system where it can execute a template and save the output HTML in the cache, allowing it to essentially serve static pages instead of dynamically generating them if your site does not change often (obviously this is an optional feature).

  20. #20
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    I was under the impression that Smarty only recompiles the templates that have been altered
    Actually that can be configured. Usually Smarty only recompiles on an altered template, but this can be changed to recompiled every time the template is run. This can be useful when debugging.

    The speed issue hasn't been a problem with my sites. But I don't have 50000 visitors an hour either.





    Cheers.

  21. #21
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey
    i thought you have to manually recompile the templates?? if its automated... i dont care

    im gonna do some digging to... anyone know how much load it puts etc?? im pretty happy with phplib... but it yet lacks that something in it...


    on another note, does anyone know of any good smarty docs? the docs on there site are kinda confusing.. and the one zend is mixed and matched with pear... isnt there any document dedicated only to smarty
    i dunno...

  22. #22
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by kunal
    hey
    i thought you have to manually recompile the templates?? if its automated... i dont care



    on another note, does anyone know of any good smarty docs? the docs on there site are kinda confusing.. and the one zend is mixed and matched with pear... isnt there any document dedicated only to smarty
    Per default, Smarty checks if the template in question has been modified, and if so, recompiles it. This can be turned off if you want.

    About documentation, I find the PDF doc on the smarty site to be pretty good.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  23. #23
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey.. i was write.. you have to manually recompile your templates for them to work

    from the docs of smarty @ http://www.phpinsider.com/php/code/Smarty/docs/x80.htm
    [qoute]
    ...
    If you change a template, you must recompile from the command line again.
    ...
    [/quote]


    Also, anyone know abt smarty's perfomance if caching is turned off? the true perfomance?
    i dunno...

  24. #24
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The best way to get started is to install Smarty and have a play around with the demo - read the code, read the templates and try and figure out what's going on. The QUICKSTART file is helpful as well. The documentation is excellent but a tiny bit unfriendly for new users, however once you've played around with the demo/QUICKSTART enough to understand the basic principles behind the system the docs start to make a lot more sense.

  25. #25
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Skunk
    The best way to get started is to install Smarty and have a play around with the demo - read the code, read the templates and try and figure out what's going on. The QUICKSTART file is helpful as well. The documentation is excellent but a tiny bit unfriendly for new users, however once you've played around with the demo/QUICKSTART enough to understand the basic principles behind the system the docs start to make a lot more sense.
    hehe.. true... im just loading it up... should be interesting...
    i dunno...


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
  •