문제

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top