0

I have a sample code: in .php

STemplate::assign('image', 'test.png');

in .tpl

{literal}
<script type="text/javascript">
var image_src = {$image};
alert(image_src);
</script>
{/literal}

How to fix it?

1
  • You probably want it to be "{$image}"; so that the javascript parser will see test.png as a string and not some javascript statement Commented Oct 14, 2014 at 10:13

2 Answers 2

3

Your { are not working as smarty code, because you are in a literal block. You can break out of it like so:

{literal}
    <script type="text/javascript">
        var image_src = '{/literal}{$image}{literal}';
        alert(image_src);
    </script>
{/literal}

or, as you are not using { in javascript, do this

    <script type="text/javascript">
        var image_src = '{$image}';
        alert(image_src);
    </script>

I've also added some ' I think you need.

Sign up to request clarification or add additional context in comments.

1 Comment

(If you do need { in the javascript, you can use {ldelim} when not using {literal} by the way)
0

You can solve your problem below 3 ways

1.

{literal}
    <script type="text/javascript">
        function myFunc(){
            var image_src = {/literal}{$image}{literal};
            alert(image_src);
        }
    </script>
{/literal}

2.

<script type="text/javascript">
        function myFunc(){ldelim}
            var image_src = {$image};
            alert(image_src);
        {rdelim}
</script>

3.

<script type="text/javascript">
  var myImage = {$image}
  {literal}
        function myFunc(){
            var image_src = myImage;
            alert(image_src);
        }
  {literal}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.