1

I'm trying to include some javascript code inside a template.

The code of my html page :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="jquery-jtemplates.js"></script>
</head>

<body>
    <div id="infos"></div>
    <div id="my_template"></div>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#my_template').setTemplateURL("my_template.html");
            try {
                $('#my_template').processTemplate({'var' : 1});
            }
            catch (e) {
                $('#infos').html('error : '+e);
            }
            $('#my_button').click(function(){
                alert('it works outside');
            });
        });
    </script>

</body>
</html>

and the template

content of the template<br/>
{#if $T.var == 1}

 <script type="text/javascript">
  $(document).ready(function() {

   $('#my_button').click(function(){
    alert('it works inside');
   });

  });
 </script>
 <input type='submit' id='my_button' value='click me' onclick='alert("direct");'/>

{#else}
 not working
{#/if}

produce me an error inside the infos balise

error : SyntaxError: missing } after function body

if I just put alert('it works inside'); inside the script balise (remove all the jquery related code), the page load, the two message "direct" and "it works outside" are showed but not "it works inside" message.

It's suppose to works as said on the doc page

Allow to use JavaScript code in templates

Thank you

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
  • 2
    Start by validating your document, 79 errors! http://validator.w3.org/check. And switch to a more usable doctype, XHTML1 strict is useless unless you are embedding MathML etc. – Sean Kinsey May 26 '10 at 10:07
  • 79 errors but if I change just the doctype, I drop to 1. Not so good the validator :) I still have one error, but I don't understand why – Martin Trigaux May 26 '10 at 10:49
  • 2
    The doctype tells the browser how to handle your document, it's like saying "hi this is an excel file" then feeling it a .zip, you will get unpredictable behavior. You need to put the **correct** doctype. – Nick Craver May 26 '10 at 11:57
  • ok so what's the correct doctype ? where can I find information about it ? thank you – Martin Trigaux May 26 '10 at 14:18

2 Answers2

1

Looks like {} signs are garbled by the template preprocessor. I'm not familiar with jtemplate, but this might be the solution for your problem: jTemplates escape {$

Community
  • 1
  • 1
Tgr
  • 27,442
  • 12
  • 81
  • 118
0

I've found a solution I think.

The template allow me to execute only relly simple JS inside <script> tags but I can either call a function defined in my main page or use an external JS file

content of the template<br/>
{#if $T.var == 1}
    <script type="text/javascript" src="fct_template.js"></script>
    <input type='submit' id='my_button' value='click me'/>
{#else}
    not working
{#/if}

where I can put all the code I want

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58