我必须根据用户所处的状态显示不同的医疗表格。还有许多州共享的默认表格。这些医疗表格都是用模板工具包编写的,它们包含在更大的模板中。状态可用作标准化形式的变量。
我需要选择特定于状态的模板(如果存在),否则回退到默认值。我将如何最好地去做这件事?INCLUDE_PATH
已被用于控制站点样式之间的切换。
最佳答案
这样的事情应该可以完成这项工作:
主要.tt:
This is a main template [% GET state %]
[% SET iname = state _ ".tt" %]
[% TRY %]
[% INCLUDE "$iname" %]
[% CATCH %]
[% INCLUDE default.tt %]
[% END %]
End of main template
默认.tt:
This is default template
s1.tt:
This is template for state s1.
t.pl:
#! /usr/bin/perl
use 5.006;
use strict;
use warnings;
use Template;
my $tt = Template->new();
$tt->process("main.tt", { state => "s1" })
|| die $tt->error, "\n";
print "---------\n";
$tt->process("main.tt", { state => "unknown" })
|| die $tt->error, "\n";
运行时
t.pl
:This is a main template s1
This is template for state s1.
End of main template
---------
This is a main template unknown
This is default template
End of main template
关于perl - 我可以在模板中设置模板继承吗? (模板工具包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3581639/