2

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' } ... ]

wes
  • 7,795
  • 6
  • 31
  • 41
Sismetic
  • 227
  • 4
  • 16

1 Answers1

2

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.

wes
  • 7,795
  • 6
  • 31
  • 41
  • Yes, I have it set to 0! Thanks! If it will die when I try to set a param that doesn't yet exist on the template, how can I set that param then? I read the docs and I don't see why $Template->param('symbols') does not exist. In my template I have this: < TMPL_LOOP NAME = "symbols" > fetchQuoteData('[%symbol%]'); < /TMPL_LOOP> – Sismetic Jul 07 '14 at 21:43
  • Hi, I added some `TMPL_LOOP` info from the docs that may help. – wes Jul 08 '14 at 04:15
  • Of course, thank you very much for your help. I do send an arrayref of hashrefs apparently. I'll put the code as an edit – Sismetic Jul 08 '14 at 23:29
  • The mistake was that I had a spaces between < and TMPL_LOOP, so it didn't process it. Thanks anyway! You're awesome ;) – Sismetic Jul 09 '14 at 02:11
  • Glad it works now. I've never used HTML::Template, but it seems really finicky. I know it's not so helpful to recommend other packages/frameworks when you're already invested, but have you considered using [Template::Toolkit](https://metacpan.org/pod/distribution/Template-Toolkit/lib/Template/Manual/Intro.pod)? It's a proven and flexible templating system that I've used a great deal. There's a plugin for CGI::App (CGI::Application::Plugin::TT), as well. – wes Jul 09 '14 at 15:29