Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Are there benefits using Smarty against using PHP directly?

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Mon Jan 13, 2014 5:20 pm    Post subject: Are there benefits using Smarty against using PHP directly? Reply with quote

Dear All,
I'm new to Smarty. I use Smarty at present to insert in different template files text, at each call either English, or Thai, or German. I prepared at present 5 Smarty templates, that work perfectly at the above mentioned 3 languages. So far, so good. Previously, before I started with Smarty I inserted the different languages with PHP directly into my PHP file templates.
I investigated now Smarty and I found in my Host directory 'templates_c' compiled Smarty files for my applications. I found there e.g. the compiled term:

<option value="../slides4/6-Tents.jpg"><?php echo $_smarty_tpl->tpl_vars['text']->value['tents15'];?></option>

This example line of the compiled .tpl file indicats, that Smarty forms the term

<option value="../slides4/6-Tents.jpg">{$text.tents15} </option>

of the referring .tpl file obviously into a php term.
Before I came to Smarty, I inserted at the same spot into my php template the following term:

<option value="slides4/6-Tents.jpg"><?php echo $text[tents15]; ?> </option>

and I achieved the same result in the displayed file at the screen.
This direct PHP term looks much more simple than the Smarty term and it does the same.
My question now:
What is the benefit of using Smarty instead of using PHP directly without Smarty?
With regards
Siegfried
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 539

PostPosted: Mon Jan 13, 2014 8:51 pm    Post subject: Reply with quote

See smarty site:
http://www.smarty.net/about_smarty
http://www.smarty.net/why_use
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4631
Location: Hamburg / Germany

PostPosted: Mon Jan 13, 2014 11:12 pm    Post subject: Reply with quote

Smarty does provide also features not available in PHP.
One example is caching of rendered pages
See http://www.smarty.net/docs/en/caching.tpl
Back to top
View user's profile Send private message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Tue Jan 14, 2014 3:24 am    Post subject: Smarty versa directly used PHP Reply with quote

Dear U. Tews,
Thank you for your reply.
I checked Smarty caching with the following result:
If the caching lifetime is set to a substantial time, the compiled Smarty .tpl file stays for this time in the cache and inside this time frame no new compilation will be performed for a new call of the .tpl file because Smarty reflects to the cached file which is already compiled.
But is this really a benefit in comparison to PHP?
I think no.
Because, if a PHP template is prepared then this template is already from the beginning in the condition comparable to a cached Smarty file. Caching is no time saving compared to genuine PHP because genuine PHP does not require compilation like Smarty does.
Caching saves only time inside Smarty but no time saving is to be recognized if caching is compared against PHP.
Is this finding correct?
With regards
Siegfried
From the at present windy Thailanmd
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7267
Location: Lincoln Nebraska, USA

PostPosted: Tue Jan 14, 2014 2:51 pm    Post subject: Reply with quote

Smarty caching skips the runtime php process for the template(s) involved in a page, and instead uses a static text file containing the output. This is the speed gain by caching, and can be quite beneficial for very large pages. Of course something else could be built directly with PHP to serve the same purpose. Smarty supplies it for you along with templating.

Many other benefits of Smarty are covered in the literature on the website. Namely, there are features that are not simple things to do in plain PHP such as template inheritance, sandboxing templates (separating PHP code from the template for restricted usage), and cleaner template-oriented syntax. There are plenty of pros and cons to going with a template engine depending on your needs, and much of that is also covered in the use cases.
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Tue Jan 14, 2014 3:50 pm    Post subject: Smarty versa PHP Reply with quote

Dear mohrt,
I'm not at all against Smarty. I converted just my 6th template from direct PHP to Smarty.
But I'm a German Telecommunications Enbgineer, responsible for the design and ordering of the technology of the Saudi Arabian fiber optics network nationwide. And because of my vocational background I wont to know every time exactly, what I'm doing and why I do things.
I have another question concerning Smarty. There is a Smarty Directory named templates_c. This directory contains all my compiled templates in alphabetic order.
Additionally there is a Smarty directory cache that contains in alphabetic order exactly the same compiled template files like the Smarty directory templates_c.
For what purpose is this duplication?
My cache lifetime I set to
$smarty->cache_lifetime = 3600;
that means 1 hour as I interpret that.
Why are all my compiled files in the directory templates_c and in the directory cache, too? For what is this redundancy good?
With regards
Siegfried
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7267
Location: Lincoln Nebraska, USA

