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
  • 相关阅读:
    MySql 分页存储过程
    Wireshark图解教程
    Android全局变量使用
    Memcache存储大数据的问题
    论这场云盘大战,以及各网盘的优劣
    [MySQL CPU]线上飙升800%,load达到12的解决过程
    一步一步写算法(之排序二叉树)
    platform_device与platform_driver
    SPOJ 130
    Java实现 蓝桥杯VIP 算法训练 奇偶判断
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2225893.html
Copyright © 2011-2022 走看看