1 of 27

Parse Order in

ExpressionEngine’s Templates

But First...

2 of 27

Introduction:

Who am I?

Rowan Sentesy

Self-taught (mostly)

~12 years EE experience

Entrepreneur

Freelance

Now: developer with Hop Studios

3 of 27

Introduction: Overview

What we’re going to talk about:

  1. What is Parse Order?
  2. Hop’s EE6 Parse Order PDF
  3. Parse order problems
  4. PHP in Templates
  5. Examples
  6. Q&A

4 of 27

EE Templating

  • Powerful
  • Elegant
  • Easy to get quick results
  • Simple things are .. simple!
  • Complexity
    • Snippets
    • Addons
    • Variables
    • Caching

5 of 27

What is Parse Order?

{stuff} => <html>..</html>

Parse & Render

Tags are rendered, can be used later in the process.

Order is important

6 of 27

Let’s see what EE has to say...

7 of 27

Can we change parse order?

Yes … Kind-of …

8 of 27

Example 1 - Default (Outward)

<h1>Default (outward)</h1>

{exp:rss_parser url="https://ellislab.com/blog/rss-feed" limit="5"}

<ul>

{feed_items}

<li><a href="{item_link}">{item_title}</a><br />

{exp:word_limit total="35"}

{item_content}

{/exp:word_limit}

</li>

{/feed_items}

</ul>

{/exp:rss_parser}

9 of 27

Example 2 - Inward

<h1>Inward</h1>

{exp:rss_parser url="https://ellislab.com/blog/rss-feed" limit="5" parse="inward" }

<ul>

{feed_items}

<li><a href="{item_link}">{item_title}</a><br />

{exp:word_limit total="35"}

{item_content}

{/exp:word_limit}

</li>

{/feed_items}

</ul>

{/exp:rss_parser}

10 of 27

Example 3 (bonus)

<h1>Outward with :limit</h1>

{exp:rss_parser url="https://ellislab.com/blog/rss-feed" limit="5"}

<ul>

{feed_items}

<li><a href="{item_link}">{item_title}</a><br />

{item_content:limit characters="210"}

</li>

{/feed_items}

</ul>

{/exp:rss_parser}

11 of 27

Extension Hook Priority

Two hooks for template parsing:

  1. template_fetch_template
  2. template_post_parse

priority - an extension hook could have many extensions being called, so there needs to be priority. 1 => First, 10 => common default, 99+ => Last.

Think: z-index for timing.

12 of 27

The Big Ones �(my list)

  • Module & plugin tags
  • Variables (single, pair)
  • Conditional tags
  • Site Template Variables
  • Template Partials
    • The variables formerly known as ‘snippets’
  • Template Layout tags
  • Embeds
  • PHP Code

13 of 27

Thanks to Low

Kudos to @Low for this EE 2 Parse order document!

Back in 2010, Low created this document.

Lodewijk Schutte (aka @Low)

14 of 27

Introducing the …

EE Parse Order

Quick Reference Guide

15 of 27

Common problems with Parse Order

Variable name collision was a problem when nesting {exp:}tag pairs!

  • {count} {total_results} {no_results} �would sometimes show unexpected results

Over the years this has become less of an issue, thanks to prefixing!

  • var_prefix (i.e. Matrix)
  • EE Relationships & Grid - full field name prefixing�{relationship_field:my_relationship status="open"}...

16 of 27

Prefixing: A Quick look

Generally, this works very well.

It is rare to encounter issues with this since Relationships & Grid were introduced by EE.

17 of 27

Use PHP in templates

Don’t do it!

  • Experienced developer
  • Use an existing Add-on
  • Use an embed
  • Security
  • Performance
  • Complexity
  • Maintainability
  • Time/Budget

Do it!

  • Beginner or new to EE
  • Can’t solve with add-on
  • Can’t solve with embed
  • Security
  • Performance
  • Simple to moderately complex
  • Time/Budget

18 of 27

PHP not in EE (without add-on)

PHP to provide info about

  • System, server, or client
  • Functionality or data
  • CURL
  • Connect to another db

Example: file directory listing

19 of 27

PHP Parse Stage

Parse stage: Input

Do some PHP feed results into EE module.

<?php

/* Set an array */

$entry_ids = [1,2,3,4,5];

?>

<!-- Use an array -->

{exp:channel:entries entry_id="<?=implode("|",$entry_ids)?>"}

<h1>{title}</h1>

{/exp:channel:entries}

Parse stage: Output

Use EE module to build php variable/array.

<?php

$data = [];

?>

{exp:channel:entries limit="100"}

<?php

$data[] = [{entry_id},'{title}'];

?>

{/exp:channel:entries}

<?php

echo "<pre>";

print_r($data);

echo "</pre>";

?>

20 of 27

Problem:

List & Total

Consider this example

How would you solve for…�Price total (numeric)�Emails (list)

Type it in the chat.

21 of 27

Try without PHP

(First try)

Requires: Stash, MX Calc

  • Parsing not working
  • MX Calc was not working with Stash

22 of 27

Try without PHP�(Second try)

Requires: Embed, Stash, Tally

23 of 27

Try with PHP

Requires: PHP (output)

24 of 27

List & Total: Conclusions

Which do you like the best?

  • Embeds, addons can be used
    • Can be fiddly & confusing
  • PHP example is simplest, cleanest code (IMHO)

25 of 27

But First...

Summary

  • What is Parse Order?
  • Hop’s EE6 Parse Order PDF
  • Old Parse order problems
  • PHP in Templates
  • Examples

26 of 27

Q&A

Ask in the Q&A tab,

or follow the instructions provided by EE Conference team...

27 of 27

Thank you!

Thanks to the EE Team: �Tom, Brian, Andy (and others!)

Reach out:rowan@hopstudios.com

EE Slack:�@Rowan Sentesy

Twitter:�@hopstudios

Parse Order PDF:

hopstudios.com/parse_order