Announcement

Collapse
No announcement yet.

Checking for non-required attributes

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

  • SidFeyDesigns
    replied
    Originally posted by Kent Multer View Post
    The template language includes <mvt:foreachstop>, which can be used to break out of a loop.
    Ah ha. Did not know that one existed.

    Edited the code in the post.

    Thanks Kent!

    Leave a comment:


  • Kent Multer
    replied
    The template language includes <mvt:foreachstop>, which can be used to break out of a loop.

    Leave a comment:


  • SidFeyDesigns
    replied
    It would be great if there was a way to incorporate a while loop that could exit the loop after finding the first required or non-required attribute.

    Not sure if that is possible but we have products with a lot of attributes and it may be more efficient that way.

    Leave a comment:


  • SidFeyDesigns
    replied
    This may be a dead thread but I thought this would be helpful to the original poster or anyone else who stumbles upon the thread.

    I needed something similar as the original poster but based on if required attributes existed so I will include examples for both.

    Both the code examples are based on a variable assigned and counted with a small foreach loop as Kent suggested.

    Code to check if Required Attributes exist

    Code:
    <mvt:assign name="g.reqpos" value="1" />
    <mvt:foreach iterator="attribute" array="attributes">
            <mvt:if expr="l.settings:attribute:required">
                    <mvt:assign name="g.reqpos" value="g.reqpos + 1 " />
                    <mvt:foreachstop />
            </mvt:if>
    </mvt:foreach>
    <mvt:if expr="g.reqpos GT 1">
            CONTENT IF REQUIRED ATTRIBUTE EXISTS
    <mvt:else>
            other content (this content and the <mvt:else> are not required if only showing content based on required attributes existing)
    </mvt:if>
    Code to check if Non-Required Attributes exist

    Code:
    <mvt:assign name="g.nonreqpos" value="1" />
    <mvt:foreach iterator="attribute" array="attributes">
            <mvt:if expr="l.settings:attribute:required">
                    <mvt:comment> ========== Important to keep this blank and free of code or text ========== </mvt:comment>
            <mvt:else>
                    <mvt:assign name="g.nonreqpos" value="g.nonreqpos + 1 " />
                    <mvt:foreachstop />
            </mvt:if>
    </mvt:foreach>
    <mvt:if expr="g.nonreqpos GT 1">
            CONTENT IF NON-REQUIRED ATTRIBUTE EXISTS
    <mvt:else>
            other content (this content and the <mvt:else> are not required if only showing content based on non-required attributes existing)
    </mvt:if>
    Hope this is helpful.
    Last edited by SidFeyDesigns; 08-15-19, 07:57 AM.

    Leave a comment:


  • Kent Multer
    replied
    The expression l.settings:attributes:required will not return any value; that's not how Miva Script works. l.settings:attributes is an array; it has numbered elements, each of which holds the data for one attribute. Miva Script doesn't have a way to directly read or count the :required values of all the array elements. To count the number of required or non-required attributes, or to check whether there are any, you'll need to write a small mvt:foreach loop that goes through all the attributes, and checks the :required element of each one.

    HTH --

    Leave a comment:


  • Bruce - PhosphorMedia
    replied
    don't know of the top of my head if that function can take an expression or if l.settings:attribute:required is the correct value, but try miva_array_elements(l.settings:attribute:required EQ 1)

    Leave a comment:


  • gsam
    started a topic Checking for non-required attributes

    Checking for non-required attributes

    I've seen a few post on how to count the attributes of a product, such as:

    Code:
    <mvt:assign name="g.attribute_count" value="miva_array_elements( l.settings:attributes )" />
    <mvt:if expr="g.attribute_count EQ 0">    
    There are no attributes
    <mvt:else>    
    There are attributes
    </mvt:if>
    Is there a way to count only non-required attributes? In fact all I really need to know is if there is at least 1 non-required attribute.

    If I changed the above code to: value="miva_array_elements( l.settings:attributes:required )" I think it would still return a count since this variable exist for all attributes and is true or false.
Working...
X