pyramid模板语言

关于针对python的html框架Chameleon常用statement

 

声明 描述
tal:define 定义变量
tal:switch 定义一个连接条件
tal:condition 当表达式为真时处理元素 (个人感觉就是if条件)
tal:repeat 重复元素
tal:case 当表达式等于父开关时才处理元素 (个人感觉就是case)
tal:content 替换元素的内容
tal:replace 用动态内容替换元素的内容
tal:attributes 动态的更改或者插入输入的元素
tal:block 个人感觉为宏的开始与结束的标识符

此上原文引自:https://www.cnblogs.com/ysysqsh/p/10601656.html

遍历tal.repeat

<ul>
    <li tal.repeat="name categories>${name}</li>
</ul>

相当于mako模板的

% for name in categories:
    ${name}
% endfor

 拼接url地址

<ul>
    <li tal.repeat="category categories>
        <a href="${request.route_ur('category', id=category.id)">  #category’ 对应__init__.py里的category的url映射
             ${category.name}
       </a>

    </li>
</ul>

 判断tal:condition

<ul tal:condition="id">  # 如果id不为空
    ${id}      # 显示id
</ul>
<p tal:condition="not id">  # 如果id为空
    并没有传入id
</p>

 替换元素内容tal:content

数据库里有<h2></h2>.....如此的html标签的时候,想显示出来html效果的时候加structure

<table>
    <tr>
        <td>简介</td>
        <td tal:content="structure item.description">这里不管写神码都会被替代</td>  # 加上structure就不会被转义,就能显示出来html效果了
    </tr>
</table>

 导入静态资源

request.static_url('项目名称:文件路径')

<script type="text/javascript" src="${request.static_url('myshop:static/js/jquery-1.8.3.min.js')}"></script>  

 使用layout模板引擎

安装layout模板

pip install pyramid_layout

master.py

master.py
pyramid_layout.layout import layout_config  # 应用layout布局引擎

@layout_config(name="master",  # 指定布局名称,哪个模板需要这个布局就可以在后面加layout='master', 不写name属性的话默认应用到所有布局
            template='myshop:templates/layouts/layout.pt'  # 模板路径
            )
class MasterLayout(object):
    def __init__(self, context, request):
        self.context = context  # 该处定义的属性在html要用layout.context展示
        self.request = request

@view_config(match_param=('ctrl=%s' % ctrl, 'action=view'),
                 renderer="item.html", permission='item',
                 layout='master')
    def view(self):
        id = self.request.params.get('id')  # 接收id
        item_info = category.get_item_by_id(id)
        return {'item':item_info,
                'categories': self.category_list}



master.pt  # 基础模板定义一个槽
<div metal:define-slot='content'></div>

some_template.pt
<metal:block use-macro="main_template">
<div metal:fill-slot="content">  # 填充槽 fill-slot
....
</div>
</metal:block>

master.pt # 基础模板定义一个槽

<div metal:define-slot='content'></div>

some_template.pt  继承基础模板的视图模板

<metal:block use-macro="main_template">
<div metal:fill-slot="content"> # 填充槽 fill-slot
....
</div>
</metal:block>

在development.ini中定义layout模板

[app:main]
use = egg:MyShop

pyramid.reload_templates = true
pyramid.debug_authorization = true
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
    pyramid_debugtoolbar
    pyramid_tm
    pyramid_layout

安装包setup.py里加pyramid_layout

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.txt')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()

requires = [
    'pyramid==1.4',
    'SQLAlchemy',
    'transaction',
    'pyramid_tm==1.1',
    'pyramid_debugtoolbar==4.5.2',
    'zope.sqlalchemy==1.1',
    'waitress',
    'pyramid_layout',
    ]
原文地址:https://www.cnblogs.com/yifengs/p/12251500.html