Page 1 of 2

smarty template engine??

Posted: Tue Apr 18, 2006 8:25 am
by pleigh
hi guys, just wanna know what are the advantages and disadvantages of using template engines such as smarty...is it a wise decision to use one?secured?

thanks in advance. :)

Posted: Tue Apr 18, 2006 8:28 am
by feyd
The decision rarely has to do with security, but more of wanting to separate display logic from business logic. Smarty and other template engines can push the separation a bit more such that a programmer generally isn't needed to alter the page design (or needed quite little.)

Posted: Tue Apr 18, 2006 8:34 am
by Chris Corbyn
The way I see it templates offer two things.

They give designers the opportunity to create a number of different designs for the same page. They (more importantly to me) provide a means of keeping a spaghetti of markup and logic out of your source code.

As soon as I started playing around with templates I've never looked back. There's not even any need to use Smarty or a readily available library. The overall concept is simple, and you can writie your own basic/light template system in a short space of time.

Posted: Tue Apr 18, 2006 8:53 am
by Oren
d11wtq wrote:The way I see it templates offer two things.

They give designers the opportunity to create a number of different designs for the same page. They (more importantly to me) provide a means of keeping a spaghetti of markup and logic out of your source code.

As soon as I started playing around with templates I've never looked back. There's not even any need to use Smarty or a readily available library. The overall concept is simple, and you can writie your own basic/light template system in a short space of time.
I'm with d11wtq. I totally agree with you and that's what I've always thought.

Re: smarty template engine??

Posted: Tue Apr 18, 2006 9:37 am
by Roja
pleigh wrote:hi guys, just wanna know what are the advantages and disadvantages of using template engines such as smarty...is it a wise decision to use one?secured?
Advantages:

- Well maintained: Regular updates (including security fixes and optimizations) mean less work for you.
- Feature rich: Having to re-engineer your code or your template engine when you encounter a new need means more work. They've already done that work for you.
- Extremely scalable: Its being used at some of the largest sites in the world, including parts of Yahoo.
- Career: Because it is widely used in the corporate world, it looks good on a resume.
- Secure: An extensive set of features that help reduce the risks that a template engine can add to an app.

Disadvantages:

- Performance: Memory use can be lower (witness Template-Lite), and it may cause performance issues in some specific configurations
- Another language: Its another language to learn, when PHP is already capable of templating.

I use it on virtually all of my projects, so I have a clear preference for it. The benefits of having a well-maintained library that does everything you need, and doing it well enough that it scales from a webpage to Yahoo can't be overemphasized. Its a drop-in solution, that works well, that is secure, and that is extremely well documented. The fact that its opensource and that the dev team takes patches easily is icing on a delicious cake.

I mentioned Template-Lite, and if performance becomes an issue, and security isn't your primary concern, take a look. Its very close to a feature-complete drop-in replacement for Smarty, and uses roughly half the memory. It uses the same syntax and everything, so you can relatively easily switch from one to the other.

Personally, I feel that coding your own solution to a solved problem - especially one that has been solved dozens of times, and to such a degree that it works on the largest sites on the planet - is reinventing the wheel. Its a waste of time that would be better spent on your application itself. Thats the whole point of frameworks, and libraries. Its "the UNIX way" - lots of little things that do a job, and do it well, working together.

Posted: Tue Apr 18, 2006 12:52 pm
by John Cartwright
I duno.. I used to avoid using smarty, but I have been integrating it into some small applications and with caching enabled there was only a tiny performance impact.

Posted: Tue Apr 18, 2006 1:14 pm
by Roja
Jcart wrote:I duno.. I used to avoid using smarty, but I have been integrating it into some small applications and with caching enabled there was only a tiny performance impact.
You can read about my experiences with Template-Lite so far on my blog. I was by far NOT a fan of the idea. However, PanamaJack is right on the money with TL in my experience. Its a drop-in replacement for the items in Smarty I was using, and it uses roughly half the memory. That clearly shows a performance difference from Smarty. (Your mileage may substantially vary in both impact and features supported).

