Search code examples
javascriptphpsmarty

Smarty variable inside javascript


I am really starting with Smarty and I do not understand this fact:

if I put the next code inside my template index.tpl

<script type="text/javascript">
  function toAlert() {
    alert('{$text}' );
  }
</script>

I can access to the function toAlert and show the content of Smarty variable {{$text}}, but if I put this code into a js file lije javascript.js and I try to access it by putting into de template the link:

I cannot access to the function as well.

Can anyone tell me why or help wher can I find this specific info? thank you!!


Solution

  • Smarty 2 requires escaping of the "{" and "}" characters, you can use {ldelim} and {rdelim} to escape them individually or wrap entire blocks of text with {literal}{/literal}. It's usually cleaner to use {ldelim} and {rdelim} when there is embedded smarty tags, so example:

    <script type="text/javascript">
      function toAlert() {ldelim}
        alert('{$text}');
      {rdelim}
    </script>
    

    Smarty 3 conveniently ignores "{" and "}" characters surrounded by white space, so your javascript example would work as-is.