加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 我可以在模板中设置模板继承吗? (模板工具包)

发布时间:2020-12-16 06:25:44 所属栏目:大数据 来源:网络整理
导读:我必须根据用户所处的状态显示不同的医疗表格.还有许多州共享的默认表格.这些医疗表格都是用Template Toolkit编写的,它们包含在较大的模板中.状态可作为标准化形式的变量使用. 我需要选择特定于状态的模板(如果存在),否则回退到默认模板.我最好怎么做呢? IN
我必须根据用户所处的状态显示不同的医疗表格.还有许多州共享的默认表格.这些医疗表格都是用Template Toolkit编写的,它们包含在较大的模板中.状态可作为标准化形式的变量使用.

我需要选择特定于状态的模板(如果存在),否则回退到默认模板.我最好怎么做呢?

INCLUDE_PATH已用于控制站点样式之间的切换.

解决方法

这样的事情应该做的工作:

main.tt:

This is a main template [% GET state %]
[% SET iname = state _ ".tt" %]
[% TRY %]
[% INCLUDE "$iname" %]
[% CATCH %]
[% INCLUDE default.tt %]
[% END %]
End of main template

default.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读