zoukankan      html  css  js  c++  java
  • [python]pyramid 学习6 (template)

    直接调用模板

    from pyramid.renderers import render_to_response

    def sample_view(request):
    return render_to_response('templates/foo.pt',
    {'foo':1, 'bar':2},
    request=request)

     

    Response输出

    from pyramid.renderers import render
    from pyramid.response import Response

    def sample_view(request):
    result = render('mypackage:templates/foo.pt',
    {'foo':1, 'bar':2},
    request=request)
    response = Response(result)
    return response


    view_config中定义模板

    from pyramid.view import view_config

    @view_config(renderer='templates/foo.pt')
    def my_view(request):
    return {'foo':1, 'bar':2}

     

    ZPT宏的运用(chameleon)

    from pyramid.renderers import get_renderer
    from pyramid.view import view_config

    @view_config(renderer='templates/mytemplate.pt')
    def my_view(request):
    main = get_renderer('templates/master.pt').implementation()
    return {'main':main}

    templates/master.pt

    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:tal
    ="http://xml.zope.org/namespaces/tal"
    xmlns:metal
    ="http://xml.zope.org/namespaces/metal">
    <span metal:define-macro="hello">
    <h1>
    Hello <span metal:define-slot="name">Fred</span>!
    </h1>
    </span>
    </html>

    templates/mytemplate.pt

    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:tal
    ="http://xml.zope.org/namespaces/tal"
    xmlns:metal
    ="http://xml.zope.org/namespaces/metal">
    <span metal:use-macro="main.macros['hello']">
    <span metal:fill-slot="name">Chris</span>
    </span>
    </html>

     

    使用Chameleon的txt模板

    from pyramid.view import view_config

    @view_config(renderer='templates/mytemplate.txt')
    def my_view(request):
    return {'name':'world'}

    templates/mytemplate.txt

    Hello, ${name}!

    模板错误调试
    命令行设置

    PYRAMID_DEBUG_TEMPLATES=1 bin/paster serve myproject.ini

    ini文件中定义

    pyramid.debug_templates = true

    设置mako模板目录

    mako.directories = mypackage:templates

    使用Mako模板

    from pyramid.view import view_config

    @view_config(renderer='foo.mak')
    def my_view(request):
    return {'project':'my project'}

    foo.mak

    <html>
    <head>
    <title>${project} Application</title>
    </head>
    <body>
    <h1 class="title">Welcome to <code>${project}</code>, an
    application generated by the <a
    href="http://docs.pylonsproject.org/projects/pyramid/current/"
    >pyramid</a> web application framework.</h1>
    </body>
    </html>

    Mako Template

    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

    自动加载模板(如果模板有修改不用重启应用)
    命令行

    PYRAMID_RELOAD_TEMPLATES=1 bin/paster serve myproject.ini

    .ini配置文件

    [app:main]
    pyramid.reload_templates = true
  • 相关阅读:
    iis6|iis7|配置URLRewriter|64位操作系统下|.net2.0|.net4.0|配置URLRewriter|Web.config配置详情
    如何用Fiddler对Android应用进行抓包
    asp.net写验证码
    Linq to sql 语法方法示例
    asp.net正则表达式学习例子
    js 根据年月获取当月有多少天_js获取农历日期_及Js其它常用有用函数
    Sql Server 相关错误问题及解决方法
    Javascript 添加自定义静态方法属性JS清除左右空格
    asp.net 文件批量移动重命名
    Memcached监听多个端口_同一台Windows机器中启动多个Memcached服务
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2225893.html
Copyright © 2011-2022 走看看