Search code examples
javascriptphpjquerysmarty

Smarty inside javascript condition ... possible?


I am trying to have a javascript condition and mix it with smarty code...is this possible?

For example:

<script>

if (myjsVar == '2') {
    {some smarty here}
}

</script>

This is what I'm trying to do:

{literal}
<script type="text/javascript">
  myjsVar = '2';
  if (myjsVar == '2') {
    {include file='inc.html'}
}
</script>
{/literal}

At this moment this code is not including anything ...syntax?


Solution

  • You can use literal that allows a block of data to be taken literally.Eg Javascript or CSS

    {literal} tags allow a block of data to be taken literally. This is typically used around Javascript or stylesheet blocks where {curly braces} would interfere with the template delimiter syntax. Anything within {literal}{/literal} tags is not interpreted, but displayed as-is. If you need template tags embedded in a {literal} block, consider using {ldelim}{rdelim} to escape the individual delimiters instead.

    {literal}
    <script type="text/javascript">
    if (myjsVar == '2') {
        {include file='inc.html'}
    }
    </script>
    {/literal}
    

    You can also use {ldelim} and {rdelim} that is used for escaping smartys template delimeters

    <script type="text/javascript">
    if (myjsVar == '2') {ldelim}
        {include file='inc.html'}
    {rdelim}
    </script>