New issue 87 by j...@johnmckinzie.com: Add ability to set variables in
template
http://code.google.com/p/ctemplate/issues/detail?id=87
It would be nice to be able to set variable for a template in the template.
The use case would be for a set of web pages that use a common
header/footer. I would like to be able to set the scripts need for the
individual page in the template, and have those accessible to the included
header template.
// foo.tpl
{{! SCRIPTS[] = "main.js" }}
{{! SCRIPTS[] = "foo.js" }}
{{>HEADER}}
<p>Foo template</p>
{{>FOOTER}}
The header template could then use the SCRIPTS variable in a section. It
just doesn't feel right setting the necessary scripts in my C++ code.
The style of ctemplate is to do this stuff in your code. So your template
would be
outline.tpl:
header stuff
{{>BODY}}
footer stuff
Your C++ would be:
FillCommonDictionary(dict);
... add my scripts to dict ..
TemplateDictionary body = dict->AddIncludeSection(ko_BODY);
I know it's possible. My point is for things such as adding script tags,
those really should belong in the view/template and not the C++ code. The
C++ code should know nothing about what CSS/JS files are needed in the HTML
template. From an MVC perspective, the controller should only be providing
data, not determining what additional tools the view needs to interact with
the user. Below is a link that shows how scripts can be set in the view
using CakePHP.
http://book.cakephp.org/1.3/view/1589/script (2nd paragraph)
Why don't you put the script tags in foo.tpl itself? Is there a need to put
them in header?
You can do this for scripts, however for CSS, the standard (HTML4/XHTML)
says the <style> tag may only be in the head element. This is also true in
HTML5 if you do not use the "scoped" attribute.
Isn't the recommendation for CSS (and JS) to have a single (minimized) file
anyway?
Comment #6 on issue 87 by olafv...@gmail.com: Add ability to set
variables in template
http://code.google.com/p/ctemplate/issues/detail?id=87
(No comment was entered for this change.)
You would probably minify JS and CSS that's common to all pages, but I
think in most cases you would still be page/module specific JS/CSS separate.
Why would you do that?