0

Can I execute jQuery script syntax like,

$('some-selector').val(${data});

inside my jQuery template?

How do I hook up event handlers for controls inside the jQuery template, where the event handler is in the template, without writing script outside the template?

So, I know this doesn't work:

<script id="modal-edit-mail" type="text/x-jquery-tmpl">
<button id="killswitch">Stop Now</button>
$('#killswitch').click(function(){some function});
</script>

3 Answers 3

1

To your first question:

Yes, you can place Javascript expressions (including function calls) inside your templates using the ${expr} or {{= expr}} syntax. These examples all worked for me using Chrome:

${myTemplateVariable}
${Math.max(Math.min(1, myTemplateVariable), 4)}
${$("#test").html()}
{{= String({"foo": "bla"})}}

But there are some problems:

  • Curly braces are often a problem for jQuery. That's why in the last example I had to use the alternative {{= }} syntax.
  • Some JavaScript expressions are not allowed due to security concerns, e.g. Google Chrome won't let you use eval in your templates.

Additionally, if the code you want to include in your template is rather complex it is a good idea to define a function on the special $item template variable. E.g.

$("#myTemplate").tmpl(myData, {"myComplexFunction": function () {
    // doing complex stuff here
    (...)
    return someBadAssValue;
}}).appendTo("#myTargetElement");

Then you could write the following template code:

${$item.myComplexFunction()}

See the documentation of the second parameter to the tmpl function: http://api.jquery.com/tmpl/


To your second question (how to hook up event handlers):

When I used jQuery I wrote separate functions for updating each part of a page with the current data. To that end, the functions would empty() certain divs in the page and render the new content with the appropriate jQuery templates. In the very same functions I then re-configured all event handlers, i.e. after the new HTML has been inserted into the page.

I know that there is some (badly/not documented) update() method with which jQuery templates can be updated more directly, but I think that would still require you to reconfigure your event handlers (in case new interactive elements had been added by the update).

EDIT: To know which element triggered a certain event, I used HTML-IDs that included the data element's ID, e.g.

<a href="javascript:void" id="remove_person_5">Remove</a>

And this JS code for setting up the event handlers:

$("[id^='remove_person_']").click(handle_remove_person);
Sign up to request clarification or add additional context in comments.

Comments

0

No - it's a template. Templates are meant to separate presentation and functionality.

Comments

0

You can add a separate JS script that adds the functionality you want. In the <head> section of your page, just add a link to it thusly:

<script type="text/javascript" src="killswitch.js"></script>

Then, in killswitch.js, add the selector and click function, like you have above, inside a ready function so that it fires when the whole page finishes loading:

$(document).ready(function() { 
    $('#killswitch').click(function(){some function});
});

2 Comments

maybe I should change my example to clarify the need: I have a modal template that I'm using for edits to an address block. the address block gets read out of my .data so I need a way to set the value of the state select list. normally, I'd have a function with a variable that finds the read only version of the state: var state =('.state').text(); and then the function would set the correct value in the select list: $('#state-field').val(state). so how do I do that in a template?
I don't think that clarified anything. Why can't you use this method?

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.