admin管理员组

文章数量:1332395

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?

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?

Share Improve this question edited Feb 2, 2016 at 10:36 Satch3000 asked Feb 2, 2016 at 10:10 Satch3000Satch3000 49.4k90 gold badges224 silver badges349 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

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>

In Smarty 3 templates, the { and } braces will be ignored so long as they are surrounded by whitespace.

You can use following without literal escapement.

<script>
if (myjsVar == '2') {
    {some smarty here}
}
</script>

Following code requires literal escapement.

{literal}
    function bazzy() {alert('foobar!');}
{/literal}

Optionally you can also change Smarty delimiters like so:

<?php

$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';

Smarty version 2 is confused by the curly braces used by Javascript code; it tries to interpret them as open tags. To avoid this issue, the content of the <script> element is usually enclosed in a {literal} Smarty block.

The drawback is that you have to close the {literal} block and open it again if you need to use Smarty functions to generate a fragment of the Javascript block.

In Smarty 2, it works like this:

{assign var="text" value="Hello world!"}
<script>
{literal}
if (myjsVar == '2') {
    alert('{/literal}
    {* Smarty code here *}
    {$text|escape:'javascript'}
    {*
     * End of the Smarty code; start another 'literal' block
     * for the rest of the Javascript code
     *}
{literal}');
}
{/literal}
</script>

The generated text looks like this:

<script>
if (myjsVar == '2') {
    alert('Hello world!');
}
</script>

Smarty 3 doesn't need enclosing the script in a {literal} block as long as the curly braces ({ and }) are surrounded by whitespace characters.

Update:

Alternatively, if the block of Javascript contains just a few braces and a lot of Smarty code you can forget about the {literal} blocks and use {ldelim} and {rdelim} for { and }.

Like this:

{assign var="text" value="Hello world!"}
<script>
if (myjsVar == '2') {ldelim}
    alert('{$text|escape:'javascript'}');
    {include file="inc.html"}
{rdelim}
</script>

It works the same in both Smarty versions 2 and 3.

try below :

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

or

{literal}
<script language="javascript">

function ab()
{

////What you need

}
</script>
{/literal}

<a href="javascript:ab();">Something you need</a> 

If you want to assign smarty variable to javascript

{literal}
<script>

if (myjsVar == '2') {
    {/literal}{include file='inc.html'}{literal}
}

</script>
{/literal}

本文标签: phpSmarty inside javascript conditionpossibleStack Overflow