Announcement

Collapse
No announcement yet.

Using <mvt:assign with arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using <mvt:assign with arrays

    I use Toolbelt to sum array elements using and index to the array. but this does not work with mvt:assign, because it does not parse the array index.

    <mvt:item name="ry_toolbelt" param="assign|l.all_settings:totals[g.index]:quantity|l.all_settings:totals[g.index]:quantity + l.all_settings:item:quantity" />

    How would I do this in straight page template language.

    I've used miva_array_insert() to add items to an array, but here I need to sum elements
    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    #2
    I don't think you can assign the sum to an array element, although I have not tested, either way here's how to add those two vars together.

    <mvt:assign name="l.settings:sum" value="l.all_settings:totals[g.index]:quantity + l.all_settings:item:quantity" />

    sum = &mvt:sum;
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers

    Comment


      #3
      That's a good suggestion and gets us 1/2 way there. I swear, I worked out how to do that at one point; for the life of me, I can't remember how.
      I was just looking at this command with interest. http://www.mivascript.com/item/miva_...ilter_ref.html

      Ideally we would have support for one of these syntax examples.
      Hint Hint er.. ah... Jon B?
      Code:
      <mvt:assign name="{ l.variable_name }" value="g.total" />    Useful... Create the array structure element as a string assigned to l.variable first
      <mvt:assign name="l.array" index="l.index" member="total" value="g.total" />   Better
      <mvt:assign name="l.array[l.index]:total" value="g.total" />  My personal favorite.
      
      Or alternatively a new string function  miva_reference() would allow us to two step it.
      <mvt:assign name="l.varname" value="miva_reference( 'l.all_settings:totals[g.index]:quantity' )" />
      <mvt:assign name="l.varname" value="g.total" />
      
      Note again Toolbelt: <mvt:item name="ry_toolbelt" param="reference|new_variable_name|old_variable_name" />
      Since the template compiler is a subset of the Miva Script compiler. It should be doable, especially the parsing of [ l.index ]. Any bracket contents [xxx] in the variable name would need to be evaluated just like value="expression"
      Last edited by RayYates; 01-10-19, 06:49 AM.
      Ray Yates
      "If I have seen further, it is by standing on the shoulders of giants."
      --- Sir Isaac Newton

      Comment


        #4
        For Posterity

        I found a pure Miva Script solution using miva_array_filter_ref(), if you don't mind doing a search. It creates a record by reference to the Array index that you can then update.

        Code:
        <mvt:foreach iterator="item" array="order:items">
            <mvt:if expr="l.settings:item:code IN g.category_product_list">
        
                <mvt:assign name="l.record_found" value="miva_array_filter_ref( l.settings:totals, 1, l.item, 'l.item:code EQ l.settings:item:code', l.record )" />
        
                <mvt:if expr="l.record_found">
                    <mvt:comment> sum </mvt:comment>
        
                    <mvt:assign name="l.record[1]:price" value="(l.settings:item:price * l.settings:item:quantity) + l.record[1]:price" />
                    <mvt:assign name="l.record[1]:quantity" value="l.settings:item:quantity + l.record[1]:quantity" />
                <mvt:else>
                    <mvt:comment> create record </mvt:comment>
                    <mvt:assign name="l.record:code" value="l.settings:item:code" />
                    <mvt:assign name="l.record:unit_price" value="l.settings:item:price" />
                    <mvt:assign name="l.record:price" value="l.settings:item:price" />
                    <mvt:assign name="l.record:quantity" value="l.settings:item:quantity" />
                    <mvt:assign name="l.void" value="miva_array_insert( l.settings:totals , l.record, '-1' )" />
                </mvt:if>
        
            </mvt:if>
        </mvt:foreach>
        What I'd really like to see is a new function to update the array records. Ideally, it would only update element:members. Not the entire record.
        miva_array_update( array var, element var, pos )
        Ray Yates
        "If I have seen further, it is by standing on the shoulders of giants."
        --- Sir Isaac Newton

        Comment


          #5
          Hear, hear! I second this request.

          Thankfully the site I'm working on already has the toolbelt module. I'd rather not have to use it for this, but better than nothing.
          Susan Petracco
          NetBlazon

          1.866.400.2444

          _____________________________________________

          Like us on Facebook

          Comment

          Working...
          X