I usually store them on the view; here I'm going to tell you about a really interesting use-case I found for jquery-tmpl
.
I've used jquery-tmpl
on a site that, due to the huge amount of requests, required a technique I call decontextualization
. This technique was implemented just to stay alive during peak hours and it consists purely on the following rules:
- Never touch the server to regenerate the page more than once; unless it's absolutely necesary.
- Use JavaScript to provide a state of who the user is and what privileges he has.
With those two rules in mind you may notice that jquery-tmpl
with the basic amount of logic it provided was simply majestic for the given case. What I did basically is including the jquery-tmpl
templates into the documents that required to be descontextualized. All the templates were provided by the page itself; so I could make a d18n
javascript library which could do the following:
- Query a really fast script about current user, return data as a JSON object.
- Traverse the JSON and include the templates provided on the document on the specified selectors. Let
jquery-tmpl
do the math.
Whenever we needed to do a modification to the "tempalte" we would do it the same way as we would do it if there was no jquery-tmpl
available: ON THE VIEW / PARTIAL.
You would view something like this for a "can user edit post?" template:
<script id="post-edit-button" type="text/x-jquery-tmpl">
{{if user_id == "<%= post.user.id %>" || role == "staff" || $.inArray(user_id, EDITORS) }}
<a href="<%= edit_post_url(post) %>">Edit</a>
{{/if}}
</script>
Hope my experience is useful for you at some degree.