Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Math in Template::Toolkit

by talexb (Chancellor)
on Apr 30, 2007 at 02:35 UTC ( [id://612686]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to do some relatively simple math in a template, and I appear to be running into an error. perhaps someone hear can suggest a solution.

... [%- IF top_menu.$base_name != '' -%] [%- page_title = top_menu.$base_name -%] [%- width = (100/(top_menu.$size)); -%] [%- extra = (100-(width*(top_menu.$size))); -%] [%- ELSE -%] ...

This first sample compiles fine .. it has to do with setting up some nice spacing of menu items on a web page, nothing terribly exciting. But ..

... [%- IF top_menu.$base_name != '' -%] [%- page_title = top_menu.$base_name -%] [%- width = (100/(top_menu.$size)); -%] [%- extra = ((100-(width*(top_menu.$size)))/2); -%] [%- ELSE -%] ...
gives me the error
file error - parse error - vb-template.tt2 line 31: unexpected token (/2) [% extra = ((100-(width*(top_menu.$size)))/2); %]

I've looked through the man pages, asked on #perl, asked on #tt on irc.perl.org, and I'm now stuck.

Suggestions appreciated.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Math in Template::Toolkit
by kyle (Abbot) on Apr 30, 2007 at 03:43 UTC

    What's in top_menu? I can imagine it's something like this:

    $top_menu = { base_name => 'foo', size => 23 };

    And after your code executes, you expect width = 3.03 and extra = 0. Yes?

    What your code actually does is something different. To get the base_name element in a top_menu hash in TT, it's top_menu.base_name (no dollar sign). If you say top_menu.$base_name in TT, it's like $top_menu[$base_name] in Perl.

    I could be wrong about this, however. I'd expect top_menu.$size to be zero if you haven't set size to anything in particular, so I'd expect 100/top_menu.$size to bomb out as division by zero. Also, the error you're getting isn't really explained by what I'm talking about.

    Anyway, I think a Data::Dumper look into your variables might help.

    I also wonder if it would help to explicitly say

    [%- SET extra = ((100-(width*(top_menu.$size)))/2); -%]

    ...although I'm not sure why that would help.

Re: Math in Template::Toolkit
by GrandFather (Saint) on Apr 30, 2007 at 02:48 UTC

    Looks ok (correct bracket matching and such) so maybe you've hit a bug in the parser? Try variations like:

    (100-(width*(top_menu.$size)))/2 (50-(width*(top_menu.$size)/2)) (100-(width*top_menu.$size))/2 50-(width*top_menu.$size)/2 50-width*top_menu.$size/2

    DWIM is Perl's answer to Gödel

      Good idea, but ..

      [% extra = width/2; %]; [% extra = (width/2); %] [% extra = ($width/2); %]

      all failed. :(

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        how about width*.5 ? ;)
Re: Math in Template::Toolkit
by Herkum (Parson) on Apr 30, 2007 at 12:03 UTC

    I am willing to beat the error is further up the script than what you have shown us. The line you showed us is where the compiler decided were it thought the error was, not where the error actually is

    Consider moving your code out of the template and into a plugin. It would easier to read your template and I find it easier to debug Perl code rather than templates that do not compile.

Re: Math in Template::Toolkit
by FunkyMonk (Chancellor) on Apr 30, 2007 at 20:40 UTC
    Adding spaces around the divide works for me
    zippy:~$ cat x [% width = 5 -%] [% extra = width / 2; -%] [%- extra %] zippy:~$ tpage x 2.5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://612686]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found