简体   繁体   English

使用Dancer和Template :: Toolkit的多个布局/包装器

[英]More than one layout/wrapper with Dancer and Template::Toolkit

I followed this recipe to disable Dancer's "layout" and re-enable TT's "WRAPPER": 我按照这个方法禁用了Dancer的“布局”并重新启用了TT的“WRAPPER”:

How to make a page-specific title in Dancer templates? 如何在Dancer模板中制作特定于页面的标题?

which appears to be necessary to change tokens within the layout/wrapper at the route level. 这似乎是在路由级别的布局/包装器中更改令牌所必需的。 Briefly, the linked recipe requires specifying a single layout/wrapper in the config.yml file. 简而言之,链接的配方需要在config.yml文件中指定单个布局/包装器。

The recipe works great when using one layout/wrapper, but how do I go about changing (or disabling) the template layout/wrapper at the route level? 使用一个布局/包装器时,配方工作得很好,但是如何在路由级别更改(或禁用)模板布局/包装?

Since Dancer's native layout is disabled, this does not work: 由于禁用了Dancer的原生布局,因此不起作用:

 template('mytemplate', { ... }, { layout => 'some_other_layout' });   # NO GOOD

Also, I tried changing the config prior to rendering template, but that doesn't appear to work either: 此外,我尝试在渲染模板之前更改配置,但这似乎也不起作用:

 # ALSO NO GOOD
 config->{'engines'}->{'template_toolkit'}->{'WRAPPER'} = 'some_other_layout';
 return template('mytemplate', { ... });

And I also tried using the Dancer set/setting, but also no good: 我也试过使用Dancer设置/设置,但也没有好处:

 # ALSO NO GOOD
 my $engines = setting('engines');
 $engines->{'template_toolkit'}->{'WRAPPER'} = 'some_other_layout';
 set engines => $engines;

Any ideas? 有任何想法吗?

I think I figured it out. 我想我明白了。 Thanks to Yanick and to the link referenced in my other answer (Kludgy Workaround #2) for the inspiration. 感谢Yanick以及我在其他答案(Kludgy Workaround#2)中引用的链接的灵感。 Here's the recipe: 这是食谱:

  1. Create a single wrapper file for use in your entire project, and make it: 创建一个包装器文件以在整个项目中使用,并使其成为:

     <% IF layout %> <% INCLUDE "$layout" %> <% ELSE %> <% content %> <% END %> 
  2. Within your route: 在你的路线内:

     return template('mytemplate', { layout => 'layouts/some_layout.tt', param1 => data1, param2 => data2, ... }); 

And bingo, you're done. 宾果游戏,你已经完成了。 Leave out the layout parameter, and will get no layout at all. 省略布局参数,根本没有布局。

I'm no TT expert, but it seems that once WRAPPER is set for the TemplateToolkit object, it can't be changed. 我不是TT专家,但似乎一旦为TemplateToolkit对象设置了WRAPPER,它就无法更改。

If that's the case, here's a slightly devious workaround. 如果是这种情况,这里有一个稍微狡猾的解决方法。

  1. Set the layout to be, say, 'base.tt'. 将布局设置为“base.tt”。

  2. make that layout to be: 使该布局为:

    print $context->process( $stash->get('wrapper'), { content => $context->process( $stash->get( 'template.name' ) ) }); print $ context-> process($ stash-> get('wrapper'),{content => $ context-> process($ stash-> get('template.name'))});
  3. and then in your route: 然后在你的路线:

    get '/' => sub { template 'index', { wrapper => 'layouts/main.tt' }; get'/'=> sub {template'index',{wrapper =>'layouts / main.tt'}; }; };

Tadah! Tadah!

This being said, if you are using Dancer's native layouts, the tokens you're passing to template should make it to the wrapper code. 这就是说,如果你使用的是Dancer的原生布局,那么你传递给template的标记应该转换为包装代码。

Two incredibly kludgy workarounds (please don't hate me): 两个令人难以置信的kludgy解决方法(请不要讨厌我):

Kludgy Workaround #1 Kludgy解决方案#1

set template => 'simple';
my $html = template('mytemplate', {}, { layout => 'some_other_layout' });
set template => 'template_toolkit';
return $html;

Basically, we temporarily switch out template_toolkit for Dancer's 'simple' template renderer. 基本上,我们暂时为Dancer的“简单”模板渲染器切换出template_toolkit。

Kludgy Workaround #2 Kludgy解决方案#2

Give up and keep the WRAPPER, but add conditionals so that it gets itself out of the way as described in this post: 放弃并保留WRAPPER,但添加条件以使其自行脱离,如本文所述:

How can I override WRAPPER in a Template Toolkit template file? 如何在模板工具包模板文件中覆盖WRAPPER?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM