代码之家  ›  专栏  ›  技术社区  ›  Schwern

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

  •  5
  • Schwern  · 技术社区  · 14 年前

    我必须根据用户所处的状态显示不同的医疗表单。还有一个默认的形式,许多国家共享。这些医疗表格都是在模板工具箱中编写的,它们包含在较大的模板中。状态以规范化形式作为变量提供。

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

    1 回复  |  直到 14 年前
        1
  •  6
  •   Grrrr    14 年前

    像这样的事情应该可以做到:

    主.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、 损益:

    #! /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