Skip to content

Instantly share code, notes, and snippets.

@low
Created September 10, 2012 07:14
Show Gist options
  • Save low/3689381 to your computer and use it in GitHub Desktop.
Save low/3689381 to your computer and use it in GitHub Desktop.
Avoid using a tag as parameter value for another tag

Avoid using a tag as parameter value for another tag in ExpressionEngine

Instead of this:

{exp:channel:entries param="{exp:some:plugin}"}
	...
{/exp:channel:entries}

Either use an embed, like this:

{embed="channel/entries" value="{exp:some:plugin}"}

{!-- And the embedded template: --}

{exp:channel:entries param="{embed:value}"}
	...
{/exp:channel:entries}

Or use the plugin as a tag pair (if supported), like this:

{exp:some:plugin parse="inward"}
	{exp:channel:entries param="{plugin_value}"}
		...
	{/exp:channel:entries}
{/exp:some:plugin}

Or add your own suggestions below.

@jeromecoupe
Copy link

Just a remark in favour of embedding:

With solution 2 (plugin tag pair), I have sometimes encountered strange little quirks, like {count} or {no_results} not working properly in the {exp:channel:entries}.
Wondered if that was something common or not and if you had tips and tricks to avoid that when developing plugin

Example with http://devot-ee.com/add-ons/dt-plugin and EE 2.5.2 (not picking o the plugin itself, just an illustration)

{exp:dt:wrap day="14" ee_format="true" format="%Y-%m-%d %H:%i" parse="inward"}

    <p>Begin date: {current_time format="%Y-%m-%d %H:%i"} // End date: {dt_ee}</p>
    {exp:channel:entries channel="events" disable="category|category_fields|member_data|pagination" orderby="entry_date" sort="asc" show_future_entries="yes" start_on="{current_time format='%Y-%m-%d %H:%i'}" stop_before="{dt_ee}"}
        {if count == "1"}<ul>{/if}
            <li>
                <h3>{title} - {count}</h3>
                <p>{cf_event_summary}</p>
            </li>
        {if count == total_results}</ul>{/if}
    {/exp:channel:entries}

{/exp:dt:wrap}

@croxton
Copy link

croxton commented Sep 11, 2012

You don't say why it's bad to pass a tag as a parameter, so here goes:

  • you need to use parse="inward" for it to work at all. That makes EE create a new instance of the template parser to parse each parameter, whether it's the parameter containing your tag or not. More parameters = more overhead.
  • if your passed tag outputs a quote character or a line break, the EE parser will choke.
  • sometimes EE will choke if the parent tag parameters are separated onto new lines when using parse="inward"

Despite these caveats, I still think it's a useful technique for certain things and has far less overhead than an embed.

@low
Copy link
Author

low commented Sep 11, 2012

@jeromecoupe: EE provides a method for add-on devs to easily parse variables. But that method will automatically add {count}, {total_results} and {switch} to the mix. That's why there's conflicts. Not much you can do about that without getting complicated.

@croxton: The main reason why it's bad, is that it will not work in all cases. It might work in some, but inconsistency is a big pain to explain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment