View previous topic :: View next topic |
Author |
Message |
TimJim Smarty Rookie
Joined: 22 Apr 2003 Posts: 14 Location: Germany
|
Posted: Tue Apr 22, 2003 1:36 pm Post subject: Assign template to variable? Handle a set of templ. as one? |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Apr 22, 2003 2:13 pm Post subject: |
|
|
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 |
|
TimJim Smarty Rookie
Joined: 22 Apr 2003 Posts: 14 Location: Germany
|
Posted: Tue Apr 22, 2003 4:53 pm Post subject: |
|
|
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... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Apr 22, 2003 4:59 pm Post subject: |
|
|
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 |
|
Back to top |
|
TimJim Smarty Rookie
Joined: 22 Apr 2003 Posts: 14 Location: Germany
|
Posted: Fri Apr 25, 2003 5:42 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 25, 2003 5:53 pm Post subject: |
|
|
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 |
|
TimJim Smarty Rookie
Joined: 22 Apr 2003 Posts: 14 Location: Germany
|
Posted: Fri Apr 25, 2003 6:52 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 25, 2003 7:00 pm Post subject: |
|
|
because the template isn't processed until you call display (or fetch). |
|
Back to top |
|
dilvie Smarty Rookie
Joined: 25 Apr 2003 Posts: 7
|
Posted: Fri Apr 25, 2003 8:47 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
dilvie Smarty Rookie
Joined: 25 Apr 2003 Posts: 7
|
Posted: Tue Apr 29, 2003 10:27 am Post subject: |
|
|
No. THANK YOU for pointing that out to me! =) That will help a lot. _________________ MJS Webmaster Community |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 29, 2003 2:45 pm Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue Apr 29, 2003 3:18 pm Post subject: |
|
|
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 ) |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 29, 2003 3:33 pm Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue Apr 29, 2003 3:45 pm Post subject: |
|
|
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 |
|
Back to top |
|
|