Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Various Smarty design questions: fetch, template etc.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
hughprior
Smarty Rookie


Joined: 07 May 2003
Posts: 10

PostPosted: Wed May 07, 2003 3:50 pm    Post subject: Various Smarty design questions: fetch, template etc. Reply with quote

I'm used to using a template system which has the basic syntax:

$tpl = new Template("my_tpl.htm");

$tpl->assign("INFO", $info);

$html = $tpl->get();


A few questions:
a) Why does Smarty do the assign of the template file on the fetch() rather than, say, on the creation of a Smarty object? What's the logic here?

b) Why the term fetch() rather than something like get()?

I don't mind converting all my code to use the new Smarty system, but I just wondered what the design ideas were that made the difference. Am I the only person to be used to this different 'standard' (ha! I know there are about 10000 different template patterns - if nothing else, at least Smarty should kill off this problem, hooray. By the way, I'm here 'cause I'd rather change to Smarty than fix a bug in the existing system I use)?



Hugh Prior
webmaster of http://www.localpin.com when it's up ;0)

P.S. Is caching to a MySQL table possible/in plan? Sometimes this is more convenient (e.g. shared hosts do not allow file writes, more control over cached items with queries etc).
Back to top
View user's profile Send private message Visit poster's website
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Wed May 07, 2003 4:48 pm    Post subject: Reply with quote

There are differing paradigms.

Quote:
a) Why does Smarty do the assign of the template file on the fetch() rather than, say, on the creation of a Smarty object? What's the logic here?
Well, in this case, you can instantiate the object and re-use it for multiple templates without having to create new template objects. Say your page is made up of 4 templates - instead of making four different template objects, you just make one, and call fetch() as many times as you want. Of course, other template engines probably also have that capability (re-setting the file throughout the execution of the script) - if not, most could be easily modified (since they don't fetch the file till their get() or fetch() function anyway).

Quote:
b) Why the term fetch() rather than something like get()?
Is this really important? You can always make a child class (you should anyway so every time you upgrade smarty, you don't have to go in and modify all the vars) that has a get() method that calls fetch()... and a set() method that calls assign(). This doesn't seem like a template/smarty design question...
Back to top
View user's profile Send private message Send e-mail
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Wed May 07, 2003 5:24 pm    Post subject: Reply with quote

Brian: but variable assigns are kept, then... you can access variables assigned in every following template... I have a special assign function, which stores varnames in an array and after a display() or a fetch(), it disables caching and does a clear_assign() on all variables previously assigned...
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Wed May 07, 2003 10:06 pm    Post subject: Reply with quote

I don't understand your point. If you WANT to use separate objects, you are of course allowed to do that as well...
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 07, 2003 10:37 pm    Post subject: Reply with quote

Hi Hughprior.

I just thought I would add to what was already said:

Quote:
a) Why does Smarty do the assign of the template file on the fetch() rather than, say, on the creation of a Smarty object? What's the logic here?

Smarty assigned data is persistent for the entire Smarty session. This is useful, for example, when there are tempaltes that include other templates. It also allows your application programmer to focus more on the structure of the application data without worrying which templates it will get assigned to. In a way it changes the focus of the data API away from the templates and puts it back on the application.

Quote:
b) Why the term fetch() rather than something like get()?

Why? Because fetch() is better, of course ! Smile One thing about get is that it usually implies a straight operation (simply get the data) whereas when we are getting from a template, we are actually doing a transformation.

It is easy to extend Smarty to other syntaxes or uses. To emulate your syntax, you can use the following wrapper:
Code:
MySmarty extends Smarty
{
    var $tpl = '';
    function MySmarty($tpl='')
    {
    $this->tpl = $tpl;
    }
    function get()
    {
    $this->fetch($this->tpl);
    }
}

$tpl = new MySmarty("my_tpl.htm");
$tpl->assign("INFO", $info);
$html = $tpl->get();
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Wed May 07, 2003 11:36 pm    Post subject: Reply with quote

And one drawback to "generally using one tpl var and setting the tpl at the end" is that you can run into namespacing issues. If you had different TPL objects for each "section" of your site, they could each have their own title, set in whichever order you prefer... wheras, with smarty, if you are reusing tpl var names (such as title), you have to be especially careful about the order in which you assign, fetch, assign, fetch, etc.
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 07, 2003 11:55 pm    Post subject: Reply with quote

Quote:
with smarty, if you are reusing tpl var names (such as title), you have to be especially careful about the order in which you assign, fetch, assign, fetch, etc.


Perhaps. But since Smarty provides very convenient syntax access to arrays (like {$var.el.el}) I just put often used items into arrays.

imagine:
{$PAGE.title}
{$CONTENT.title}

etc.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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