Ignore a Smarty Templates Fetch() for one page
I've just been passed an application to work on written in smarty templates so I'm unfamiliar with how the whole thing works.
So my problem is smarty is fetching a template from a file at application level so it affects every page on the site. I need a way of telling a single template to ignore the application level fetch.
So at开发者_JAVA技巧 application level it is echo $smarty->fetch('layout/main.html.tpl'); I just want to ignore that on one template. Can anyone help?
You'd want to add some logic to whatever point in the application is fetching that template. The issue isn't smarty, it's the application. A smarty template has no way of interfering with the php that renders it, nor can it introduce logic within the php script.
You would call $smarty->fetch() from a script, not a template. You can use logic to choose a different template name and fetch whatever template is appropriate, so a single script can easily call any of your templates.
For instance...
$template = 'error.tpl';
if($conditions =='right')
{
$template = 'normal.tpl';
}
echo $smarty->fetch("layout/$template");
Also, note that you can use the display() method rather than echo with fetch():
$smarty->display("layout/$template");
This way you're not storing the template into a variable you're just going to output.
If it's a simple case where one script calls template "A" and another calls template "B"...
//call in template A
$smarty->display("layout/templateA.tpl");
//call in template B
$smarty->display("layout/templateB.tpl");
No need for extra logic in that case.
精彩评论