2

I'm trying to use a template to parse through nested data, but some of the child data has the same variable names as the parent. In the example below I'd like to print a list of shows and then a list of the episodes for each show, but the id, title, & description are parsed and given the parent data.

$data = array(
    array(
        'id' => 1,
        'title' => 'LOST',
        'description' => 'Best. Show. Ever',
        'episodes' => array (
            array ( 
                'id' => 1001,
                'title' => 'Pilot',
                'description' => 'The one where the plane crashes.'
            ),
            array ( 
                'id' => 1002,
                'title' => 'Dr. Jack',
                'description' => 'Jack saves everyone.'
            )
        ),
    ),
    array(
        'id' => 2,
        'title' => 'Seinfeld',
        'description' => 'Funniest. Show. Ever',
        'episodes' => array (
            array ( 
                'id' => 2001,
                'title' => 'Pilot',
                'description' => 'The one where it wasn\'t funny yet.'
            ),
            array ( 
                'id' => 2002,
                'title' => 'Kramer',
                'description' => 'Kramer isn\'t that funny yet either.'
            )
        ),
    ),
);

$tags = '<h1>{title}</h1>
<p>{description}</p>
<h2>Episodes:</h2>
<ul>
{episodes}
<li><em>{id}: {title}</em> {description}</li>
{/episodes}
</ul>';

$output = $this->EE->TMPL->parse_variables( $tags, $data );

echo $output;
?>

This generates the output below:

<h1>LOST</h1>
<p>Best. Show. Ever</p>
<h2>Episodes:</h2>
<ul>

<li><em>1: LOST</em> Best. Show. Ever</li>

<li><em>1: LOST</em> Best. Show. Ever</li>

</ul><h1>Seinfeld</h1>
<p>Funniest. Show. Ever</p>
<h2>Episodes:</h2>
<ul>

<li><em>2: Seinfeld</em> Funniest. Show. Ever</li>

<li><em>2: Seinfeld</em> Funniest. Show. Ever</li>

How can I access the episode data? If the field names are different it works just fine, but this data is coming from a web service that is not easily changed.

1 Answer 1

1

Why are you using PHP to output your data?

I know you can attempt this with Playa, and a couple channels.

1
  • That was just for example sake. The data is coming back from a REST web service call using Phil Sturgeon's rest plugin. So all of that PHP is actually taking place inside the plugin, and then I'm accessing it via a "normal" template. The data isn't in EE, so channels & playa aren't an option. Oct 22, 2013 at 14:20

Your Answer

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

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