PostPosted: Tue Jan 14, 2014 5:08 pm    Post subject: Reply with quote

They are not the same type of files. templates_c contains compiled templates. Smarty compiles all templates to php so there is no need to scan the template files on every request. The cache folder contains text files with the *output* of the compiled template files. There may or may not be a 1-to-1 file correlation, unless you have exactly one template file per cached page.

alphabetic is just how your file browser displays the files to you.
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Wed Jan 15, 2014 12:48 am    Post subject: Smarty versa PHP Reply with quote

Dear mohrt,
Thank you for your reply.
Yes, the files in the directories template_c and cache are not identical, as I saw right now. The files at templates_c contain PHP references at the spots where the text shall be inserted like:

<option value="../slidesC/3-xConfR.jpg"><?php echo $_smarty_tpl->tpl_vars['text']->value['conf13'];?></option>

while the corresponding file at cache contains already the inserted text after the PHP run at the server. The corresponding line at cache to the above line at template_c looks:

<option value="../slidesC/3-xConfR.jpg">The conference room is equipped with a stage</option>

With the help of the Smarty Forum I learn every day a little bit more about Smarty, thank you very much.
With regards
Siegfried
Back to top
View user's profile Send private message
Lord Yggdrasill
Smarty Rookie


Joined: 31 Mar 2012
Posts: 20

PostPosted: Sun Feb 02, 2014 2:53 am    Post subject: Reply with quote

Just one reason, PHP is an ugly templating language, embedding php variables directly in HTML with PHP's opening/closing tags is hurtful to the eyes.

/thread
Back to top
View user's profile Send private message
AnrDaemon
Smarty Regular


Joined: 03 Dec 2012
Posts: 48

PostPosted: Sun Jun 22, 2014 11:27 am    Post subject: Reply with quote

Let me put up an example for caching, if it's not readily apparent.
Our web page contains a catalog with all the goods we manufacture or supply, or manufactured, or supplied in the past. Sometimes, it's 4-5 levels deep.
To render a tree-like catalog structure on a page, I have to perform anywhere up to six database calls, each of which is quite stressful.
Overall, this dragging page render times to whole seconds.
I've considered a rewrite of these calls, but the realities of the use of data returned by these calls on diferent pages made this option undesirable.
I chose the other way and wrapped this code into a nice template function, then enabled a six hours caching on it.
However, I didn't see an improvement on my development machine... as I've disabled caching there, both for debugging and comparison reasons.
Once I've got the wrapper code straight, I've moved this same code to the production system, where caching enabled. The results were, to say the least, spectacular.
This is original code without caching:
Code:
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        3    3   0.5      3       4
Processing:  3244 4295 918.2   4103    6173
Waiting:     3239 4275 900.0   4098    6154
Total:       3247 4298 918.1   4106    6176

Percentage of the requests served within a certain time (ms)
  50%   3957
  66%   4726
  75%   4900
  80%   5337
  90%   5552
  95%   6176
  98%   6176
  99%   6176
 100%   6176 (longest request)


This is the same(!) code wrapped into a cached smarty template:
1. Right away after updating the code, the single longest call is the initial render of the page:
Code:
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    4   0.8      3       9
Processing:    56  168 570.6     85    6524
Waiting:       51  160 570.0     79    6518
Total:         59  171 570.5     89    6527
WARNING: The median and mean for the initial connection time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%     89
  66%    106
  75%    131
  80%    156
  90%    255
  95%    284
  98%    335
  99%    479
 100%   6527 (longest request)


2. Consecutive requests after the page has been cached.
Code:
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        3    8 110.7      4    2994
Processing:    57  101  49.2     88     712
Waiting:       52   94  45.6     82     695
Total:         61  110 120.7     92    3082

Percentage of the requests served within a certain time (ms)
  50%     91
  66%    103
  75%    114
  80%    124
  90%    160
  95%    196
  98%    227
  99%    321
 100%   3082 (longest request)


If anyone still have doubts about caching - ask your questions...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP