Projects / Smarty PHP template engine

Smarty PHP template engine

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation. The Smarty design was largely driven by these goals: clean separation of presentation from application code; a PHP backend and Smarty template frontend; compliment PHP, not replace it; fast development/deployment for programmers and designers; quick and easy to maintain; a syntax that is easy to understand, with no PHP knowledge required; flexibility for custom development; and security (insulation from PHP).

Tags
Licenses
Implementation
Translations

Recent releases

  •  06 Dec 2010 17:50

    Release Notes: Smarty 3, a completely new code base since Smarty 2, features Template Inheritance, all new compiler/tokenizer, optimization for PHP 5, and more.

    •  14 May 2009 07:45

      Release Notes: This release strips backticks from {math} equations, throws an error when a template exists but is not readable, works around a bug in some versions of PCRE, fixes a function injection security hole, fixes pass expiration time in the cache_handler_fuc call in core.write_cache_file.php, and updates compiler.class.php to allow method chaining for PHP 4 and PHP 5.

      •  20 Oct 2005 11:02

        No changes have been submitted for this release.

        •  23 Mar 2005 17:59

          Release Notes: A vulnerability in the regex_replace modifier has been fixed that allowed PHP code to be executed from a template, even with template security enabled. If you are using template security features, it is highly recommended to upgrade, or at least replace the modifier plugin. A problem with the {strip}{/strip} tag (that was introduced in 2.6.7) has been fixed. Casting objects to arrays in the {foreach} "item" attribute has been addressed.

          •  02 Nov 2004 11:29

            Release Notes: This release is focused on minor fixes. The "core" directory has been renamed to "internals", so be sure to do a clean re-install and clear the compile/cache files if upgrading.

            Recent comments

            07 Sep 2008 02:15 mohrt

            Re: What a bad idea


            > Consider the following Smary “Code”

            >

            > {if $smarty.session.user and (

            > $user_type eq "editor" or

            > $user_type eq "admin" )}

            > <input type=checkbox name=edit

            > value="y"> edit <br>

            > {/if}

            >

            > That looks a lot like a programming

            > language to me, and Smarty is nothing

            > like HTML.

            You didn't read the whole page. That is an example of what NOT to put in the template page (application logic.)

            25 Sep 2007 22:06 di11rod

            Re: What actual benefit do I get?
            I can't believe the negativity people have posted about this clever development package. For starters, it's plain rude for someone to step up and criticize people who put their own time, energy, and money into making something for others to use for free. If you think the engineering behind it is flawed, recommend improvements, fix the code yourself, or just move along to a better project. Show a little sensitivity towards the people who got off their as$es and did something for the community.

            Secondly, I have to back leeds on the performance impact of dual-layered interpretation. Anyone faulting a publishing environment for this is speaking from an academic perspective rather than years-in-the-trenches experience. Believe me. I used to travel around the country auditing web site performance and this ain't something to be worried about. Bigger performance issues can be found by:
            1. Configure memory caching for Apache.
            2. Turn off logging or set it up on a different device (not network-mounted, either).
            3. Tune your DB. Make sure common SQL queries are set up as stored procedures. Make sure indexes are configured properly. Make sure none of your code hits the DB unnecessarily. Nested for-loops making calls to the DB for each row returned in the parent loop is a horrible drag.Oh, well. Smarty looks cool to me.Seth

            19 Sep 2007 12:43 mohrt

            Re: What actual benefit do I get?
            Instead of regurgitating explanations here, read the "Right for me" and "Why use it" links from the Smarty home page. That should tell you plenty, and you can make an informed decision if Smarty is a good choice for your application. It isn't meant to be the be-all end-all template toolkit for PHP, it is just another tool in the toolbox.

            > Hi there.

            >

            > I testet Smarty and had a look at the

            > documentation. Now, however, there

            > remain some questions.

            >

            > The syntax within Smarty templates is

            > similar to the common PHP syntax. I

            > output a variable using the notation

            > {$MyVar} - in a PHP script, I would

            > write <?=$MyVar?>. Then I have

            > modifiers and plugins like

            > "upper" doing tasks that are

            > already covered by native PHP funktions

            > (e/g strtoupper()) - yet the Smarty

            > stuff is less flexible in use (and

            > probably slower since PHP functions are

            > called via the "template engine

            > detour"). All in all, any template

            > designer will have to dig into the

            > Smarty syntax and learn it before they

            > can create working templates.

            >

            > If I got it right, the compilation of

            > templates means that files are

            > translated from the "Smarty

            > language" to PHP, meaning of course

            > that these compiled scripts will be

            > parsed again by the PHP parser on

            > request.

            > Nothing against that - but when the

            > syntax is so similar, why do we need the

            > Smarty templates in the first place?

            > So from my point of view this template

            > engine just looks a little like a big

            > wrapper that does things you could do

            > with plain PHP - and yet it is not much

            > easier to use than PHP itself.

            >

            > With those drawbacks - tpl designer

            > having to learn a "language"

            > similar to real scripting, a lot of

            > overhead code on every request and

            > nothing really you couldn't achieve with

            > plain PHP code just as easily - what do

            > I get out of using Smarty?

            19 Sep 2007 12:36 mohrt

            Re: What a bad idea
            You obviously haven't used Smarty if you think it's sole purpose is an alternative syntax for template designers. That is a very small part of it, and more of a side-effect than a purposed feature.

            > Smarty is quite possibly the worst idea

            > for a template engine I have ever seen.

            > It says in their documentation that it

            > makes it so that web page designers do

            > not have to deal with PHP code syntax,

            > but instead an easy-to-use templating

            > syntax not much different than plain

            > HTML

            > (http://smarty.php.net/rightforme.php).

            > Consider the following Smary “Code”

            >

            > {if $smarty.session.user and (

            > $user_type eq "editor" or

            > $user_type eq "admin" )}

            > <input type=checkbox name=edit

            > value="y"> edit <br>

            > {/if}

            >

            > That looks a lot like a programming

            > language to me, and Smarty is nothing

            > like HTML. It is just another

            > programming language with HTML embedded

            > into it just like PHP. If the designer

            > has to learn how to program anyways why

            > not just use PHP? Now if a PHP

            > programmer ever has to change something

            > in the presentation layer he has to

            > learn a new language. It’s kind of like

            > writing a windows program in C# then

            > using VB for the presentation layer.

            > There really isn’t anything wrong with

            > it but there is no point either, why not

            > just write the whole thing in C#? That

            > would save the trouble of learning 2

            > languages for no reason. Smarty is just

            > a big waste of time and resources.

            >

            20 Jun 2007 22:32 marionconsulting

            Re: design problem with smarty


            > while using php we can show various

            > category name rowwise with more than one

            > column in each row.

            >

            > like: using while loop with

            > mysql_fetch_array()

            > we can loop the result.

            > within the loop if we use

            > mysql_fetch_array()

            > again, then the pointer jumps to

            > next record.

            >

            > I found it impossible using smarty. We

            > cant go to next record within any loop.

            > For this purpose we have to make static

            > pages while it can be made dynamic if

            > written in raw php.

            >

            > if anybody faced this problem and has a

            > solution pls reply.

            Smarty can definitely handle the situation. Pleaes provide code and an example of exactly what you want to do and I'll show you how to do it in Smarty.

            Screenshot

            Project Spotlight

            OpenStack4j

            A Fluent OpenStack client API for Java.

            Screenshot

            Project Spotlight

            TurnKey TWiki Appliance

            A TWiki appliance that is easy to use and lightweight.