Pybars3(把手)模板渲染AIOHTP。
aio_pybars的Python项目详细描述
所有Pybars
==
快速启动
----
0。安装::
pip安装aio_pybar
或通过setup.py::
>1。配置应用程序:
from aio_pybars import fstemplateloader
loop.run_直到完成(aio_pybars.setup(app,
templates_dir=config['templates_dir',
loader=fstemplateloader))
2。在视图中使用模板:
async def index(request):
context={“var”:“value”}
context)
它将在aiohttp响应的“context”中使用变量呈现“template_name.hbs”模板。
{{>;“边栏”}
helper是可从模板调用的。从语法上看,它看起来与变量相同,但可以获取参数::
<;link rel=“shortcut icon”href=“{asset”favicon.ico“}”>;
将使用“favicon.ico”参数调用“asset”可调用,并将结果放入呈现的模板中。
*使用自己的部分和帮助程序*实现模板加载器的子类:
基本目录:
super()。\uu init(app,基本目录)
def get_partials(self):
“
加载templates目录的partials/子目录中的所有文件。
方法应返回字典{'partial_name':<;compiled template>;,…}
“
partials=super().get_partials()
base_partials=os.path.join(self.app.config['templates_dir','partials')
for name in os.listdir(base_partials):
filename=os.path.splitext(name)[0]
template_source=open(os.path.join(base_partials,名称),“r”,encoding='utf8').read()
template=self.compile.compile(template_source)
partials[filename]=template
return partials
def get_helpers(self):
“
定义自己的一组helpers。
method应该返回字典{helper_name':<;可调用>;,…}
“
helpers=super().get_helpers()
helpers.update({“asset”:_asset})
return helpers
def_asset(options,val,*args,**kwargs:
返回“/static/{}”。format(val)
并将其作为加载程序参数传递给安装程序::
loop.run直到完成(aio_pybar.setup(app,
templates懔dir=config['templates懔dir'],
loader=appfstemplateloader))
如果模板的第一行包含::
{!<;base_template}
所有呈现的模板都将作为变量“body”传递给基本模板。
例如:
base.hbs::
<;!doctype html>;
<;html>;
<;head>;
<;title>;template<;title>;
<;head>;
<;body>;
{{body}
<;body>;
测试。hbs::
{{!<;base}
hello,{{name}.
文档类型HTML>;
<;html>;
<;head>;
<;title>;模板<;title>;
<;head>;
<;body>;
hello,roma
<;body>;
==
快速启动
----
0。安装::
pip安装aio_pybar
或通过setup.py::
>1。配置应用程序:
from aio_pybars import fstemplateloader
loop.run_直到完成(aio_pybars.setup(app,
templates_dir=config['templates_dir',
loader=fstemplateloader))
2。在视图中使用模板:
async def index(request):
context={“var”:“value”}
context)
它将在aiohttp响应的“context”中使用变量呈现“template_name.hbs”模板。
{{>;“边栏”}
helper是可从模板调用的。从语法上看,它看起来与变量相同,但可以获取参数::
<;link rel=“shortcut icon”href=“{asset”favicon.ico“}”>;
将使用“favicon.ico”参数调用“asset”可调用,并将结果放入呈现的模板中。
*使用自己的部分和帮助程序*实现模板加载器的子类:
基本目录:
super()。\uu init(app,基本目录)
def get_partials(self):
“
加载templates目录的partials/子目录中的所有文件。
方法应返回字典{'partial_name':<;compiled template>;,…}
“
partials=super().get_partials()
base_partials=os.path.join(self.app.config['templates_dir','partials')
for name in os.listdir(base_partials):
filename=os.path.splitext(name)[0]
template_source=open(os.path.join(base_partials,名称),“r”,encoding='utf8').read()
template=self.compile.compile(template_source)
partials[filename]=template
return partials
def get_helpers(self):
“
定义自己的一组helpers。
method应该返回字典{helper_name':<;可调用>;,…}
“
helpers=super().get_helpers()
helpers.update({“asset”:_asset})
return helpers
def_asset(options,val,*args,**kwargs:
返回“/static/{}”。format(val)
并将其作为加载程序参数传递给安装程序::
loop.run直到完成(aio_pybar.setup(app,
templates懔dir=config['templates懔dir'],
loader=appfstemplateloader))
如果模板的第一行包含::
{!<;base_template}
所有呈现的模板都将作为变量“body”传递给基本模板。
例如:
base.hbs::
<;!doctype html>;
<;html>;
<;head>;
<;title>;template<;title>;
<;head>;
<;body>;
{{body}
<;body>;
测试。hbs::
{{!<;base}
hello,{{name}.
文档类型HTML>;
<;html>;
<;head>;
<;title>;模板<;title>;
<;head>;
<;body>;
hello,roma
<;body>;