Home > templates > Output plugin result as template tag

Output plugin result as template tag

March 8Hits:1
Advertisement

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?

Answers

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.

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

Related Articles

  • Output plugin result as template tagMarch 8

    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

  • What is the best way to output plugin result in certain urlMarch 20

    Knowing that wordpress doesn't follow MVC pattern. What is the best way in worpress to output plugin's result in a certain url. lets say I would like to show "hello world" in main area under "www.example.com/show-hello-world" url. than

  • Using plugin results within channel tagsJuly 17

    I use this code using exp:math plugin: {exp:channel:entries channel="planilla_de_indicadores" limit="10"} {exp:math formula="([1] / [2]) * 100" parse="inward" params="{ps_talleres2}|{ps_talleres1}" decimal

  • How to manipulate wordpress template tags' outputNovember 11

    take the_category() as an example: Here is the output of the_category function in the wordpress loop: <ul class="post-categories"> <li> <a href="http://example.com/category/another-category/" rel="category tag"

  • How do I create a single template tag which outputs multiple bits of data?September 10

    I have just created my own module and need a bit of help displaying the content on the front end. I currently have some template tags {exp:address_book:contact}{/exp:address_book:contact} and within them a series of variables; {ab_name}, {ab_contact_

  • Find all template tags in a PHP string?February 16

    I have an activity event log in my app that logs every actions a user does in the app. The action records have an event_type to tell what type of event it is. There will be a page to view this activity log/stream as well. My app also allows users to

  • Template tags vs get_template_part() vs functions.phpDecember 18

    When to use template tags and when to use get_template_part() and when to use function.php? I am confused at them. For example, if I want to show related portfolios under single portfolio, I can create a template tag named show_related_portfolios().

  • Is it necessary to use esc_url with template tags such as get_permalink?August 6

    I have been reading that sanitizing data output is important in theme / plugin development for wordpress. Is it necessary to use functions such as esc_url with template tags like get_permalink, or is the sanitization taken care of by the template tag

  • Template tags for BuddypressDecember 22

    Is there a place that shows all available template tags and how to use them similar to how wordpress does? I'm trying to link to a profile within a loop. Ex of output <a href="http://www.mysite.com/members/Tom/">Tom's Profile</a> Tri

  • Use template tags in code snippets wordpressMay 18

    I'm trying to use WordPress template tags (through shortcodes) to merge the post title and url into hyperlink html code. Which looks like this: <a href="[Permalink]">[Post Title]</a>. Problem is the text output uses curly quotes arou

  • Template Tag "template_directory" pulling wrong path

    Template Tag "template_directory" pulling wrong pathSeptember 30

    I'm working on a theme index.php and it seems that when I use the "template_directory" it is not actually pulling the url path of the file, but instead is just pulling the main domain name. So instead of http://example.com/wp-content/themes/them

  • Does anyone have a plugin that could automatically tag custom post types in wordpress?January 28

    I was looking at http://wordpress.org/extend/plugins/web-ninja-auto-tagging-system/ and unfortunately, it wont work on custom post types. Can anyone suggest (with what changes are needed) to have this work for custom post types? --------------Solutio

  • Creating an instance of Expression Engine 2 plugin class from templateApril 10

    I have created an EE2 plugin under third_party folder and it succesfully shows up in Plugins list @ backend. Now, is it possible to create an instance of that plugin class directly from a template as I don't need any template tag behaviour? Eg. <?php

  • Why don't some template tags work when querying information on single.php?May 7

    I'm trying to fetch content about the current post into the header.php of my theme (to dynamically insert Facebook OpenGraph Metadata). Since I'm on a single post page, shouldn't it be able to run functions like the_content() or wp_get_attachment_thu

  • Is there a way to parse a template tag inside an extension hook handler?July 31

    As you know, we can't use template tags inside extension hook handlers because those will not be parsed. However is there an alternate way to access them inside extension hook handlers? Or something like given a tag string, have a function that can p

  • Plugin Development - using template params in a functionJune 5

    I'm querying an external timetable database, and I need to get the start and end points of the journey to use in a hidden field. My template code looks like this: {exp:get_piers:departure_pier cruise_id="{cruise_id}"} and {exp:get_piers:arrival_

  • Template tags to display custom post type posts in category template?January 12

    I am developing a custom theme with custom post types involved. Custom post types have been defined via plugin 'Pods' (http://pods.io). When writing a category.php, I realized, that the standard loop does not retrieve custom post types (CPT) posts wh

  • Checking values of context variables in Django custom template tag blockJanuary 15

    I'm trying to write a custom template tag in Django that will do the following: get all context variables between the start and end tags if all variables have a valid, non-empty value (i.e., not None or ''), return the entire block otherwise (if any

  • using invalid/incomplete html inside template tagJanuary 21

    I want to make templates for some rows, but I wonder if the specification allows one to omit the enclosing table tag for the rows. Is the following valid html if I want to traverse the resulting DOM? <template> <tr><td>some data</td&g

  • Where does `time` output its result?April 29

    I'm on Ubuntu. Running time seems to output its result to something other than STDOUT or STDERR. Here's why I think so: [[email protected] ~]$ time echo hi hi real 0m0.000s user 0m0.000s sys 0m0.000s [[email protected] ~]$ time echo hi >/dev/null real 0m0.000s

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.630 s.