views:

216

answers:

3

hi folks,

here in index.tpl

here in javas.js

var currentTS = "{literal}{$userid}{/literal}";
alert(currentTS);

but there will be alert {literal}{$userid}{/literal} not the $userid. any idea?

A: 

Include the javascript file in your index.tpl. If you have it outside your template directory you must use the file:/... notation (and use your own path, of cours):

<html>
  <head
  <script type='text/javascript'>
  {include file='file:/home/www/mydomain/public_html/js/javas.js'}
  </script>

if you have it in your template diretory simply:

<html>
  <head
  <script type='text/javascriptä>
  {include file='javas.js'}
  </script>

Now Smarty should parse and compile it.

Moreover, it seems to me that you {literal}{/literal} are the wrong way around. If you are using curly braces in your js file you should start the js with a {literal} tag and "unliteralize" the smarty variables:

{literal}
function test() {
   var name = '{/literal}{$name}{literal}';
   // do something
}
{/literal}
karvonen
A: 

Don't use {literal} You don't need it here.

{literal} forces to display all { as they are and don't parse smarty code. Therefore {$userid} will be displayed, as it is.

There is no point in displaying it at the place you are.

JochenJung
A: 

Smarty only works under php, you can't run it in .js , unless you add .js to php extensions in apache configruations.

On top of that it seems to me that you are trying to access the {$userid} variable from your index.php. That is never gonna happen! unless you include the file server side like karvonen explained. And your {literal} tags are unnecessary you start literal when you are gonna use { and } that are not smarty tags but for javascript, css, etc.. and the only time you see them around smarty tags is the other way around as karvonen explained

here's my suggestion: in your index.tpl right before including the java.js file do this:

<!--index.tpl-->
<script type='text/javascript'>UserID = '{$userid}';</script>
<script type='text/javascript' src='pathto/java.js'></script>



/*java.js*/
var currentTS = UserID;
alert(currentTS);
Neo