如何在 Catalyst 应用程序的 Template Tookit 模板中定义常量?

发布于 2024-07-23 02:50:36 字数 535 浏览 2 评论 0原文

我想在我的 TT 模板中使用常量。 在 HTML::Mason (我之前选择的模板引擎)中,我可以

<%once>
use MyApp::Constants qw(CONSTANT);
</%once>

这样 做:我可以在模板工具包中执行此操作吗? 正如标题中提到的,这是一个 Catalyst 应用程序,所以我想我可以将常量放入存储中,但是似乎有点尴尬。

--edit

抱歉 - 我应该提到我想使用我自己的常量 - 从 MyApp::Constants 导出,没有重复。

I want to use a constant in my TT template. In HTML::Mason (my previous templating engine of choice) I could do:

<%once>
use MyApp::Constants qw(CONSTANT);
</%once>

How can I do this in Template Toolkit? As mentioned in the title this is a Catalyst app so I was thinking I could put the constants in the stash but that seems a bit awkward.

--edit

Sorry - I should have mentioned I want to use my own constants - exported from MyApp::Constants, without duplication.

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

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

发布评论

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

评论(2

四叶草在未来唯美盛开 2024-07-30 02:50:36

在 TT 配置中,您可以使用 VARIABLES 选项来传递处理时将传递给每个模板的值列表。 使用一些符号表技巧,您可以将所有常量吸出到配置中:

use MyApp::Constants;
use Template;


my $tt;     # template object
{ 
    no strict 'refs';
    $tt = Template->new( { 
        VARIABLES => { map { $_ => &{ 'MyApp::Constants::' . $_ } } 
                       grep { defined &{ 'MyApp::Constants::' . $_ } }
                       keys %MyApp::Constants::
                     }
        }
    )
}

这会查看包 MyApp::Constants 中的所有符号,检查它们是否被定义为子例程(这就是< code>constant.pm 在后台执行),然后使用 map 向 TT 提供它们的 hashref。

In your TT configuration, you can use the VARIABLES option to pass a list of values that will be passed to every template when it's processed. Using some symbol table trickery, you can suck out all your constants into the config:

use MyApp::Constants;
use Template;


my $tt;     # template object
{ 
    no strict 'refs';
    $tt = Template->new( { 
        VARIABLES => { map { $_ => &{ 'MyApp::Constants::' . $_ } } 
                       grep { defined &{ 'MyApp::Constants::' . $_ } }
                       keys %MyApp::Constants::
                     }
        }
    )
}

This looks at all the symbols in the package MyApp::Constants, checks if they are defined as subroutines (this is what constant.pm does under the hood) and then uses map to provide a hashref of them to TT.

不必你懂 2024-07-30 02:50:36

几种可能性。
只需定义一些变量:

[% users = {
     tom   => 'Thomas',
     dick  => 'Richard',
     larry => 'Lawrence',
   }
%]

[% FOREACH u IN users %]
   * [% u.key %] : [% u.value %]
[% END %]

使用全局变量:

[% global.version=1.234 %]

This is Version [% global.version %].

META指令允许简单的
要在 a 中定义的元数据项
模板。 这些在评估时
模板被解析,因此可能
只包含简单的值(例如它是
无法插入其他
变量值转换为 META 变量)。

[% META
   title   = 'The Cat in the Hat'
   author  = 'Dr. Seuss'
   version = 1.23 
%]

正如您在问题正文中已经提到的,还有这样的:
http://template-toolkit.org/docs/manual/Variables.html# section_Compile_Time_Constant_Folding

Several possibilities.
Just define some variables:

[% users = {
     tom   => 'Thomas',
     dick  => 'Richard',
     larry => 'Lawrence',
   }
%]

[% FOREACH u IN users %]
   * [% u.key %] : [% u.value %]
[% END %]

Use the global variable:

[% global.version=1.234 %]

This is Version [% global.version %].

The META directive allows simple
metadata items to be defined within a
template. These are evaluated when the
template is parsed and as such may
only contain simple values (e.g. it's
not possible to interpolate other
variables values into META variables).

[% META
   title   = 'The Cat in the Hat'
   author  = 'Dr. Seuss'
   version = 1.23 
%]

As you already mentioned in the question body, there's also this:
http://template-toolkit.org/docs/manual/Variables.html#section_Compile_Time_Constant_Folding

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