Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Output formatting preventing the multiplication of variables

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 1:44 pm    Post subject: Output formatting preventing the multiplication of variables Reply with quote

I'm attempting to modify a value within a vendor's template; a variable is being passed with pre-defined formatting (the thousands separator is added) which makes any multiplication applied to the variable fail when the value of the variable is $1,000.00 or greater.

I have no access to the raw data, only what is passed within the variable. Is there a simple way to convert, what I believe is being perceived as a string value, into a numeric value so mathematical operations can be applied?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 12, 2015 2:05 pm    Post subject: Reply with quote

Pick a baseball bat and visit the developer of the code that calls your template.
Any formatting must be applied in the template, not in code.
That said, no, there's no easy way of doing what you want to do, and anything you could do may fail next time the idiot that write the code decide to change the format. So… bat way is the safest way.
Back to top
View user's profile Send private message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 2:30 pm    Post subject: Reply with quote

Yes, that option has crossed my mind; I only wish it were that easy... I'm not too worried about the developer changing the code; I'm not sure they even know who wrote the original code.

Just to clarify... I'm looking to take information passed in a variable (which is being treated as a string value), remove the thousands separator, and define it as a numeric value so I can perform multiplication.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 12, 2015 4:22 pm    Post subject: Reply with quote

Write a custom modifier, that can parse the value and return pure number.
But seriosuly, I see no reason why you can't fix the calling code. It is PHP after all.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue May 12, 2015 4:31 pm    Post subject: Reply with quote

You can use also existing standard modifier like:

remove just ','
Code:
$var|replace:',':'' * $foo


or

remove ',' and '$'
Code:
$var|regex_replace:'/[$,]/':'' * $foo


see
http://www.smarty.net/docs/en/language.modifier.regex.replace.tpl
http://www.smarty.net/docs/en/language.modifier.replace.tpl
Back to top
View user's profile Send private message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 5:05 pm    Post subject: Reply with quote

AnrDaemon:
To clarify... I'm modifying a User template from within my Service Provider's site. I'm able to make changes to the user portion of the template but not to the database calls; that's handled behind the scenes and the Provider is not willing to make any changes.

I am looking for assistance with creating a custom modifier; that's why I'm here.


U.Tews:
I've tried using the standard modifiers listed, they both remove the unwanted comma but won't allow for the desired multiplication:

{$mrc_total|replace:',':'' * 2}
- This returns the variable's original value

{$mrc_total|regex_replace:",":"" * 2}
- This returns a Null value

I've also tried assigning the multiplier to a new variable:
{assign var="foo" value=2}
{$mrc_total|replace:',':'' * $foo}
- Same results as above

{$mrc_total|regex_replace:",":"" * $foo}
- Same results as above

Unfortunately, I'm only able to test my modifications from the Provider's system so I'm stuck with very limited error checking (errors: preview page won't load or results displayed are incorrect/blank).
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 12, 2015 5:21 pm    Post subject: Reply with quote

Wait, you're moving business logic into template code?
Sorry, but that's a completely different level of wrong.
Back to top
View user's profile Send private message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 5:38 pm    Post subject: Reply with quote

I would disagree if what I'm trying to accomplish is at all possible; I'll take "any level of wrong" if it achieves the required task. I'm stuck with a turd, I'll settle for it being polished.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue May 12, 2015 6:54 pm    Post subject: Reply with quote

Oh your are still running Smarty2

You will need the {math} plugin to do the multiplication

Code:
{math equation="x * y" x=$mrc_total|replace:',':'' y=2}
Back to top
View user's profile Send private message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 7:06 pm    Post subject: Reply with quote

U.Tews,

That worked perfectly! Thank you
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 12, 2015 7:24 pm    Post subject: Reply with quote

Sorry, but when someone asking me "how to better shoot myself in the leg", I don't give directions.
Back to top
View user's profile Send private message
sgarzarelli
Smarty Rookie


Joined: 12 May 2015
Posts: 6

PostPosted: Tue May 12, 2015 9:03 pm    Post subject: Reply with quote

I'd assumed after your second post that you were providing comic relief more than attempting to assist.

There are specific limitations that prevent me from taking a more preferred approach and I'm ok with the alternate solution which works perfectly for my needs. My options were: switch providers, continually perform manual updates to non-associated reports, find a way to get one field to accurately update.

I appreciate that you don't want to contribute to the implementation of a non-standard solution but, in this case, the only option was to manipulate the output within the template.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP