views:

48

answers:

2

Hello guys,

I need to inline some javascript code into the Smarty template files and these {ldelim} {rdelim} things are killing me. Is there a way to tell smarty to ignore the markup for a block and just output it as is? Something similar to CDATA blocks in the xml?

Just in case: here is how simple javascript looks now:

$(function() {ldelim}
        $( "#slides" ).accessNews({ldelim}
            speed :  "{$speed}",
            slideBy : 1
        {rdelim});    
{rdelim});

creepy heh?

+3  A: 

Re your update: You can use {literal} .... insert content here {/literal} to turn off Smarty variable parsing for entire blocks.

Old answer: I like to change the default delimiter characters to something else for this exact reason. (As far as I know, that can be done for the whole project only though, so you would have to see whether it's for you.)

For example to

<( $variablename )>

(just my personal favourite, use whatever suits you)

The variables to change are the rightDelimiter and leftDelimiter properties. Documentation

$smarty = new Smarty();
$smarty->left_delimiter =  '<(';
$smarty->right_delimiter = ')>';
Pekka
Way nicer than the {literal}{/literal} mess.
Daff
Unfortunately doesn't suite my case, since I already have a lot of legacy template files to work with.
Juriy
@Juriy I see. Hmm, then I don't see a way around this except for excluzding JS blocks using {literal}.
Pekka
What do you mean by {literal} looks like exactly what I need. If i surround the js block with {literal}{/literal} then I will not need to use {rdelim} etc inside?
Juriy
@Juriy exactly; the downside is that if you want to use template variables inside, you'll have to do `{/literal}{$varname}{literal}` ... If you don't need template vars in there, literal is all right.
Pekka
+1  A: 

Using the {literal} tag is useful in this case, but as Pekka said it is very annoying to close and begin tags every time you want to insert a variable. You can change the delimiter characters of Smarty, but that's not so good if you already have lots of templates.

The Smarty team resolved this problem in Smarty 3. No more {literal} tags and the template works fine. Maybe you could try updating it.

Zsolti
Thanks for the hint, I'll certainly try it!
Juriy