2

I have read ExpressionEngine documents and I am fairly comfortable while creating plugins. However I had a requirement in which I wanted my plugin to output data as a template tags. Like,

{exp:awesome_plugin}
    {awesome}{awesome} {!-- outputs the data set for awesome tag. --}
    {really}{really}
{/exp:awesome_plugin}

Is this possible?

  • To confirm, you would like your plugin to contain a variable pair that loops though it's own data a bit like the categories tag in the channel:entries tag? – Dylan Mar 8 '13 at 6:12
4

Yes this is possible. It took me a while to find some documentation when I first tried to do it within a plugin. You need to look at the docs for the template class instead of the plugin API. Specifically the section for variable pairs.

You need to create a multidimensional array that contains the different rows for your variable pair. Here's some relevant sections from a plugin I wrote that displays a gallery that's created using matrix, assets and playa. I've simplified it a lot but hopefully you will get the idea.

In the first bit of code I've done a database query to get my assets and then looped through the results to create the variable pair array:

$assets_array = array();
if ($assets_query->num_rows() != 0)
{
  // loop through assets and create an array indexed with the associated matrix row_id
  foreach ($assets_query->result() as $row)
  { 
    $assets_array[$row->row_id][] = array('image_url' => $this->EE->typography->parse_file_paths($row->file_path),
                                  'alt_text' => $row->alt_text
                                          );
  }
}

When I create the single vars for the main tag, I loop through the query result for the matrix rows and create a var that contains the assets array.

foreach ($matrix_query->result() as $row)
{
    $vars[] = array(
       'row_id'       => $row->row_id,
       'entry_id'     => $row->entry_id,
       'image'        => $assets_array[$row->row_id],
    );
}

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

Then in my template I can do something like this:

{exp:sp_gallery}
    {image}<img src="{image_url}" alt="{alt_text}"{/image}
{/exp:sp_gallery}

Hope that helps.

| improve this answer | |
  • Thanks yeah later I found that too! The docs needs to get updated :) – TeaCupApp Mar 10 '13 at 3:18
1

Yes, you can use the Template Class for it. See here how to create paired tags:

http://ellislab.com/expressionengine/user-guide/development/usage/template.html#parsing-variables

| improve this answer | |

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.