2

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?

5 Answers 5

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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 = '}}';

Comments

2

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.

6 Comments

This is what I'm trying to do: {literal} <script type="text/javascript"> myjsVar = '2'; if (myjsVar == '2') { {include file='inc.html'} } </script> {/literal}
Smarty doesn't parse the content of {literal} blocks. You have to close the {literal} block before {include} and open a new one after it. Like this: {literal} <script type="text/javascript"> myjsVar = '2'; if (myjsVar == '2') { {/literal}{include file='inc.html'}{literal} } </script> {/literal}
It's ignoring the condition. if you change myjsVar to anything else you get the same result for some reason
Smarty code runs on the server and generates the HTML and Javascript code. The generated Javascript code runs on the visitor's browser. Don't confuse them.
Issue I'm having is that I have the variables on JS and what to create a JS condition to include different files depending on the variable value
|
0

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> 

Comments

0

If you want to assign smarty variable to javascript

{literal}
<script>

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

</script>
{/literal}

1 Comment

If you try: myjsVar = '1'; if (myjsVar == '2') { .... I'm still getting the include

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.