admin管理员组

文章数量:1399022

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?

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?

Share Improve this question edited May 4, 2010 at 2:36 ruru asked May 4, 2010 at 2:29 rurururu 1611 gold badge2 silver badges10 bronze badges 1
  • also I forgot to mention. you wanna make sure you put the double quotes "" around it only if you want the string value, if it's a integer which from the variable name sounds like a primay unique key you are better off putting no quotes – Neo Commented Oct 13, 2010 at 18:20
Add a ment  | 

3 Answers 3

Reset to default 5

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);

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 pile 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}

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.

本文标签: smarty assign to javascript externalStack Overflow