RE: [SMARTY] Smarty variable in JS and CSS files
| From: | Scott Matthewman | Date: | Sat, 16 Nov 2002 11:43:16 +0000 |
| Subject: | RE: [SMARTY] Smarty variable in JS and CSS files | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-6448@lists.php.net to get a copy of this message | ||
> And what to do, if it would be desirable, that JavaScript and
> CSS were in external files, but in them it is necessary to use
> variables Smarty? For example for colors in CSS or paths in
> JavaScript... There Were ideas associate .css and .js files with
> PHP that in them it was possible to process other patterns with help
> Smarty and to return as usual CSS or JS, there are other ideas?
This is delving into territory that's not Smarty specific, but it's good
to talk about it.
Yes, if you need to insert some dynamic data into a CSS or JS file, then
you will need to call a PHP file. Associating standard extensions with a
PHP parser is not a good idea; it means that every stylesheet or script
file gets run through the parser whether or not it needs to be, and that
can increase load on your server unnecessarily. You might as well have
all your CSS and Javascript on your web page.
An alternative is to explicitly call a PHP file, and make sure that your
PHP script issues an appropriate Content-type: header, e.g.:
<script language="javascript"
src="/scripts/dynamicmenu.php"></script>
In dynamicmenu.php:
header("Content-type: text/javascript");
And then generate the rest of your output in your usual way - via Smarty
or other means.
There are still overheads associated with this:
-- your server stats may start to register a call to your JS-generating
PHP script as another page view when they would otherwise exclude files
by their file extension.
-- the whole script/css file will be invalidated and reloaded on each
page view. If possible, split your javascript or CSS up so that you can
call one static file (which can then be cached) and one dynamic file.
Your server will thank you for it :-)
Scott
--
Scott Matthewman
Technical Manager
Gay.com UK <http://uk.gay.com/>
Best Media (Consumer) Web Site
New Media Age Effectiveness Awards 2002
[and running Smarty for all editorial content]