|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
Posted: Tue Apr 22, 2003 7:53 pm Post subject: get array from PHP-script into included header.tpl |
|
|
Hi!
From a PHP-Script I can assign an array only to one template. But my problem is, I would like to assign an array to a header.tpl which is included into my "main-template" where I assign other variables to.
I've got something like this
file.php: Code: | <?
// pass the results to the template
$tpl->assign('something', $something);
// load the template
$tpl->display('file.tpl')
?> |
file.tpl: Code: | {include file="header.tpl" title=abc}
... |
header.tpl: Code: | <html>
<head>
....
???(how can I get the array to build my navigation) |
What I want to do is to assign an array which contains the data to build the navigation. But to get this array is little bit difficult, because of checking rights...
This array which I have in file.php should be assigned to header.tpl.
Or is there an easier way to include header.tpl? Because I have the same header.tpl for all templates, is it possible to add this header.tpl automatically without needing include in every file.tpl?
It is also a problem because my templates are not stored in one directory, so I use different "default template paths", but the header.tpl is allways in the same directory, which is _not_ the default-dir while executing the script. How can I solve this?
Thank you very much!
regards
Andreas
Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Apr 22, 2003 8:39 pm Post subject: |
|
|
any variables that are visible inside your file.tpl are also visible in your header.tpl .
just use {$something} inside your header.tpl it should be definately visible there.
the only thing with the scope is: you cannot *change* $something inside your header.tpl in the hope, these changes will be accessible in file.tpl afterwards.
but you don't want to change these values anyway. you just want to display.
for the header.tpl at a fixed location and dynamic template_dirs, you can use an absolute path:
$smarty->assign('header_tpl', 'file:'.[path_to_header.tpl]);
and then do {include file=$header_tpl} where needed.
(you can also use a config-var of course, but you may want to build $header_tpl dynamically relative to other vars in your php (SMARTY_DIR or dirname(__FILE__) comes to mind), then a template-var is better than a config-var)
as a side note: if you use different template_dirs, ensure you use a distinct compile_dir for each template_dir, or you may get clashes.
HTH
messju |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Apr 22, 2003 9:11 pm Post subject: |
|
|
the point is:
it is fully legal to have many subdirectories in your templates_dir and use these as display("$foo/index.tpl"); with changing $foo.
it is also fully legal to use full-paths and one singe compile-dir.
but what causes trouble is:
$smarty->template_dir = $a;
$smarty->display('index.tpl');
[... another page, another user ....]
$smarty->template_dir = $b;
$smarty->display('index.tpl'); // another index.tpl intended
with just one compile_dir
smarty thinks index.tpl is already compiled (since it is in compile_dir), but it is the wrong one. this is where it breaks.
there are loads of workarounds (some of them may even lead to cleaner application design than switching around with $smarty->template_dir,
but that depends on the application, of course):
- using stuff like {include file="$skin_dir/index.tpl"}
- using resources ( http://smarty.php.net/manual/en/templates.from.elsewhere.php )
- using different compile_ids
there may be even more and having to use different template_dirs is not a common problem to me (ymmv) so i don't know if fixing this would be worth the effort.
greetings
messju |
|
Back to top |
|
|
|
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
|