Can you use two HTML templates like Handlebars and Jinja

I am trying to write an application in javascript with the ember.js library which relies heavily on the Handlebars template system. However, I use FLASK, which also uses the jinja template system.

Can I use both template visualization tools at the same time? Or do I need to use one over the other. Anyone who has experience using both flasks and ember.js knows which one could potentially be easier to replace with another? (Maybe steering is much easier to replace Jinja or vice versa).

+7
source share
4 answers

Note that these two template engines are in different places. Jinja2 will work on the server side, Handlebars will work on the client side. You could use both without interference if you need to.

But with that said, there really is no need to use server-side templates if you have a rich client infrastructure such as ember.js. In your situation, the Flask server will most likely have routes that serve the data through ajax requests back to the ember.js client, so the client is really the best option for rendering templates.

+7
source

You can mark sections of code as {% raw %} to tell jinja2 to ignore it. Wrap the handlebars.js template in raw tags, for example:

 {% raw %} <script id="foo-template" type="text/x-handlebars-template"> <p>{{foo}} - {{bar}}</p> </script> {% endraw %} 
+3
source

As @Miguel said, you do not need Jinja2, if you use ember.js, I realized that you do not want to display these templates, just return flask.send_file ("your html file here") instead of returning flask.render_template ('your html file here '). See docs for more details.

+2
source

While I fundamentally agree with the way @Miguel and @Ali, several companies I worked with are mixing the RESTful API model with server-side HTML. [ NOTE: This should not be the case when using Ember, but I work with Flask / Jinja2 and Backbone in my current client code base.]

I really found a solution using Pybars , based on some reading from the Khan Academy Style Guide :

 @app.template_filter("handlebars") def handlebars_filter(context, filepath): source = open( filepath, "r").read().decode('utf-8') template = pybars.Compiler().compile( source ) return Markup( u"".join( template( context ))) 
+1
source

All Articles