Skip to content

Instantly share code, notes, and snippets.

@nathanpalmer
Forked from getify/3.html
Created September 12, 2012 14:47
Show Gist options
  • Save nathanpalmer/3707126 to your computer and use it in GitHub Desktop.
Save nathanpalmer/3707126 to your computer and use it in GitHub Desktop.
how "grips" templating handles it, in response to https://gist.github.com/3696453
<script id="template" type="text/x-jquery-tmpl">
<h1>Settings</h1>
{{each(name,setting) settings}}
<h2>${name}</h2>
{{each(index,value) ['low','high']}}
<input type="radio" name="${name}" value="${value}" {{if setting === value}}checked{{/if}}> ${value}
{{/each}}
{{/each}}
</script>
<!--
Here's how jQuery templating engine would handle it (yes I know it's been dead for some time.)
- I named each index/value in the loops for clarify
- otherwise I can reference $index or $value. However that gets difficult with two loops.
- I threw this in a script tag however this isn't how we normally use it (written in a separate file and the compiled down to javascript.)
- Used the if conditional for the checked tag
-->
@getify
Copy link

getify commented Sep 12, 2012

  • I named each index/value in the loops for clarify
    • otherwise I can reference $index or $value. However that gets difficult with two loops.

So, in the inner loop, when you do ${name}, how would you have done that if you hadn't named your outer loop's $index and $value? Is there a special syntax for accessing the parent scope?


If you look at how grips handles things, I think I agree a lot with this approach, except for the inline if construct. I really think conditional logic shouldn't be inline in the template, so grips lets you "pre-compute" your conditionals in the section/partial definition header.

https://gist.github.com/3706912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment