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

assign_template()

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
Unknown Relic
Smarty Rookie


Joined: 28 Apr 2004
Posts: 9

PostPosted: Wed Apr 28, 2004 9:03 pm    Post subject: assign_template() Reply with quote

I'm a long time user of FastTemplate looking at migrating to Smarty for future development, but one thing that FastTemplate has which Smarty seems to be lacking is FastTemplate's Parse() function. This function is very simple in concept - all it does is assign another template to a template variable instead of a value. The functionality is possible to replicate using the following code, but this method adds in an extra step and is less than elegant:

[php:1:ce42c2a646]
$smarty = new Smarty();

$template1 = $smarty->fetch('template1.tpl');
$smarty->assign('template1', $template1);

$template2 = $smarty->fetch('template2.tpl');
$smarty->assign('template2', $template2);

$smarty->display('main.tpl');
[/php:1:ce42c2a646]

The function I'm proposing would be used as follows:

[php:1:ce42c2a646]
$smarty = new Smarty();

$smarty->assign_template('template1', 'template1.tpl');
$smarty->assign_template('template2', 'template2.tpl');

$smarty->display('main.tpl');
[/php:1:ce42c2a646]

I haven't tested it, but I imagine implementing this function would be as easy as adding the following to Smarty.class.php (though I'm not sure if this is the most efficient method performance wise):

[php:1:ce42c2a646]
function assign_template($tpl_var, $resource_name, $cache_id = null, $compile_id = null)
{
$fetched_template = $this->fetch($resource_name, $cache_id, $compile_id);
$this->assign($tpl_var, $fetched_template);
}
[/php:1:ce42c2a646]

Thanks
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Apr 28, 2004 9:53 pm    Post subject: Reply with quote

That's probably because in Smarty, you don't process the template directly in your PHP code. Instead, SMarty provides facilities for you to do this type of processing directly from your templates. This doesn't prevent dynamic inclusions, either. So in your example:
[php:1:33057ddf33]<?php
$smarty = new Smarty();
$smarty->assign('template1', 'template1.tpl');
$smarty->assign('template2', 'template2.tpl');
$smarty->display('main.tpl');
?>[/php:1:33057ddf33]

Code:
I am main.tpl
{include file=$template1}
{include file=$template2}


Easy Peasy. As you can see, Smarty helps create an interface between your calling code and the template it calls.

Note that {include} also has the option of capturing its output to a local template var. Indeed, much of Smarty is geared to provide the exact tools you need for display processing. If you want to use Smarty successfully, don't think about it in the same way as you think about FastTemplate. With FastTemplate, you still intermix your display and so-called application logic. If you keep the same mindset, you will do the same with Smarty, even though Smarty gives you a way out of that mess Smile

Cheers!
Back to top
View user's profile Send private message
Unknown Relic
Smarty Rookie


Joined: 28 Apr 2004
Posts: 9

PostPosted: Thu Apr 29, 2004 5:20 am    Post subject: Reply with quote

Thanks for the quick reply, however placing includes directly in the template in that fashion actually doesn't quite work for my needs, which I don't believe can be too a-typical.

My company develops PHP based applications, and the administrative sections of these applications all shares a specific look, which is captured by a template - let's call it main.tpl. As an extremely rough example, this template might look like the following:

Code:
<html>

<head>
<title>Site Administration</title>
</head>
<body>

<table>
<tr><td colspan="2">{$PRIMARY_CONTROLS}</td></tr>
<tr><td>{$QUICK_HELP}></td><td>{$BODY}</td></tr>
</table>

</body>
</html>


The entire purpose of this template is to provide a standard surround for the administrative functionality, include standard style sheets, etc. and this base template is used by all applications.

The {$PRIMARY_CONTROLS} variable would hold application specific buttons, with each application having one or more templates for these controls, depending on the current context. Determining which of these templates is appropriate to display is business logic, not presentation logic, and thus doesn't truely belong in the template.

The {$BODY} tag is even more open ended, since there are literally dozens if not hundreds of different screens (a good number of which potentially require unique templates) the logic determining which to display definatly doesn't belong inside the template itself.

Having PHP code which would do this business logic determination of which main templates to use also has the benefit (yes benefit) of restricting what aspects of the layout the designer is able to change, thus enforcing consistency. You'd have something like this:

[php:1:83ce1a0a5c]$smarty = new Smarty();

$smarty->assign_template('PRIMARY_CONTROLS', 'cms_controls.tpl');
$smarty->assign_template('BODY', 'cms_site_map.tpl');

$smarty->display('main.tpl');[/php:1:83ce1a0a5c]

The only real difference in this case is that instead of the designer working on main.tpl, they get to go crazy on cms_site_map.tpl.

I suppose the way I use templates there are really two distinct types:

1. Standardized abstract templates which are rarely modified, and are used to provide a unified look/feel. If they do need to be modified however, changing a single template will update all applications.

2. Specific templates which are embedded into the abstract templates, and contain display logic and do all the things templates are normally expected to do. These are the templates that are actually being modified on a day-to-day basis.

