1

I now have my module working like I need it to and my single tag pair calls the correct function. But I'm wanting to create a tag pair that is common in most modules/plugins. Here is the function I am calling:

function get_photos()
{
    $albums = unserialize( $this->get_setting_value( 'facebook_albums' ) );
    $limit = intval( $this->EE->TMPL->fetch_param('limit') );

    if( is_null( $limit ) || empty( $limit )  )
    {
        $limit = 50;
    }

    $layout = '';
    $i = 0;
    foreach( $albums as $album )
    {
        $photo_data = $this->get_facebook_graph_data( $album, 'photos' );
        foreach( $photo_data->data as $photo ) {
            if( $i === $limit )
            {
                break;
            }
            else {
                $layout .= sprintf( '<img src="%s" alt="%s" rel="" />', $photo->images[5]->source, @$photo->name );
            }
            $i++;
        }
    }

    return $layout;
}

I would like my tag pair to look like this:

{exp:fbphotos:get_photos}
   <img src="{source}" alt="{name} />
{/exp:fbphotos}

I'm not looking for someone to write all of the code, just a way to map the tags to values and create the loop. Thanks!

1 Answer 1

5

The methods you want to use in order to have a tag pair is:

//To loop over multiple items with variables 
//inside each (eg: looping through entries
$this->EE->TMPL->parse_variables($template, $data);

//To show a single item with multiple variables within it
$this->EE->TMPL->parse_variables_row($template, $data);

Most of the time you will use the parse_variables method, since it allows you to loop through multiple items.

The first method uses a multidimensional array, which is looped through for each data in each cycle. The first argument of each is the template data between the tag pair, which in EE can always be retrieved by using: $this->EE->TMPL->tagdata

Both methods return the finished string, so you would use them in your example as follows:

function get_photos()
{
    $albums = unserialize( $this->get_setting_value( 'facebook_albums' ) );
    $limit = intval( $this->EE->TMPL->fetch_param('limit') );

    if( is_null( $limit ) || empty( $limit )  )
    {
        $limit = 50;
    }

    $layout = '';
    $i = 0;
    $data = array();
    foreach( $albums as $album )
    {
        $photo_data = $this->get_facebook_graph_data( $album, 'photos' );
        foreach( $photo_data->data as $photo ) {
            if( $i === $limit )
            {
                break;
            }
            else {
                //Build a multidimensional array, where each sub array
                //contains data for a single "loop"
                $data[] = array(
                    "source" => $photo->images[5]->source, 
                    "name" => @$photo->name 
                );
            }
            $i++;
        }
    }

    return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $data);
}

In your template you can then use tag pairs:

{exp:fbphotos:get_photos}
   <img src="{source}" alt="{name} />
{/exp:fbphotos:get_photos}

Update

If you are unsure about how to use the EE Template Parser library ($this->EE->TMPL) checkout the official docs.

2
  • Ok, I added your code and all it returns is <img src="{src}" alt="{name}"> {/exp:fbphotos}. Any thoughts as to why this is happening? Jan 15, 2013 at 1:16
  • 1
    Can you ensure the closing tag is correct {exp:fbphotos:get_photos} <img src="{source}" alt="{name} /> {/exp:fbphotos:get_photos} Jan 15, 2013 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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