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
  • 相关阅读:
    mac搭建mamp环境
    iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
    iOS开发UI篇—UITabBarController简单介绍
    OS开发UI篇—ios应用数据存储方式(归档)
    iOS开发UI篇—ios应用数据存储方式(偏好设置)
    IOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
    iOS开发UI篇—控制器的View的创建
    iOS开发UI篇—UIWindow简单介绍
    iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
    IOS开发UI篇—导航控制器属性和基本使用
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2225893.html
Copyright © 2011-2022 走看看