我已经将Template :: Toolkit用于我的最后几个Catalyst项目,并且有一个我喜欢使用的设置,可以清晰地分离我的模板.现在我希望使用Text :: Xslate,但是我无法弄清楚我是否可以进行相同的设置.以下是我通常用于Template :: Toolkit的内容.
__PACKAGE__->config({
...
WRAPPER => 'site/wrapper',
...
});
Run Code Online (Sandbox Code Playgroud)
包装纸
[% content WRAPPER site/html + site/layout %]
Run Code Online (Sandbox Code Playgroud)
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>[% template.title or site.title %]</title>
<style type="text/css">
</style>
</head>
<body>
[% content %]
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
布局
<div id="header">[% PROCESS site/header %]</div>
<div id="content">
[% content %]
</div>
<div id="footer">[% PROCESS site/footer %]</div>`
Run Code Online (Sandbox Code Playgroud)
然后页眉和页脚都有自己的内容.我喜欢这个解决方案,因为所有内容都是干净利落的,我不会在内容中分解任何div标签,因为必须将开头标记放在标题中并关闭页脚.它看起来像TTerse语法有一些包装器功能,但我不确定是否允许我重新创建我通常做的.我也发现这个答案说你可以在理论上使用包装器,但实际上没有给出任何例子.
在梅森,我可以定义一个过滤器:
<%filter Div($class)>
<div class="<% $class %>">
<% $yield->() %>
</div>
</%filter>
Run Code Online (Sandbox Code Playgroud)
后来我可以用它
% $.Div("row") {{
1 The "$yield->()" method returns everything from here (free text)
% $.Div("col") {{
2 even could have another nested filter, etc...
% }}
% }}
Run Code Online (Sandbox Code Playgroud)
结果
<div class="row">
1 The "$yield->()" method returns everything from here (free text)
<div class="col">
2 even could have another nested filter, etc...
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
例如,该$yield->()方法返回封闭过滤器内部的所有内容.
想要使用Text :: Xslate实现相同的功能,但不知道如何.
我找到的最接近的东西是我可以写的宏块:
: …Run Code Online (Sandbox Code Playgroud) 在Text::Xslatecascade中,是将特定模板(或在其他模板引擎中包装)特定模板到主模板的好方法。还可以将include公共块放入模板中。
我想将include其分为主模板中的特定模板cascade,但在某种程度上,included 模板具有替换(around关键字)主模板中的某些命名部分的部分。
简化的测试用例
use strict;
use warnings;
use FindBin qw($Bin);
use Text::Xslate;
my $tx = Text::Xslate->new(
path => [ "$Bin/template" ],
);
print $tx->render( 'some.tx' );
Run Code Online (Sandbox Code Playgroud)
: cascade main
: around some -> {
some text 1
: include included;
some text 2
: }
Run Code Online (Sandbox Code Playgroud)
MAIN template
: block main -> {
main DEFAULT text, should replaced with included.tx one
: } # …Run Code Online (Sandbox Code Playgroud)