Escaping smarty

juro

Joined: 2004-12-12
Posts: 1
Posted: Sun, 2004-12-12 11:45

I want to add a <script> line in wich { tags are used:

<script language=javascript>h1 = document.getElementsByTagName('h1');if(h1.length > 0){h1=escape(h1[0].innerText);}else{h1='';};document.write

etc.

How can I escape the { tags so this code won't call smarty?

Regards, Robin

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Sun, 2004-12-12 17:15

Move to the G2 forum. No smarty in G1. :wink:

Dave

 
baschny
baschny's picture

Joined: 2003-01-04
Posts: 328
Posted: Sun, 2004-12-12 17:31

juro, take a look at the smarty documentation. The function you want is {literal}...{/literal}. Note that you can't use other smarty tags inside this block. If you need that, you have to replace all {'s and }'s with {ldelim} and {rdelim}.

floridave, thanks!

 
CSpotkill

Joined: 2004-12-11
Posts: 113
Posted: Sun, 2004-12-12 21:29

Would { and } work also? They're the HTML-escaped versions of { and } respectively.

lol ... the forums changed it for me. I meant: & #123; and & #125; without the spaces between & and #

I've never tried using them to replace { in a script ... and it's probably faster to just use {literal} ... {/literal} ... but it's the first thing that came to my mind ;)

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Mon, 2004-12-13 04:52

Unfortunately, if you use the html entities (& #123; and & #125;) then the Javascript engine won't parse them as open and close braces and will treat them as syntax errors. Eg:

Error: missing { before function body
Source Code:
      function foo() {

I generally use {literal}. However, you can also use {ldelim} and {rdelim} for open and close braces, which is useful if you want to intersperse Smarty tags inside your Javascript. We do that in a few places in the code.

 
CSpotkill

Joined: 2004-12-11
Posts: 113
Posted: Mon, 2004-12-13 08:07

Figured as much. Thanks for letting me know :)