Utku Dalmaz
Utku Dalmaz

Reputation: 10192

Passing php variable to smarty literal tags

I am trying to pass a php variable to javascript in smarty literal tags.

So far, i tried

{literal}
<script type="text/javascript">
var a = {/literal}{msg}{literal};
  window.onload = function () {
    alert(a);
}
</script>
{/literal}

which doesn't work. Any idea how can i make it work ?

Upvotes: 0

Views: 977

Answers (2)

Tpojka
Tpojka

Reputation: 7111

Just break it where ever you need it. I have next situation that is working for me:

{literal}<script>var myArr = [];{/literal}
{foreach from=$myArr item=v}
    {if $v.title eq 'My Title'}
        {literal}var myArr.push({/literal}{$v.id}{literal});{/literal}
    {/if}
{/foreach}
{literal}</script>{/literal}

Upvotes: 1

Borgtex
Borgtex

Reputation: 3245

The quotes are missing and the $ sign is missing. Also, you don't really need to open {literal} before the script, just before the first '{' in the script, so things can look clearer:

<script type="text/javascript">
var a = '{$msg}';
{literal}
  window.onload = function () {
    alert(a);
}
</script>
{/literal}

Upvotes: 0

Related Questions