Re: Best way to include a .js file ?
| From: | Bertrand DUNOGIER | Date: | Fri, 01 Feb 2002 08:49:00 +0000 |
| Subject: | Re: Best way to include a .js file ? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2635@lists.php.net to get a copy of this message | ||
Helloooo !
(see at the bottom of this email, there is question :p)
JL> 2) I write in my .tpl file the following line
JL> <script type='text/javascript' src='file.js'></script>
JL> It does not work for me wherever I put the file.js
It should work if you enter a path to your JS file relative to your
document root (like monte said).
JL> Where must I put it ?
See above.
JL> Is there a better way to include css and javascript files ?
I do run scripts using CSS + Javascripts.
for the CSS, i just use
<link rel="stylesheet" href="/css/{$CSS}.css">
and i see the CSS according to some parameters (like the browser, the
weather, the country, the captain's age...)
Concerning the JS part, here is what i'm using:
{if $JAVASCRIPT_FILES ne ''}
{section name=js loop=$JAVASCRIPT_FILES}
<SCRIPT LANGUAGE="JavaScript"
src="{$JAVASCRIPT_FILES[js]}"></script>
{/section}
{/if}
{if $JAVASCRIPT_SOURCES ne ''}
<SCRIPT LANGUAGE="JavaScript">
<!--
{section name=js loop=$JAVASCRIPT_SOURCES}{$JAVASCRIPT_SOURCES[js]}{/section}
-->
</SCRIPT>
{/if}
in my PHP script, i fill in the two arrays, $JAVASCRIPT_FILES and
$JAVASCRIPT_SOURCES. In JAVASCRIPT_FILES, i put relative paths to .js
files. Say i have on my site's document root
/
/index.php
/js/script1.js
/js/script2.js
i may use
$JAVASCRIPT_FILES[] = /js/script1.js;
$JAVASCRIPT_FILES[] = /js/script2.js;
...
$tpl->assign('JAVASCRIPT_FILES', $JAVASCRIPT_FILES);
For the JAVASCRIPT_SCRIPTS part, it is a bit more complex. I use this
section to run dynamic javascripts, parsed by smarty. I have js
templates, with some variables (like multiple checkboxes autocheck,
personnalized messages...). When i want to add a JS Script to my page,
i just assign the variables in the script, then add the template path
to $JAVASCRIPT_SOURCES:
$JAVASCRIPT_SOURCES[] = "jstemplates/script1.js";
...
$JAVASCRIPT_SOURCES[] = "jstemplates/script2.js";
...
Then just before i $tpl->display:
<?php
foreach($JAVASCRIPT_SOURCES as $key => $file) {
$JAVASCRIPT_SOURCES[$key] = $tpl->fetch($file);
}
and i assign $JAVASCRIPT_SOURCES to 'JAVASCRIPT_SOURCES
in the end, my HTML page looks like that:
<SCRIPT LANGUAGE="JavaScript" src="/js/script1.js"></script>
<SCRIPT LANGUAGE="JavaScript" src="/js/script2.js"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Some JS stuff
function foobar() {
// ... JS stuff from script1.tpl
}
function foobar2 {
// ... JS stuff from script2.tpl
}
-->
</SCRIPT>
And... it works perfectly.
By the way, about CSS and Smarty, did any of you ever use smarty to
generate CSS files ? like...
<link rel="stylesheet" href="css.php?browser={$browser}&skin={$skin}">
With intelligent caching, it may be great !
Well well well, it is coding time,
Bertrand DUNOGIER