Disable or change {{}} delimiters in web2py templates?
I'm implementing a web 开发者_开发百科app using web2py and jQuery. I want to use the jquery template plugin but the plugin uses the same notation for templates as web2py; {{ jQuery code }}
and collides with web2py templates.
Is there any way I can disable web2py templates or escape the {{
and }}
parts?
You can disable web2py templating my simply haing the controller functions return a string instead. You may also want to consider this option: in jquery-tmpl.js you can replace
/{{(\/?)(\w+|.)(?:\((.*?)\))?(?: (.*?))?}}/g
with
/{%(\/?)(\w+|.)(?:\((.*?)\))?(?: (.*?))?%}/g
and use {%...%}
in place of {{...}}
so no more conflict with web2py syntax. Similarly we have add an option to web2py to switch syntax there. If this is critical bring it up on the web2py mailing list.
Anyway, I just implemented arbitrary delimiters in web2py trunk. Now you can do in a controller:
def render(filename,**variables):
context = globals()
context.update(variables)
from gluon.template import render
return render(filename=os.path.join(request.folder,'views',filename),
path=os.path.join(request.folder,'views'),
context=context,delimiters=('{%','%}'))
def index():
return render('default/index.html',message='hello world')
and in default/index.html:
{%=message%}
This is very new (5 mins ago) so give it a try and let me know if it works for you. Please follow up on our mailing list.
I also had to replace
/\${([^}]*)}/g, "{{= $1}}
with
/\${([^}]*)}/g, "{%= $1%}
but after that it works fine. Thank you!
精彩评论