Stache

Trimmed mustache logic-less templates for Python
Download

Stache Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Linh-Nam Vu
  • Publisher web site:
  • https://github.com/hyperturtle/

Stache Tags


Stache Description

Stache implements everything from Mustache.5 except for lambdas in ~150 lines of code. Plus two new things. {{.}} and {{< blah}}{{/blah}}Why?Because the current Pystache implementation has holes. And because I wanted to learn about python generators. As a result my codebase is considerabley smaller and easier to grok too. Also benchmarking the two with my tests, mine was slightly faster, around 2x to 3x.New Stuff{{.}} Self ReferencerThis is useful if you want to iterate over an array and wrap them.{{#array}}< li >{{.}}< /li >\n{{/array}}with array = will produce:< li >1< /li >< li >2< /li >< li >3< /li >< li >yay< /li >{{< thing}} Pusher/Injector {{/thing}}It renders the inner block and adds it to the data scope.{{< thing}}It takes this. You can put anything in here.{{tags}}, {{#blocks}}{{/blocks}}, etc.{{/thing}}and it populates the global scope with a key of thing. Watch out, it can override existing vars. A convention such as{{< namespace.thing}}{{/namespace.thing}}or similiar will help with collisions. This is helpful if you want to use stache templates for masterpages/inheritance. Lets say you have these templates:master = '{{header}}\n{{footer}}'page = '{{< header}}{{name}}{{/header}}{{< footer}}footer{{/footer}}{{ >master}}'Rendering the page template with {'name': 'Stachio'} will produceStachiofooterYou can also apply the inverted block to supply default blocksmaster = '{{header}}{{^header}}Default Header{{/header}}\n{{footer}}{{^footer}}Default Footer{{/footer}}'Rendering {{< footer}}Stachio{{/footer}}{{ >master}} with {} will produceDefault HeaderStachioInstallpip install stacheTestYou can run python test.py or if you have nosetests:cd stachenosetestsBenchmarkpython test.pyUsage:>>> from Stache import Stache>>> Stache().render("Hello {{who}}!", dict(who="World"))Hello Worldor>>> import Stache>>> Stache.render("Hello {{world}}!", dict(world="Stache!"))Hello Stache!To populate partials:>>> from Stache import Stache>>> stachio.add_template('main', 'a = {{a}};')>>> stachio.add_template('main1', 'b = ;')>>> stachio.add_template('main2', 'stachio')>>> stachio.add_template('woah', '{{>main}} {{>main1}} {{>main2}}')>>> stachio.render_template('woah',dict(a=1, b=))a = 1; b = stachioIf you want to put in dynamic file loading of partials you can override Stache().templates with a dict() like object and override the __get__ method. Once you load up the template, you'll need to call self.add_template(template_name, template)Timeline:I'm wary of lambdas, because I want the templates to be language agnostic. The main reason I liked Mustache in the first place is because of possibility of template reuse.Some future ideas I have is rendering to javascript templates to be used on browser frontend, bypassing the need for a client side script to compile it into javascriptProduct's homepage


Stache Related Software