View previous topic :: View next topic |
Author |
Message |
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 1:44 pm Post subject: Output formatting preventing the multiplication of variables |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue May 12, 2015 2:05 pm Post subject: |
|
|
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 |
|
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 2:30 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue May 12, 2015 4:22 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 5:05 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue May 12, 2015 5:21 pm Post subject: |
|
|
Wait, you're moving business logic into template code?
Sorry, but that's a completely different level of wrong. |
|
Back to top |
|
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 5:38 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue May 12, 2015 6:54 pm Post subject: |
|
|
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 |
|
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 7:06 pm Post subject: |
|
|
U.Tews,
That worked perfectly! Thank you |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue May 12, 2015 7:24 pm Post subject: |
|
|
Sorry, but when someone asking me "how to better shoot myself in the leg", I don't give directions. |
|
Back to top |
|
sgarzarelli Smarty Rookie
Joined: 12 May 2015 Posts: 6
|
Posted: Tue May 12, 2015 9:03 pm Post subject: |
|
|
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 |
|
|