Posted: Tue Apr 18, 2006 4:46 pm
by neophyte
Template or not to template?

My rules:

1. Will the html files "templates" be shared with others in the office who may not know PHP? Then I'd template.

2. If I'm the only guy to touch the files... PHP the perfect template language! For me this about 99% of the projects.

Hope that helps.

Posted: Tue Apr 18, 2006 5:08 pm
by RobertGonzalez
At first I just intermingled PHP code with my markup. Then I used a variant of the phpBB template system (which up until recently worked very good for me). Then, after looking at a few mainstream products (Wordpress being one of them) and their implementation of PHP as a template generator, I set myself on a course to try what they are doing. We'll see how it works. As of now, however, all of my projects are templated just in case a designer/developer other than myself needs access to the markup without the weight of the code.

Posted: Sat Apr 22, 2006 4:59 am
by AKA Panama Jack
Now some people consider PHP to be a templating engine in its own right. :) It can be used as such but I found it to be a pain in the butt overall to try and use PHP in that manner. Using PHP as a templating engine itself basically involved using variable shorthand.

Code: Select all

<?=$var;?>
This is similar to the braces {} some templating engines use for referencing variables. Many templating engines allow you to change the braces to anything you would like. Some people change them to PHP (<? ?>) or ASP (<% %>) tags. They do this so they are highlighted in whatever editor they are using. Personally, I find it a tad confusing and leave them set to braces.

A templating engine is basically used to separate programming logic from presentation logic. Presentation logic is NOT just HTML as it can contain its own form of program logic.

Example:
=======================

Code: Select all

{if $variable == 1}
	Hi there {$name}!
{/if}
One of the great things about using a templating engine is how easy it is to have many different templates but only one php program to generate the data that is used by the different templates. Blog software is one of the many examples of this. Check out the blogs at http://www.aatraders.com. There is only one readblog.php program and it compiles the data to send to the template that the blogger selected. The template engine either compiles the template, uses a precompiled template or a cached template and sends the result to the browser. No need to have a different readblog.php for each display style.

The most popular templating engine is Smarty. Now, Smarty got many things right during its creation but it fell into the trap so many programs fall into anymore. Code Elegance. The latest version of Smarty has some elegant coding but it is overdone and highly inefficient. There was a radical change in the coding practices for Smarty a few versions back and this coding change was definitely not for the better. Unless you think pretty code is for the better while sacrificing resources and speed.

Roja mentioned Template Lite as a replacement for Smarty. I took over that project after the original author discontinued support for it because the Smarty programmers yelled at him for using the Smarty name in his program. They trademarked the name and didn't like someone else calling their template engine Smarty Light. So I took over the project and renamed it Template Lite after making sure no one else was using that name. ;)

Smarty Light (Template Lite) had just a small subset of the Smarty functions when I took it over. In the last two months I have added most of the things it was missing except for resource and security features. BTW, the security features are mainly for restricting access to certain directories in a directory list and are kind of redundant considering how Smarty works in the first place. The great thing is how Template Lite uses about half the resources that Smarty uses and is about twice as fast as Smarty when compiling templates. Smarty had a great idea when it came to using plugins but they didn't take it far enough. They could have made most of the compiler functions as modules that are only loaded when needed similar to their plugins. But their biggest problem is code bloat where they use many lines of code to accomplish one thing where just a few lines would have worked. I have a new version (1.80) that has been in closed testing for the last week that should be released this weekend. I had one hell of a tester step up and test the package on a commercial application and help me weed out all of the differences between Template Lite and Smarty. The new version will be about as close as you can get to Smarty. :)

But all of that is beside the point.

Using a templating engine is a boon to all website designers and programmers. You can create one frontend program package and use it on many different web sites and just change the templates for each site instead of rewriting the program files for each site.

