Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for local templates #77

Closed
rubiii opened this issue Feb 13, 2011 · 1 comment
Closed

support for local templates #77

rubiii opened this issue Feb 13, 2011 · 1 comment

Comments

@rubiii
Copy link

rubiii commented Feb 13, 2011

hey aaron,

i'd love to use sammy with jammit which supports packaging the templates instead of loading them via http. since sammy does not (yet?) support local templates, i hacked together a small workaround which extends the render method, loads the template and returns an object, tricking sammy to use it as a jquery element.

app: https://github.com/rubiii/sam/blob/master/public/javascripts/application.js

workaround: https://github.com/rubiii/sam/blob/master/public/javascripts/sammy-jst.js

loading the templates upfront might be especially interesting for small applications with a few templates. i don't think it would require much code to do this and could try to implement a non-hacky version (without introducing a dependency on JST) if you think this is a good idea.

cheers,
daniel

@quirkey
Copy link
Owner

quirkey commented Feb 14, 2011

Hey daniel

I hadnt seen how jammit packages templates until just now and it presents an interesting problem. Sammy does support packaging templates, just in a different (albiet more standard way) of being able to load them from DOM elements or <script> tags on the page. jQuery-tmpl and Mustache particularly work well this way. render() and partial() can take a string (the name of a template) or a jQuery or DOM element. The use case is simple, embed the template in a script tag on the page:

   <script id="name-template" type="text/mustache" data-engine="mustache">
      I'm a template named {{name}}.
    </script>

then in a route/helper, you can do:

     this.partial($('#name-template'), {name: 'NAME'});

I like this approach as it lets all the view logic be embeded with the rest of the view. With JST it looks like JST 'templates' are just functions, similar to the way we define template 'engines' in Sammy. In this way you might be able to use interpolate() directly (not tested):

     var rctx = new RenderContext(this);
     var engine = function(content, data) { return JST.nametemplate(data); }
     rctx.interpolate({name: 'NAME'}, engine).appendTo('#main');

Still ugly but perhaps a little less hacky?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants