We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

A module with nested pair variables?

Development and Programming

J3roen's avatar
J3roen
21 posts
14 years ago
J3roen's avatar J3roen

I’m trying to create module method that needs the support of nested pair tags.

For example:

{exp:module:method}

    {product-category}
        <div id="{category}">

            {product}
                <div = "{product-id}">
                {poduct-info} {some-other-tags}
                </div>
            {/product}

        </div>
    {/product-category}

{/exp:module:method}

Note: every tag pair can contain one or more rows.

If I don’t use nested tag-pairs, ie. just one, it works fine. But nested variables won’t work. Should this be possible or is it a limitation of the templating system?

http://ellislab.com/expressionengine/user-guide/development/usage/template.html#parsing_variables_pair_vars < this is the method I’m using. As in creating an multidimensional array and parse it all at once using the $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $variables); method.

If it is possible, can someone maybe post an example of the code and array that is fed to the parse_variables method?

Thanks for taking the time to read this!

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
14 years ago
Rob Sanchez's avatar Rob Sanchez

I was confused by this at first as well. A tag pair’s variable array is not just a lone associative array, but an (indexed) array of (associative) arrays. So a tag pair is meant to loop through all of the rows of associative arrays, even if you are only using one “row” per tag pair.

$variables = array(
    0 => array(
        'product-category' => array(
            0 => array(
                'product' => array(
                    0 => array(
                        'product-id' => 1,
                        'product-info' => 'something',
                        'some-other-tag' => 'else',
                    ),
                    1 => array(
                        'product-id' => 2,
                        'product-info' => 'other',
                        'some-other-tag' => 'test',
                    ),
                ),
            ),
        ),
    ),
);
       
J3roen's avatar
J3roen
21 posts
14 years ago
J3roen's avatar J3roen

Thanks Rob for taking the time to answer!

First going to have something to eat, and then give it a go again. A clear mind, a full stomach and a good example array will probably do it 😊

I’ll let you know how it worked out!

       
J3roen's avatar
J3roen
21 posts
14 years ago
J3roen's avatar J3roen

Thanks again Rob, I get it now! It’s working as expected.

For other people struggling with the same issue, here again is Rob’s code but with the addition of the ‘category’ tag + example of more then 1 category.

$vars = array(

            0 => array(
                /* Product-category tag pair */
                'product-category' => array(
                    0 => array(
                        /* Single var */
                        'category' => 'Category 1',
                        
                        /* Product tag pair */
                        'product' => array(
                            0 => array(
                                /* Single vars */
                                'product-id' => 1,
                                'product-info' => 'something',
                                'some-other-tag' => 'else',
                            ),
                            1 => array(
                                /* Single vars */
                                'product-id' => 2,
                                'product-info' => 'other',
                                'some-other-tag' => 'test',
                            ),
                        ),     /* /Product tag pair */                        
                    ),

                    1 => array(
                        /* Single var */
                        'category' => 'Category 2',
                        
                        /* Product tag pair */
                        'product' => array(
                            0 => array(
                                /* Single vars */
                                'product-id' => 1,
                                'product-info' => 'again something',
                                'some-other-tag' => 'else',
                            ),
                            1 => array(
                                /* Single vars */
                                'product-id' => 2,
                                'product-info' => 'another',
                                'some-other-tag' => 'test',
                            ),
                        ),     /* /Product tag pair */                        
                    ),

                ), /* /Product-category tag pair */
            ),

        ); /* /Main array */

    $output = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
    return $output;

Using this with:

{exp:module:method}
    {product-category}
        <div id="{category}">
            {product}
                <div = "{product-id}">
                {product-info} {some-other-tag}
                </div>
            {/product}
        </div>
    {/product-category}
{/exp:module:method}

Will produce:

<div id="Category 1">
    
        <div = "1">
        something else
        </div>
    
        <div = "2">
        other test
        </div>
    
</div>

<div id="Category 2">

    
        <div = "1">
        again something else
        </div>
    
        <div = "2">
        another test
        </div>
    
</div>
       
Rob Sanchez's avatar
Rob Sanchez
335 posts
14 years ago
Rob Sanchez's avatar Rob Sanchez

And by the way, you don’t have to do 0 => , 1 => etc, since indexed arrays will take care of this naturally, I just put it in there to demonstrate that there are multiple rows.

       
J3roen's avatar
J3roen
21 posts
14 years ago
J3roen's avatar J3roen

Got it! 😉 Same as the [] behavior.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.