Hopefully I explained that well enough, there really is a method to my madness here, and not just the FastTemplate mindset (at least I don't think it is!).

(Sorry, I just re-read your reply and realized that everything in this specific example could be done using your method, but one key difference is that the method you've outlined forces you to include a file, whereas the method I'm suggesting here gives the programmer the flexibility to decide if {$BODY} should be populated by a template, or just by a php variable - say an error message, or in the case of $PRIMARY_CONTROLS, perhaps an empty string.)
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Apr 29, 2004 6:19 am    Post subject: Reply with quote

I do this kind of thing all the time, wrapping administration tools in a themed invironment in my case. Basically you can assign some vars use them like so:

{include file="$theme/$page_tpl"}

The business logic is assigning vars that tell the template engine what templates need to be included (the main content). The presentation is putting all the templates together to create a web page. If you assign presentation markup into the variables themselves, you are basically handling presentation in your PHP logic, defeating the purpose of the template engine. This approach is typically more difficult to manage, that is the very thing that Smarty lets you get away from! You can argue the case where content could come from a template or somewhere else. In that case, you can use a custom resource to fetch it however you wish.
Back to top
View user's profile Send private message Visit poster's website
Unknown Relic
Smarty Rookie


Joined: 28 Apr 2004
Posts: 9

PostPosted: Thu Apr 29, 2004 7:57 am    Post subject: Reply with quote

Hi mohrt,

As a sort of tangental note, I actually got the idea for this function from this post about caching included template files.

If people are using a simple "surround" template as the one which fetch() or display() is called, doesn't this largely negate the benefits of the caching system since included files (to my understanding) aren't automatically cached? While the solution boots gave is simple and effective, it again seems like it could be made more elegant, and wouldn't an assign_template() type function inherently handle the caching issue?

Maybe I'm just not seeing the drawbacks? As you say, it ties your php code to display logic, but by dynamically specifying which templates to include, isn't your method doing that anyways? On top of this, the include method creates additional complexity if you're going to handle the case where you want the template to be able to either handle having a template assigned, or simply accept a standard variable assignment.

The assign_template function has two positives that I can see: the ability to easily handle the above mentioned case (which in my experience is quite common when working with generic templates), as well as automatically handling the caching issue.

The only "negative" I can see is that you're assigning the presentation markup to the variables directly - as you said - but isn't this essentially semantics? In one case you're specifying which template to load, and letting the include handle it, in the other you're specifying the template to load, and inserting it directly. In both cases the same content is being displayed from the same template in the same place, and both are reliant on php code to function properly. Even the code is virtually identical:

[php:1:beaa57eda7]// Assigning to {include file=$template1}
$smarty->assign('template1', 'template1.tpl');

// Assigning to {$template1}
$smarty->assign_template('template1', 'template1.tpl');[/php:1:beaa57eda7]

On top of this, if we use boots' solution to the caching issue, that just further requires us to be aware on the php side of things of what templates are being used.

Not trying to sound like a broken record here, just trying to understand and further explore the actual benefits vs drawbacks of the two methods. Feel free to tell me to shut up at any time. Very Happy
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Apr 29, 2004 8:55 am    Post subject: Reply with quote

I dont think boot's suggestion in
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=2369
actually works.

templates arent't cached at all. template's *output* is cached. if you display index.tpl with caching enabled, then index.tpl's output is cached. if index.tpl includes alice.tpl, bob.tpl and chuck.tpl all the output of these is also cached.

I don't think
$smarty->assign('foo', $smarty->fetch('foo.tpl')); --- {$foo}

is any worth or better than
$smarty->assign('foo', 'foo.tpl')); --- {include file=$foo}

@Unknown Relic
if you really feel you need assign_template() extend class Smarty in your application and add the method you suggested to your extended Smarty.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


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

PostPosted: Thu Apr 29, 2004 2:06 pm    Post subject: Reply with quote

You can certainly piece together the templates however you wish, but assigning output of a template as a variable to another template is atypical in the way Smarty is generally used. To me, {$template1} isn't very intuitive. I don't know what that content is or where it comes from. OTOH, {include file="template1.tpl"} is immediately understandable.

You can just do your assigning like so:

$smarty->assign('template1', $smarty->fetch('template1.tpl'));

Or like messju said, extend the class to wrap the above into your own assign_template method.
Back to top
View user's profile Send private message Visit poster's website
Unknown Relic
Smarty Rookie


Joined: 28 Apr 2004
Posts: 9

PostPosted: Thu Apr 29, 2004 5:21 pm    Post subject: Reply with quote

Thanks for the feedback.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Apr 29, 2004 9:46 pm    Post subject: Reply with quote

Hey messju, I think my example that you linked does work, if you follow what it is doing and play along with what Smarty does. Much like the way clipcache worked, as long as the outer template regenerates faster than the menu/categories template, you will get a cache effect. Does it help a single page--no, obviously. It is only interesting if you have multiple pages that use menu/categories. Feel free to correct me, of course Smile
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 -> Feature Requests 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