Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Ability to perform calculations in templates

Quote Reply
Ability to perform calculations in templates
I think it would be cool to be able to perform some basic calculations in the templates using something like:
<%if Username%><%calc Field1 * 0.2%><%endif%>

Where this would multiply Field1 by 0.2 and replace the value within the calculation. Having them nested in parenthesis would be the ultimate!

Quote Reply
Re: Ability to perform calculations in templates In reply to
I think you can do that, because each tag can even be a function.

There were some examples posted, and the "date" field in the template is one example.

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ


Quote Reply
Re: Ability to perform calculations in templates In reply to
Really Shocked
That will be VERY nice...
I should download it to try it out!

Quote Reply
Re: Ability to perform calculations in templates In reply to
You can calculate fields via tags in the template file...I've used adding, subtracting, etc. between different field values via tags.

Regards,

Eliot Lee
Quote Reply
Re: Ability to perform calculations in templates In reply to
That is awesome...
AnthroRules out of curiosity how extended is the calculation ability? Would something like this be possible?
<%if Username%><%begincalc%><%if MyVarField = 12%><%SpecialDiscountVar%><%endif%> * 0.2<%endcalc%>

Can you give me an example of it's usage, and is it just a Template.pm modification to parse or is it more than that?

This feature will make running online stores based on Links, very easy I would imagine...by being able to drop all the needed <input hidden> tags into a cgi script.

Thanks for the feedback! Smile
Just this feature addition alone, has me WAY EXCITED about the future of LinkSQL 2.x, kudos Alex on this one!!!

PS: If it is just changes to Templates.pm can you send me a copy of the new parse routine via private message, this is one feature I was just wanting to incorporate into my site and was looking at parse() to see if I could add in in support for it.

Quote Reply
Re: Ability to perform calculations in templates In reply to
You can't do math calculation inside template tags, but it is fairly easy.

1. Go to Template Editor->Globals and create a tag mycalc with:

sub {
my $tags = shift;
return $tags->{Field1} * 0.2;
}

2. In your template put:

<%if Username%><%mycalc%><%endif%>

which will do what you want.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Ability to perform calculations in templates In reply to
Hi

When I do this:

sub {
my $tags = shift;
return $tags->{storrelse} / 7;
}

it returns a number with very many decimals. Is it possible to get the Int value of the calculation into my tag insted, like INT(storrelse / 7) ?

Quote Reply
Re: Ability to perform calculations in templates In reply to
return int ($tags->{storrelse} / 7 );


PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