epoch1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I thought I'd have a bash at a Dancer2 project but I'm having a slight problem with the TT DEFAULT directive. I'm using dbix class to return a single row from a database, then passing the output to a template:
my $rs = schema->resultset('Payment') ->find({id => route_parameters->get('code')}); template 'form', {'element' => $rs};
At the top of my form template I have the following to set default values for values which could be null in element:
{{ DEFAULT element.email = 'email' element.price = '0' element.date = 'date' element.title = 'title' }}
This doesn't work though. Even if the keys have values the DEFAULT values are applied. I did find this post http://www.perlmonks.org/?node_id=133377, but I still can't see what I'm doing wrong. I'm not looping through element I'm just printing each value out at different points in the template. Can anyone advise please?

Replies are listed 'Best First'.
Re: Template Toolkit DEFAULT for null hash value
by hippo (Archbishop) on May 07, 2016 at 17:32 UTC

    That approach works fine for me. Perhaps something is awry with your template?

    #!/usr/bin/env perl use strict; use warnings; use Test::More; use Template; my $text = q#[% DEFAULT element.email = 'email' element.price = '0' %] email: [% element.email %] price: [% element.price %] #; my $out = ''; my $tt = Template->new; $tt->process (\$text, {element => {price => 10}}, \$out); like ($out, qr/email: email/, 'Default email used'); like ($out, qr/price: 10/, 'Non-default price used'); done_testing;
      Thanks Hippo, Yes it's odd. I'm using element.foo || 'bar' at the moment as a workaround until I have more time to investigate.