template-toolkit

How do I get a random number in template toolkit?

守給你的承諾、 提交于 2020-01-03 10:49:21
问题 I want to get a random number using template toolkit. It doesn't have to be particularly random. How do I do it? 回答1: From this post at Slashcode: [slash@yaz slash]$ perl -MSlash::Test -leDisplay [% digits = [ 0 .. 9 ]; anumber = digits.rand _ digits.rand _ digits.rand; anumber; %] ^D 769 回答2: Hmm, you might have issues if you don't have (or cannot import) Slash::Test. From a "vanilla" installation of TT, you can simply use the Math plugin: USE Math; GET Math.rand; # outputs a random number

Perl - Template Alloy and Template toolkit array ref

我们两清 提交于 2019-12-24 01:36:10
问题 I using template alloy and Template toolkit, in TT I want to detect array reference like i do with perl: for my $parents ( @{$value} ){ if (ref($parents) ne 'ARRAY'){ push @all_urls_names, $parents; } } This is my code in tt: use warnings; use v5.20; #strict is already set in the version use Template; my $t = Template->new( INCLUDE_PATH => ['.'], ); my @menus = ( ["parent", [qw(child1 child12 child13 child14) ] ], ); my $mvs = {# my variables menus => \@menus }; $t->process("index.tt", $mvs,

How to make a page-specific title in Dancer templates?

一笑奈何 提交于 2019-12-24 00:59:30
问题 I have a standard Perl Dancer app, using Template::Toolkit as rendering engine, with two routes: get '/' => sub { template 'index'; }; get '/foo' => sub { template 'foo'; }; My views/templates/main.tt contains the line: <title><%= title %></title> I want the value of title var be "My Site" on page '/', and "Foo - My Site" on page '/foo'. I know I can put these values in the controller file, like this: template 'index', { title => 'My Site' }; but I want to specify them in the corresponding

How do I handle errors in methods chains in Perl?

空扰寡人 提交于 2019-12-20 10:41:40
问题 What is the best way to deal with exceptions threw in a method chaining in Perl? I want to assign a value of 0 or undef if any of the methods chained throw an exception Code sample: my $x = $obj->get_obj->get_other_obj->get_another_obj->do_something; What the best way to do it? Do I have to wrap in a try/catch/finally statement everytime? The context I want to apply this is: Im working in web development using Catalyst and DBIC and I do a lot of chained resultsets and if some of this

xslate/Catalyst::View::Xslate wrapper

好久不见. 提交于 2019-12-12 17:10:04
问题 I have a Catalyst application that uses TT for views: https://metacpan.org/module/Catalyst::View::TT and I would like to try out https://metacpan.org/module/Catalyst::View::Xslate We use the WRAPPER directive with Template Toolkit and I am curious if it is at all possible to reuse the wrapper somehow with xslate or would I have to break them into headers and footers? 回答1: You can in theory, by using TTerse syntax. According to the manual, enabling this allows you to use a lot of TT compatible

AJAX: how to get progress feedback in web apps, and to avoid timeouts on long requests?

爱⌒轻易说出口 提交于 2019-12-12 08:58:24
问题 This is a general design question about how to make a web application that will receive a large amount of uploaded data, process it, and return a result, all without the dreaded spinning beach-ball for 5 minutes or a possible HTTP timeout. Here's the requirements: make a web form where you can upload a CSV file containing a list of URLs when the user clicks "submit", the server fetches the file, and checks each URL to see if its alive, and what the title tag of the page is. the result is a

Can Perl's Template Toolkit warn on undefined values?

最后都变了- 提交于 2019-12-12 07:47:04
问题 Is there a way to make Perl's Template display warnings for all undefined values that I attempt to use the GET directive on (via [% %] ) during Template::process ? The default behavior is to ignore and move on. I'd like to warn only in the case of undefined values, if possible, and to log messages to STDERR. 回答1: Yes. If you pass the DEBUG option to Template->new , TT will warn you about undefined values. See the docs here: http://search.cpan.org/dist/Template-Toolkit/lib/Template/Manual

How to detect array type in Template toolkit? [closed]

寵の児 提交于 2019-12-11 01:55:17
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I need to detect some variable for accessory to array type in Template toolkit. Are there best practices? 回答1: Your data should be validated by the controller BEFORE it's passed to the template. There should be no mystery what format your data is in. That said, the most useful method to check this

I get extra CR using TT (perl template toolkit)

一世执手 提交于 2019-12-10 14:55:58
问题 I use perl v5.10 (on windows 7) + TT v2.22. When I use TT, for each source line, I get in the produced html an extra CR : Source text (windows format): "Some_html" CR LF Output text : "Some_html" CR CR LF However, when I convert my source file to unix format, and then I run TT, I get : Source text (unix format): "Some_html" LF Output text : "Some_html" CR LF (I use notepad++ to show the CR & LF characters; also to change unix <-> windows formats in the source template). When I google the

List of paths into hash array tree in Perl

我的未来我决定 提交于 2019-12-07 16:15:31
问题 I got an array of paths C:\A C:\B\C D:\AB and I'd like to have these in a hash array tree so I can go through them in a TT2 template. What I mean is like this: @dirs = [ { name => "C:", subs => [ { name => "A", subs => [], }, { name => "B", subs => [ { name => "C", subs => [], } ], } ] }, { name => "D:", subs => [ { name => "AB", subs => [], } ], } ] I also know that I'm probably doing brainderp here so I'm open to other approaches, only requirement is turning that list of paths into