1
kaotik
smarty inside js file?
  • 2009/12/23 12:24

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


Is there any way for smarty to be compiled inside a js (javascript) file?
The only alternative I can think of is, instead of using a js file, I use a standard html template file and place my javascript code inside that.

..any suggestions would be greatly appreciated.

Thanks

2
ghia
Re: smarty inside js file?
  • 2009/12/23 12:59

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


I don't think there are other options, than to set the JavaScript variables inside the template.
Use the <{literal}> tag to reduce some unwanted interactions between Javascript and Smarty.

3
kaotik
Re: smarty inside js file?
  • 2009/12/23 14:58

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


Thanks. That's the method I'm using.
For anyone else searching for this, here's a mini tutorial.

Variables in javascript work a little diferent from php. Here's some code to exemplify:
//PHP
$a=2;
function 
test (){
echo 
$a;
}
// this will throw back an error of undefined variable
$a=2;
function 
test (){
global 
$a;
echo 
$a;
}
// this will output: 2

So as you can see, in PHP, if you want to use an existing variable inside a function, you must first declare it global.
Now in javascript:
//JAVASCRIPT
var a=2;
function 
test(){
alert (a);
}
//this will popup an alert window with: 2

In javascript, once a variable is declared (outside a function) it exists in all space. In example 2, if you declare a variable inside a function:
function test(){
var 
a=2;
}
alert (a);
//this throws a variable undefined error.

In this scenario, all variables declared inside a function are only local. This applies both to javascript and PHP.
Now back to my original problem.
In XOOPS there are 2 smarty variables you will use a lot: $xoops_url and $xoops_rootpath, since we can't use smarty directly in javascript files, how do we pass these values?
Step 1:
in your XOOPS smarty template:
style>
<
script language="javascript">
var 
xoopsRoot="<{$xoops_rootpath}>";
var 
xoopsUrl="<{$xoops_url}>";
script>
<
link rel="stylesheet" href="<{$xoops_url}>/modules/greenlime/templates/js/newpost.css" type="text/css" media="all" />
<
script type="text/javascript" src="<{$xoops_url}>/modules/greenlime/templates/js/jquery-1.3.2.min.js">script>

Now I can use the javascript variables "xoopsRoot" and "XoopsUrl" inside any javascript file that comes after.
You can do this for any other smarty variable (even your own)
..for a mini tutorial, not to shaby

4
frankblack
Re: smarty inside js file?
  • 2009/12/23 15:11

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Hi kaotik, old jquery genius. Two observations I made.

Best place for jquery.js should be really the upmost top of the theme.html, right under the title if possible, because jquery scripts behave badly, when they are placed before jquery.js. Problem is that addScript and $xoops_module_header get placed at different locations in the structure. So if the site uses jquery a lot, I would not mind to embed it manually in the theme.html instead of assigning it with xoops_module_header or addScript.

Second observation: when Javascript and smarty are mixed in template sometimes (in very small cases) smarty isn't properly translated. So it helps, when you put smarty between backticks. Maybe this information is outdated, but I ran into this problem a while ago.

5
ghia
Re: smarty inside js file?
  • 2009/12/23 15:53

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Also, it should be
<script type="text/javascript">
because
<script language="javascript">
is deprecated.

6
kaotik
Re: smarty inside js file?
  • 2009/12/23 18:25

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


ah yes... I really need to stop copy/paste from older code

Frankblack: I find jquery easier especially after a beer or two kill the brain cells which aren't understanding the code
Regarding obs 1, your absolutly right. There is some discussion on the web regarding where to include javascript files. There are pro's and con's to both including in head or bottom of page.
obs 2; thanks for the info. It's good to know in case I encounter this too.
I tried installing debaser (3.05) but I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE debaser_mimetypes ( mime_id int(11) NOT NULL auto_increment, ' at line 3
I'm going through the sql file to try and pinpoint what it is.

7
frankblack
Re: smarty inside js file?
  • 2009/12/23 20:03

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Did you grab an older version of debaser 3.05 before 2009-12-17? Guess so, there was - shame on me - a bug in the sql. Right after the last player is inserted, there is a comma instead of a semicolon.

Login

Who's Online

390 user(s) are online (291 user(s) are browsing Support Forums)


Members: 0


Guests: 390


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits