We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15705
    • 9 Posts
    Quote from: BobRay at Mar 02, 2011, 05:21 AM

    I think you’ll need to write your own snippet with its own internal iterator. You can send a comma-delimited list of the TV names and just iterate through them, adding to the output and, for every other one, adding the class name, using if($iterator%2). It will be a lot faster than using PHX. PHX conditionals are very slow and hog a lot of resources.

    It would look something like this

    [!Iterator? &specs=`Spec1,Spec2,spec3,Spec4`!]


    <?php
    /*Iterator snippet */
    
    $iterator=0;
    $allSpecs = explode(',', $specs);
    $output='';
    
    foreach($allSpecs as $spec) {
        $output .= '<li';
        if ($iterator%2) {
            $output .= ' class="even "';
        }
        $iterator ++;
        $output .= '>[*' . $spec . '*]</li>' . "\n";
    }
    return $output;
    


    Awesome solution, thank you!
    But I had to modify the snippet call and still use pxh inside it because I lost ability to check if the [*Spec1..2..*] exists. So I did:
    [!Iterator? &specs=`[*Spec1:isnot=``:then=`Spec1`:else=``*],[*Spec2:isnot=``:then=`Spec2`:else=``*],[*Spec3:isnot=``:then=`Spec3`:else=``*],[*Sepc4:isnot=``:then=`Spec4`:else=``*]`!]
    


    and in the snippet I check if Spec is not empty and if it doesn’t I check what description name Spec has and publish it in the same <li>:
    <?php
    /*Iterator snippet */
    
    function description($anyspec)
    {
        switch ($anyspec) {
        case 'Spec1':
            return "Mass";
            break;
        case 'Spec2':
            return "Weight";
            break;
        case 'Spec3_cost':
            return "Color";
            break;
        case 'Spec4':
            return "Smell";
            break;
    	}
    }
    
    $iterator=0;
    $allSpecs = explode(',', $specs);
    $output='';
    foreach($allSpecs as $spec) {
    if($spec!=''){
        $output .= '<li';
        if ($iterator%2) {
            $output .= ' class="even "';
        }
        $iterator ++;
        $output .= '><table><tbody><tr><td class="title">'.description($spec).'</td><td class="body">[*' . $spec . '*]</td></tr></tbody></table></li>' . "\n";
         }
    }
    return $output;
    ?>
    


    Everything works great! Thank you!
      ^^
      • 3749
      • 24,544 Posts
      I’m glad it helped. FWIW, you can get the TV in code and check to see if it is empty in the snippet -- that would be faster. I just couldn’t remember how to do it in Evolution.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting