2

I'm trying to do some basic math using a Stash Get var and MX Calculator. Any recommendations? I'm sure my lack of understanding of parse order strikes again.

I can get the variable using:

{exp:stash:get name='house_starting_price'}

But when I try to use it in the MX_Calculator plugin the value doesn't parse.

{exp:mx_calc parse="inward" expression="({exp:stash:get name='house_starting_price'}*.05)"}

Based on feedback I've made the following adjustments:

The stash variable is set like so (note it is in a matrix field):

{exp:channel:entries channel="Homes" dynamic="no" limit="1" url_title="{segment_3}" require_entry="yes" disable="categories|category_fields|member_data|pagination"}
                {!-- Go through the elevations matrix but only pull the fields that are from the current community, set with segment_4 --}
            {elevations orderby="elevation_name" sort="asc" orderby="elevation_name" sort="asc"  search:elevation_community="{segment_4}"}
                {!-- Stash the first price as starting price for community --}
                {exp:stash:set name="house_starting_price" match="^1$" against="{row_count}"}{elevation_price thousands_sep=''}{/exp:stash:set}
            {/elevations}
{/exp:channel:entries}

I've modified the stash get to use stash:parse:

    <br>starting price = {exp:stash:get name='house_starting_price'}
    {exp:stash:parse parse="end"}<br>This is within stash parse: {exp:stash:house_starting_price}

    <br>MX Calculator value: {exp:mx_calc expression="({exp:stash:house_starting_price}*.05"})
{/exp:stash:parse}

Here's what's output:

Starting price = 326600
This is within stash parse: 326600
MX Calculator value:

In the output debugger I see:

 {exp:mx_calc expression="(M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr*.05"})
 {exp:mx_calc expression="(365000*.05)"} <-manually entered 365000

UPDATE 2012-12-01

I've simplified this even more for testing. This takes channel:entries out of the equation all together. I'm finally getting it to calculate but I'm seeing some weird stuff.

{!-- Set the mytestvariable --}
{exp:stash:set name="mytestvariable"}645323{/exp:stash:set}
<br>The test variable outside of MX Calculator equals: {exp:stash:get name="mytestvariable"}

    {!-- Use stash:parse to attempt using the stash var within MX Calculator --}
    {exp:stash:parse process="end"}

      {!-- Ensure the varible loads within stash:parse --}
      <br>The test variable within Stash Parse equals: {exp:stash:get name="mytestvariable"}

      {!-- Try to use the stash variable in MX Calculator --}
      <br>The test varible within MX Calculator equals: {exp:mx_calc parse="inward" expression="{exp:stash:get name='mytestvariable'}*.05"}
    {/exp:stash:parse}

Output:

The test variable outside of MX Calculator equals: 645323
The test variable within Stash Parse equals: M60o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr
The test varible within MX Calculator equals: 32266.15

Question: Should the second value not equal 645323?
Note: I tried switching all stash gets to shorthand version as well: {exp:stash:mytestvariable}

When I do this I get the following output:

The test variable outside of MX Calculator equals: 645323
The test variable within Stash Parse equals: 32266.15
The test varible within MX Calculator equals:

When I look in the debugger MX Calc is using M60o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr*.05 instead.

  • How are you setting the stash variable? – Siebird Nov 30 '12 at 18:23
  • 2
    My guess is that when you set the variable 'house_starting_price' you may have captured expressionengine tags rather than their parsed value. Please post your set code. – Mark Croxton Nov 30 '12 at 19:01
  • Updated with more info. – Anson LeClair Nov 30 '12 at 21:19
  • I updated my answer with new context – Siebird Nov 30 '12 at 21:25
  • @Siebird Ok, I've changed it and EE is still outputting with a comma in all places. From a look in the database it's stored without a comma. I'm pretty sure I started with Integer, been switching it around trying to find a solution. I could wrap some PHP around it to strip the comma, but would prefer not to. – Anson LeClair Nov 30 '12 at 21:27
0

When EE parses a template it looks for tags that are identical, parses the first in the set that it finds and replaces the rest with a cache marker M60o9...

The problem here is simply that you used the identical tag twice, with the second instance being held within a queue (by stash:parse) to be parsed after all template parsing has completed:

{exp:stash:get name='house_starting_price'}

{exp:stash:parse process="end"}
    {exp:stash:get name='house_starting_price'}
{/exp:stash:parse}

EE therefore never replaces the second instance of the tag with the cached value.

The solution is to simply add the word 'random' to the list of parameters passed to the second instance (and any other instances in the template):

{exp:stash:get name='house_starting_price'}

{exp:stash:parse process="end"}
    {exp:stash:get name='house_starting_price' random}
{/exp:stash:parse}

Alternatively, the stash variable can be accessed as a placeholder, as it is being used within a stash:parse tag pair:

{exp:stash:get name='house_starting_price'}

{exp:stash:parse process="end"}
    {stash:house_starting_price}
{/exp:stash:parse}
  • Thanks Mark! I think I'm going to start a "Rules of Stash" notebook in Evernote :) – Anson LeClair Dec 4 '12 at 13:24
1

Looks like you may need to delay parsing of the plugin. Try this:

{exp:stash:parse process="end"}
{exp:mx_calc parse="inward" expression="({stash:house_starting_price}*.05)"}
{/exp:stash:parse}

The {exp:stash:parse} tag is new to the beta version. So you may need to change versions

Update

The comma is definitely throwing the calculation off. Set the content to that matrix column to integer so the data is standardize. Then I would use JS to format the currency

update TABLE_NAME 
set FIELD_NAME = replace(
    FIELD_NAME, 
    ‘,’, 
    ‘’
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.