0

EE 5.2.4

I'm trying to create an array that can be looped through as tagdata in the frontend template.

My method grabs results from an API query, identifies images from the result, assembles them into an array, and parses them into tagdata.

  function item_detail ()
  {

    $id = ee()->TMPL->fetch_param('id');
    $result = $this->query_product($id);

    $variables = array();

    foreach ($result['digital_assets'] as $asset) {
      if ($asset['asset_resource_type'] == 'image' ) {
        $variables[0]['images'][] = $asset['url'];
      }
    }

    return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $variables);

  }

This gives me an array that looks like this

Array
(
    [0] => Array
        (
            [images] => Array
                (
                    [0] => http://domain.com/image/1.jpg
                    [1] => http://domain.com/image/2.jpg
                    [2] => http://domain.com/image/3.jpg
                    [3] => http://domain.com/image/4.jpg
                    [4] => http://domain.com/image/5.jpg
                )
        )

)

I would like to be able to loop through this array in the template like this

{images}
    // value
{/images}

There must be a way to do this?

Thanks!

1

I believe key should be name of variable. so instead of 0..1..2 it should be and array. Also, parse_variables use for loop. I belive you have single item and loop for sub item (images), You should return parse_variables_row. I have modified code a bit, that should work for you.

function item_detail()
{

    $id = ee()->TMPL->fetch_param('id');
    $result = $this->query_product($id);

    $prefix = ee()->TMPL->fetch_param('prefix');
    if($prefix == "") {
        $prefix = "images";
    }
    $variables = array();

    $cnt = 1;
    $total = 0;
    foreach ($result['digital_assets'] as $asset) {
        if ($asset['asset_resource_type'] == 'image' ) {
            $total++;
        }
    }
    foreach ($result['digital_assets'] as $asset) {
        if ($asset['asset_resource_type'] == 'image' ) {
            $variables['images'][] = array(
                $prefix . ':count' => $cnt++,
                $prefix . ':total_results' => $total,
                $prefix . ':image' => $asset['url'],
            );
        }
    }

    return ee()->TMPL->parse_variables_row(ee()->TMPL->tagdata, $variables);

}

Template code will look like this:

{exp:my_plugin_name:item_detail}
    {images}
        {images:count} - {images:total_results} - {images:image}<br>
    {/images}
{/exp:my_plugin_name:item_detail}

If you want to loop through EXP and not with sub variables, Code will be change a bit:

Plugin Code:

function item_detail()
{

    $id = ee()->TMPL->fetch_param('id');
    $result = $this->query_product($id);

    $prefix = ee()->TMPL->fetch_param('prefix');
    if($prefix == "") {
        $prefix = "images";
    }
    $variables = array();

    $cnt = 1;
    $total = 0;
    foreach ($result['digital_assets'] as $asset) {
        if ($asset['asset_resource_type'] == 'image' ) {
            $total++;
        }
    }

    if($total == 0) {
        return ee()->TMPL->no_results();
    }

    foreach ($result['digital_assets'] as $asset) {
        if ($asset['asset_resource_type'] == 'image' ) {
            $variables[] = array(
                $prefix . ':count' => $cnt++,
                $prefix . ':total_results' => $total,
                $prefix . ':image' => $asset['url'],
            );
        }
    }

    return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $variables);

}

Template Code:

{exp:my_plugin_name:item_detail}
    {if no_results} <p>NO DATA found!</p> {/if}
    {images:count} - {images:total_results} - {images:image}<br>
{/exp:my_plugin_name:item_detail}
2
  • 1
    Thanks @mufi. Very detailed and excellent answer! – Shayne Jul 15 '19 at 16:57
  • 1
    You're most welcome :) – Mufi - Amici Infotech Jul 16 '19 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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