Re: [SMARTY] global vars in included templates
| From: | Jochem Maas | Date: | Tue, 13 Jul 2004 06:36:29 +0000 |
| Subject: | Re: [SMARTY] global vars in included templates | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11579@lists.php.net to get a copy of this message | ||
Mark Rogers wrote:
Martin Kjeldsen writes:the way you write it sound like you should just include and sub-templates should assume its there. but I think I get your problem somewhat. I have another approach: use a subclass of smarty. give is a set of methods that allow you to build up arrays of things you want to add to the page: css files, javascript 'globals', javscript files, meta stuff, (if you use a filename as the key and you can use the value to store the type of media maybe give it some method that set flags (like: output this special stuff) make a pageDisplay() function which you can pass a template and some setup params (I use a PageBase class which maps the most important datastructures in the custom Smarty object.) let the pageDisplay() do this: $this->mySetup() $x = $this->fetch($tplFile); $this->myHeaderSetup(); $this->display($headerTplFile); echo $x; $this->display($footerTplFile); thereby any calls to the custom Smarty objects 'flagging' methods in the main (content/app) template will automatically trigger the correct assignments before the header is output. you've probably already had the problem of what to assign the body onload Handler more than once and from arbritrary places (templates, plugins) e.g. window.onload = new Function( bla bla ). the following, combined with the above should afford some flexibility: function joinFunctions() {Is {assign var="name" value="Bob" global="1"} a good idea? Is {include file="footer.tpl" once="1"} a good idea?I think the idea of being able to adjust variables outside of the template scope would be useful for other purposes, so I'd prefer the former approach rather than the latter which is more limiting (for me anyway). I've generally needed it where any included template can do any number of "things", determined by the variables passed to it, and the higher level template just needs to keep track of how many "things" have been done, because it affects the layout of other elements on the page. However, the disadvantage of the first construct is that it wouldn't allow the same to apply to variables passed from higher up (eg from a $smarty->assign() call). In programming terms what you're trying to do is pass a variable by reference, so maybe that's what would be best at this level too, eg:{assign var=name value="Bob"} {include file="footer.tpl" name=&$name}I suspect that this approach would meet some resistance though. I have also, like Martin, had situations where I simply need to do the equivalent of include_once. For example, an application which needed to have a certain form field on every page, so my master template needed to include it at the end if no sub-template had already included it.
var newfunc = '';
for (var c = 0;c < arguments.length;c++) {
if ( arguments[c] ) {
var func=arguments[c].toString();
newfunc+=func.substr(func.indexOf("{")-1);
}
}
return new Function(newfunc);
}
so now I have a template with the following in it.
{if bla}
...
window.onload = joinFunctions( function() { if (opener) {opener.blockEvents();} }, window.onload);
window.onunload = joinFunctions( function() { if (opener) {opener.unblockEvents();} }, window.onunload);
...
{/if}
thats called by the head bla being yes or something or the basis of a possible call to an appropriate method or assign.
just a thought.
---
theres another mail which mentions the PHP5 dereferenced syntax ala..
Hmmm::Sigh()->crazyTwich()->recieveFunnyLooks();
nice to see some one else giving it a go ;-)
Does anybody know of another way of doing this?I did try a few things once apon a time, but as I recall I ended up working around it. The net result was quite a lot of logic which would have been better in a sub-template being repeated several times in a higher level template, so I'd certainly support a better way of doing things.