Search code examples
perltemplatesparamcgi-applicationperl-html-template

CGI::Application param() not setting


I have a piece of code that uses CGI::Application as its base, but something is not working correctly.

When I try to set something via $Template->param() it seemingly does not set. $Template is equal to $self->load_tmpl($template);

And the piece I want to save is:

$Template->param('symbols' => \%a_hash_ref);

I know \%a_hash_ref contains the expected value. It has a similar form:

{'symbol' => 'DTX'},{'symbol' => 'QFLD'}

Also, if I do

$Template->param('Hey!xD' => 'Something');

it doesn't save, but

$Template->param($Pagination);

does, where $Pagination is also a hash_ref.

I know all the values are what they should be, and I also tried it with some simple strings, which should work, but they are not set. I know this because when I run:

my @params = $Template->param();
die Dumper \@params;

it outputs all the variables it should have set, but the expected ones(including the 'Hey!xD' string) are missing. I also know it actually runs the code, because this die Dumper is after I try to set the values.

The template file contains this piece of code:

$(document).ready(function () { mainFunction('[%symbol%]'); });

Any help would be appreciated.

EDIT:

This is what is being given to the param:

[ { 'date' => '2006-07-05', 'avg_gain' => undef, 'bollinger_mid' => '32.80000', 'symbol' => 'BBQ' }, { 'date' => '2006-04-04', 'avg_gain' => undef, 'bollinger_mid' => '34.55656', 'symbol' => 'AAPL' } ... ]


Solution

  • This is actually a HTML::Template problem, which CGI::Application uses by default for templating.

    Do you perhaps have die_on_bad_params set to 0 in your load_tmpl call? HTML::Template should by default die if you try to set a parameter that doesn't exist in the loaded template (docs here). When die_on_bad_params is disabled, you'll get the behavior you're describing, where an unknown template param is ignored and discarded.

    Additionally, according to the docs, TMPL_LOOP takes an arrayref of hashrefs as its param argument. Maybe it would help to pass your "symbols" parameter like this:

    my $symbols = [{ symbol => 'DTX' },{ symbol => 'QFLD' }];
    $Template->param(symbols => $symbols);
    

    If you post a sample template and small script that shows the error it'll be easier to tell for certain.