首页 > pyramid框架,jinja2和mako模板互换问题

pyramid框架,jinja2和mako模板互换问题

For example, here's an example of using "raw" Mako from within a Pyramid view:

from mako.template import Template
from pyramid.response import Response

def make_view(request):
template = Template(filename='/templates/template.mak')
result = template.render(name=request.params['name'])
response = Response(result)
return response

===============================================================
现在我想把Mako模板换成Pyramid_Jinja2, 使用通用的接口该怎么办?


先用pyramid_jinja2这个绑定,然后pyramid会根据模板的扩展名进行判断。一般直接用render_to_response就可以了。

#/ __init__.py # pseudo code

from pyramid.config import Configurator as C
from pyramid.renderers import render_to_response as rr
from pyramid.views import view_config as v

@v(name='')
def view1(request):
    value = dict(method=request.method)
    if request.method is 'GET':
        return rr('templates/test.mako', value)
    else: return rr('templates/test.jinja2', value)

def main(global_config, **settings):
    config = C(settings=settings)
    config.include('pyramid_mako')
    config.include('pyramid_jinja2')
    config.scan()
    return config.make_wsgi_app()

http://restait.gitcafe.com/html-docs/pyramid/1.5/api/renderers.html#pyramid.renderers.render_to_response

【热门文章】
【热门文章】