5

ユーザーがどの州にいるかに応じて、さまざまな医療フォームを表示する必要があります。また、多くの州で共有されるデフォルトのフォームもあります。これらの医療フォームはすべてテンプレート ツールキットで作成され、より大きなテンプレートに含まれています。状態は、正規化された形式の変数として使用できます。

州固有のテンプレートが存在する場合はそれを選択する必要があり、存在しない場合はデフォルトに戻ります。これを行うにはどうすればよいですか?

INCLUDE_PATHは、サイト スタイルの切り替えを制御するために既に使用されています。

4

1 に答える 1

6

このようなものが仕事をするはずです:

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
于 2010-08-27T07:50:13.657 に答える