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

发布于 2024-09-16 08:43:54 字数 185 浏览 6 评论 0原文

我必须根据用户所处的状态显示不同的医疗表单。还有一个许多状态共享的默认表单。这些医疗表格都是用模板工具包编写的,并且包含在更大的模板中。状态可用作标准化形式的变量。

我需要选择特定于州的模板(如果存在),否则回退到默认值。我最好怎样做呢?

INCLUDE_PATH 已用于控制网站样式之间的切换。

I have to display different medical forms according to which state the user is in. There is also a default form that many of the states share. These medical forms are all written in Template Toolkit and they are included in larger templates. The state is available as a variable in a normalized form.

I need to select the state-specific template, if it exists, otherwise fall back to the default. How would I best go about doing this?

INCLUDE_PATH is already being used to control switching between site styles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-09-23 08:43:54

像这样的东西应该可以完成这项工作:

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";

When running 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

Something like this should do the job:

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";

When running 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
幸福%小乖 2024-09-23 08:43:54

我刚刚在具有继承的Perl模板中回答了类似的问题

使用https://template-toolkit.org/docs/manual/Views.html,此构造支持继承。

I just answered a similar question in Perl templates with inheritance

Use https://template-toolkit.org/docs/manual/Views.html, this construct supports inheritance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文