Posted: Sat Apr 22, 2006 1:03 pm
by John Cartwright
Roja wrote:
Jcart wrote:I duno.. I used to avoid using smarty, but I have been integrating it into some small applications and with caching enabled there was only a tiny performance impact.
You can read about my experiences with Template-Lite so far on my blog. I was by far NOT a fan of the idea. However, PanamaJack is right on the money with TL in my experience. Its a drop-in replacement for the items in Smarty I was using, and it uses roughly half the memory. That clearly shows a performance difference from Smarty. (Your mileage may substantially vary in both impact and features supported).
Very cool, let me check it out and thanks for the link.

Posted: Sat Apr 22, 2006 7:05 pm
by RobertGonzalez
OK, So I finally downloaded Template Lite and I have to say, I love it. It is very fast, very easy to use, very intuitive. But I have one question...

Is there going to be a formlized forum for it's use? I know there is one at SourceForge (I posted to the forum there just a little bit ago), but it seems like it doesn't get a whole lot of traffic. I would love to move forward into implementing TL in my projects, I just want to make sure the kinks are worked out (or should I say my kinks with it are worked out :oops: ?).

Posted: Sat Apr 22, 2006 8:58 pm
by AKA Panama Jack
Everah wrote:OK, So I finally downloaded Template Lite and I have to say, I love it. It is very fast, very easy to use, very intuitive. But I have one question...

Is there going to be a formlized forum for it's use? I know there is one at SourceForge (I posted to the forum there just a little bit ago), but it seems like it doesn't get a whole lot of traffic. I would love to move forward into implementing TL in my projects, I just want to make sure the kinks are worked out (or should I say my kinks with it are worked out :oops: ?).
Thanks, you have to give cudos to the original author, Paul Lockaby, who created the execellent core code that everything else I have added has been based upon. He did one hell of an excellent job.

As to a formalized forum well...

I created one specifically for ADOdb Lite thinking people would rather use it instead of the forums on Sourceforge. Absolutely NO ONE other than some link spammers used the forums I provided. Everyone stuck to using the Sourceforge forums. After 6 months I took down the forums I provided. There were only the welcome messages I originally posted when I took it down.

So, I will probably stick with using the Sourceforge forums unless I get enough feedback from people asking for a seperate forum.

So far the package has had very little in the way of problems. The next version adds more support for some missing Smarty features. The version after this next one may have support for different resources. I am working on a new method for using resources that is smaller while using the same syntax.

Posted: Sat Apr 22, 2006 9:28 pm
by RobertGonzalez
The app absolutely rocks. I give props to anyone that helped on this project. I am absolutely loving it with the tests I am doing right now. I can already see where my PHP code is going to get minimized in favor of presentation logic where it belongs, in the presentation. I am totally thrilled. I am just having a little problem with one of the features (which I posted on the SF forum). Other than that little issue, I am overly pleased. :D

Posted: Sat Apr 22, 2006 10:43 pm
by AKA Panama Jack
I posted a reply over there. It looks more like a weird directory permissions problem.

I am going to try and get 1.80 out tonight as it has some nifty changes.
  • Moved gzip variables from global status to public object variables.
    Added const reserved variable {$templatelite.const.[something]}.
    Added TEMPLATE_LITE_DIR constant variable for referencing directory path to class files.
    Added variable list support in {include} function.
    Added debug support for templates included with the {include} function.
    Fixed %section% variable usage in {if} and {math} functions.
    Fixed backticks around variables inside double quotes in a template file not being removed when the variable was being processed.
    Fixed debug console was not displaying include files and indenting.
    Fixed some variables inside quotes not being processed as variables if they are the FIRST item right beside the quote. Example: "$variable" not processed but " $variable" is processed
Plus a few other compatiblity changes.

Image

The above gives an idea of the compile speed differences between Smarty and Template Lite.