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 to variable? Handle a set of templ. as one?
Goto page 1, 2  Next
 
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
TimJim
Smarty Rookie


Joined: 22 Apr 2003
Posts: 14
Location: Germany

PostPosted: Tue Apr 22, 2003 1:36 pm    Post subject: Assign template to variable? Handle a set of templ. as one? Reply with quote

My site uses a set of diferent templates, that I'd put together with my old template system. For example I got a main.tpl in which are two variables $content and $buttons. To this variables I assigned other templates (but always diferents), which can be static or dynamic.

Is it possible in some way to assign to a variable a full template and handle this as if it is part of the main template, so that i can use all the functions of Smarty?

Or can I do this with the include function? But how can I change the name of the included file dynamically? But how I understood the include function, there is is only the possibilty to assign variables to the included file, without all the other functionality of Smarty.

Thx for helping.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Apr 22, 2003 2:13 pm    Post subject: Reply with quote

you can do {include file=$template_filename} with $template being an assigned variable.

this is always much better than {eval var=$template_sourcecode}. with {include} you have templates compiled just once. with eval the sourcecode hase to be
compiled on every call. the performance difference is *huge*.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TimJim
Smarty Rookie


Joined: 22 Apr 2003
Posts: 14
Location: Germany

PostPosted: Tue Apr 22, 2003 4:53 pm    Post subject: Reply with quote

And with the included template I can do all the stuff like in the main template? Functions, sections, variables, ...?

btw: I just saw, that you come from Germany, too. Greetz from Hamburg... Smile
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Apr 22, 2003 4:59 pm    Post subject: Reply with quote

yes of course.

{assign var=foo value="foo.tpl"}{include file=$foo}
is semantically equivalent to
{include file="foo.tpl"}

greetings to hamburg, that's really not far away Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TimJim
Smarty Rookie


Joined: 22 Apr 2003
Posts: 14
Location: Germany

PostPosted: Fri Apr 25, 2003 5:42 pm    Post subject: Reply with quote

Does it matter in which order I assign the variables? For example if I assign a variable of the included template before I assign the variable which file I will include?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Apr 25, 2003 5:53 pm    Post subject: Reply with quote

yes, the order matters. the variable gets accessible *after* the assign.

so
{$foo}{assign var=foo value="HELO"}{$foo}
will show "HELO" just once and not twice (the first {$foo} will be empty).

normally you shouldn't use such assigns in the template, it was just for the example. you should assign the variable in you php-script before you display the template:

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

and in the template do:
{include file=$foo}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TimJim
Smarty Rookie


Joined: 22 Apr 2003
Posts: 14
Location: Germany

PostPosted: Fri Apr 25, 2003 6:52 pm    Post subject: Reply with quote

I refer me to the include template.

For example:

main.tpl:
bla<br>
{include file=$template_name}

additional.tpl:
{$name}

This one works:
index.php:
$smarty = new Smarty;
$smarty->assign('name', 'blubb');
$smarty->assign('template_name', 'additional.tpl');
$smarty->display('index.tpl');

As well as this one:
index.php:
$smarty = new Smarty;
$smarty->assign('template_name', 'additional.tpl');
$smarty->assign('name', 'blubb');
$smarty->display('index.tpl');

Is this a special case, because I'm including a template or why is the order not relevant?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri Apr 25, 2003 7:00 pm    Post subject: Reply with quote

because the template isn't processed until you call display (or fetch).
Back to top
View user's profile Send private message
dilvie
Smarty Rookie


Joined: 25 Apr 2003
Posts: 7

PostPosted: Fri Apr 25, 2003 8:47 pm    Post subject: Reply with quote

messju wrote:
you can do {include file=$template_filename} with $template being an assigned variable.

this is always much better than {eval var=$template_sourcecode}. with {include} you have templates compiled just once. with eval the sourcecode hase to be
compiled on every call. the performance difference is *huge*.


I tried to do just that in order to include several files from a site-wide template directory under windows XP (I'm pre-compiling the HTML and uploading static pages to my website). Unfortunately, I could not get that to work. I always got an error -- I think it has to do with the windows path format: C:\documents and settings\user\site\templates -- anyway, I couldn't get it to work. Is there a better way to do that?
_________________
MJS Webmaster Community
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Apr 25, 2003 9:14 pm    Post subject: Reply with quote

on
http://smarty.php.net/manual/en/language.function.include.php

it reads:
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}

did you do that?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dilvie
Smarty Rookie


Joined: 25 Apr 2003
Posts: 7

PostPosted: Tue Apr 29, 2003 10:27 am    Post subject: Reply with quote

messju wrote:
on
http://smarty.php.net/manual/en/language.function.include.php

it reads:
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}

did you do that?


No. THANK YOU for pointing that out to me! =) That will help a lot.
_________________
MJS Webmaster Community
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Apr 29, 2003 2:45 pm    Post subject: Reply with quote

I've comtemplated making smarty ignore one-character resource names. That way c:foo/bar.tpl will work as expected. You just have to name your resources with two characters or more. Any thoughts on that?
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue Apr 29, 2003 3:18 pm    Post subject: Reply with quote

Even simplier way, Monte:
Why don't you "hardcode" C..Z as resource locators if the current OS is Windows?

if method Smarty::_get_php_resource():
Code:

 [snip]
 } else if ($resource_type != 'file') {
    $readable = true;
    $template_source = null;
    if (!isset($this->_plugins['resource'][$resource_type]) && $current_os == "win" && (strtolower($resource_type) >= "a") && (strtolower($resource_type) <= "z") {
      if(file_exists($resource_type.":".$resource_name) && is_readable($resource_type.":".$resource_name)) {
        $readable = true;
        $resource_name = $resource_type.":".$resource_name;
    }
    else {
      $resource_func = $this->_plugins['resource'][$resource_type][0][0];
      $readable = $resource_func($resource_name, $template_source, $this);
    }
 }
 [snip]


This way C .. Z will be recognized as valid resource locator on Windows machines. But a user can even register his own "C" resource locator which is prefered over the other one.
This shouldn't break BC but should make life easier for Windows users.

(Notice: $current_os == "win" in the above script is pseudo code Wink )
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Apr 29, 2003 3:33 pm    Post subject: Reply with quote

Hmm, I wouldn't want to treat C: as a resource type, I'd want it to be part of the filepath (which it is). Also, treating C: different on windows and other OS would only cause confusion. What if someone on linux created a resource type called "C" and someone on windows tried to use it?

The cleanest method would be to ignore one-character resource types and treat them as part of the filepath. If someone tries to register a one-character resource then throw an error.

Monte
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue Apr 29, 2003 3:45 pm    Post subject: Reply with quote

Quote:
What if someone on linux created a resource type called "C" and someone on windows tried to use it?


Then the custom resource type "C" would be used and not the drive "C". Otherwise you will break BC. This is why I would prefer first checking for isset($this->_plugins['resource'][$resource_type])
I am not using one character resource types but I am sure some people will because it's less typing Wink
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
Goto page 1, 2  Next
Page 1 of 2

 
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