Processing ember.js tagged html with python
Asked Answered
A

2

3

I have the following scenario:

  1. We are using web2py in the server side
  2. We are serving some ember.js pages
  3. Currently those ember.js pages are inside an iframe, because ember.js and web2py conflict with template {{ }} marks. That means we can not easily mix web2py templates and ember.js templates.
  4. So I have implemented the helper class solution: class em(DIV)
  5. Now I want to process the original ember-tagged html files, and produce the em-tagged files, integrating ember.js and web2py templating-systems into a cohesive unit.

For that I need to change all instances of {{XXX}} in the ember.js files to {{=em('XXX')}}, including instances which span over several lines. I am thinking of going regex here, but I would like to avoid reinventing the wheel (and having to handle strange corner-cases)

Can you think of a generic method in python of parsing these kind of templates. It is simply a matter of looking for start and end delimiter ({{ and }}), and putting an =em('XXX'), handling newlines, and keeping the format (that is, keep the newlines if there are some).

Note: this is actually not ember.js specific; it could apply to any multi-line delimiter-based templating system.

Arbor answered 26/8, 2012 at 8:11 Comment(0)
U
3

Note, in the trunk version of web2py (which will be released as web2py 2.0 in the next few days), you can now specify custom delimiters for the templates -- so you can change the web2py delimiters so they no longer conflict with the ember.js delimiters. For example, in a model file:

response.delimiters = ['{%', '%}']

Then in your web2py templates, you can do:

{%=P('hello world')%}
<p>{{ember template code}}</p>
{%=P('{{ember template code generated by web2py}}')%}

which will produce:

<p>hello world</p>
<p>{{ember template code}}</p>
<p>{{ember template code generated by web2py}}</p>

Note, response.delimiters is set on each request, so if you don't want to change the web2py delimiters on all pages but only those that include ember code, you can set response.delimiters conditionally (either by setting it in the specific actions that need it, or by checking the requested controller and/or function in a model file). For example, in a model file:

if request.function in ['action1', 'action2', 'action3']:
    response.delimiters = ['{%', '%}']

or in a controller:

def action1():
    response.delimiters = ['{%', '%}']
    [etc.]
Unprejudiced answered 26/8, 2012 at 13:23 Comment(2)
Thanks Anthony. That looks like an interesting approach. The problem that I see with your suggestion in my current application, is that lots of my web2py views are already implemented with the default delimiters. If I want to extend the html files (with extend directive), or anyhow reuse my existing web2py templates, I will get into trouble. But in the same spirit as what you are suggesting, maybe it would be possible to (instead of using the em helper class), to use a different delimiter for ember.js (I do not have ember.js legacy). I do not know if that is configurable, I will investigate.Arbor
This is indeed what I ended up doing. Working fine so far.Arbor
W
0

It may worth trying an approach with keeping all your Ember.js stuff in separate files and do not mix it with with web2py templates. Fortunately, Ember.js easily allows to do it.

Wolfhound answered 26/8, 2012 at 20:28 Comment(4)
Could you please elaborate on this? How would you serve with web2py the ember.js files without getting into trouble because of the conflicting template delimiters? The only way I know to implement this is by serving the ember.js inside an iframe.Arbor
Ember uses Handlebars.js for templating. You can pre-compile your Handlebars templates and add them to web2py pages as a regular javascript files via <script src="" directive. Ember.Handlebars.precompile() function can be used for templates pre-compilation.Wolfhound
I see. But not being able to mix web2py and ember.js can be a problem in the future. I am not able to foresee if I will have special situations where mixing both templating systems will be necessary.Arbor
It's up to you, but I would prefer do not mix them. It's really a different layers of the application. And Ember.js layer can be used outside of the web2py scope eventually.Wolfhound

© 2022 - 2024 — McMap. All rights reserved.