Parse Order Pro

Parse Order Pro

Be a Pro with ExpressionEngine's Parse Order.

A347386377e120e3230403d2558c1a0d?s=128

Lodewijk Schutte

October 12, 2011
Tweet

Transcript

  1. Parse Order Pro By Lodewijk Schutte ~ Low, October 2011

  2. Hi! I'm Low!

  3. gotolow.com

  4. None
  5. ExpressionEngine’s Parse Order In what order does EE parse its

    variables and tags? Read URI to determine template Get template from DB, check permissions & authentication Get template from file Return static template 1. Parse snippets / global variables, segment variables and embed variables * Parse date string constants Parse {template_edit_date} and {current_time} If present, get cached template, then go to #7 2. Parse PHP on Input 3. Parse simple conditionals: segment, embed, global variables 4. Assign and parse preload_replace variables 5. Parse module and plugin tags 6. Parse PHP on Output Write cache file 7. Parse advanced conditionals 8. Process embedded templates Redirect 9. Parse User Defined global variables and others: {hits} {ip_address} {gzip_mode} {app_version} {group_title} {member_group} gotolow.com/parse-order
  6. None
  7. Parse Order Parse Order

  8. None
  9. How does parsing work? •Document (template) •Markers (variables and tags)

    •Values (data) •Find & Replace (parsing)
  10. Parse Order Parse Order

  11. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     3 {a} {b} {a} {c} {c}
  12. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     3 1 {b} 1 {c} {c}
  13. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     3 1 2 1 {c} {c}
  14. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     3 1 2 1 3 3
  15. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     3 1 2 1 3 3
  16. None
  17. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} {a} {d} {b} {a} {c} {c} {e} {e}
  18. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 {d} {b} 1 {c} {c} {e} {e}
  19. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 {d} 2 1 {c} {c} {e} {e}
  20. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 {d} 2 1 {d} {d} {e} {e}
  21. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 3 2 1 3 3 {e} {e}
  22. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 3 2 1 3 3 {a} {a}
  23. Parse Order {a}  =>  1 {b}  =>  2 {c}  =>

     {d} {d}  =>  3 {e}  =>  {a} 1 3 2 1 3 3 {a} {a}
  24. None
  25. Parse Order in ExpressionEngine

  26. Early vs. Late stuff {exp:tags} more  stuff Early Late

  27. Input vs. Output stuff {exp:tags} more  stuff PHP  on  Input

    PHP  on  Output
  28. Variables

  29. Variable Scope

  30. Variable Scope Global Variables: all templates

  31. Variable Scope Regional Variables: single template

  32. Variable Scope Local Variables: single template, inside tag

  33. Global Variables •Parsed Early • Snippets • Config variables •

    Segment variables •Parsed Late • User Defined Global Variables • "Standard Global Variables" http://bit.ly/standard-global-vars
  34. Regional Variables •Parsed Early • Embed variables • Preload replace

    variables * *) Parsed early-ish
  35. Local Variables •Parsed during Tags •Can be more complex •

    Variable parameters • Variable pairs
  36. All Variables snippets config  vars segment  vars embed  vars preload

     replace  vars {exp:tags}  local  vars  {/exp:tags} user  defined  vars standard  vars
  37. None
  38. Variables Example 1

  39. {gv_header} <h1>{sn_{segment_1}_title}  containing  {username}</h1> {exp:channel:entries  channel="{segment_1}"    search:cf_summary="{username}"}    

       <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  40. Snippets {gv_header} <h1>{sn_{segment_1}_title}  containing  {username}</h1> {exp:channel:entries  channel="{segment_1}"    search:cf_summary="{username}"}  

         <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  41. {gv_header} <h1>{sn_{segment_1}_title}  containing  {username}</h1> {exp:channel:entries  channel="{segment_1}"    search:cf_summary="{username}"}    

       <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  42. Segment vars {gv_header} <h1>{sn_{segment_1}_title}  containing  {username}</h1> {exp:channel:entries  channel="{segment_1}"    search:cf_summary="{username}"}

           <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  43. {gv_header} <h1>{sn_news_title}  containing  {username}</h1> {exp:channel:entries  channel="news"    search:cf_summary="{username}"}    

       <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  44. Tags & Local vars {gv_header} <h1>{sn_news_title}  containing  {username}</h1> {exp:channel:entries  channel="news"

       search:cf_summary="{username}"}        <h2>{title}</h2>        {cf_summary} {/exp:channel:entries} {gv_footer}
  45. {gv_header} <h1>{sn_news_title}  containing  {username}</h1> {gv_footer}

  46. User Defined Global vars {gv_header} <h1>{sn_news_title}  containing  {username}</h1> {gv_footer}

  47. <html> <head>    <title>{sn_{segment_1}_title}  -­‐  {site_name}</title> </head> <body> <h1>{sn_news_title}  containing

     {username}</h1> <p>&copy;  {current_date  format="%Y"}</p> </body> </html>
  48. Standard Global vars <html> <head>    <title>{sn_{segment_1}_title}  -­‐  {site_name}</title> </head>

    <body> <h1>{sn_news_title}  containing  {username}</h1> <p>&copy;  {current_date  format="%Y"}</p> </body> </html>
  49. <html> <head>    <title>{sn_{segment_1}_title}  -­‐  EE  Outsider</title> </head> <body> <h1>{sn_news_title}

     containing  kennymeyers</h1> <p>&copy;  {current_date  format="%Y"}</p> </body> </html>
  50. Variables Example 2

  51. {sn_header} {preload_replace:pre_limit="1"} {preload_replace:pre_body="{cf_summary}"} {sn_page} {gv_footer}

  52. Snippets {sn_header} {preload_replace:pre_limit="1"} {preload_replace:pre_body="{cf_summary}"} {sn_page} {gv_footer}

  53. <html> <head>    <title>{gv_{segment_1}_title}</title> </head> <body> {preload_replace:pre_limit="1"} {preload_replace:pre_body="{cf_summary}"} {exp:channel:entries  channel="{segment_1}"

       limit="{pre_limit}"}        <h2>{title}</h2>        {pre_body} {/exp:channel:entries} {gv_footer}
  54. Segment vars <html> <head>    <title>{gv_{segment_1}_title}</title> </head> <body> {preload_replace:pre_limit="1"} {preload_replace:pre_body="{cf_summary}"}

    {exp:channel:entries  channel="{segment_1}"    limit="{pre_limit}"}        <h2>{title}</h2>        {pre_body} {/exp:channel:entries} {gv_footer}
  55. <html> <head>    <title>{gv_news_title}</title> </head> <body> {preload_replace:pre_limit="1"} {preload_replace:pre_body="{cf_summary}"} {exp:channel:entries  channel="news"

       limit="{pre_limit}"}        <h2>{title}</h2>        {pre_body} {/exp:channel:entries} {gv_footer}
  56. Preload Replace vars <html> <head>    <title>{gv_news_title}</title> </head> <body> {preload_replace:pre_limit="1"}

    {preload_replace:pre_body="{cf_summary}"} {exp:channel:entries  channel="news"    limit="{pre_limit}"}        <h2>{title}</h2>        {pre_body} {/exp:channel:entries} {gv_footer}
  57. <html> <head>    <title>{gv_news_title}</title> </head> <body> {exp:channel:entries  channel="news"  limit="1"}  

     <h2>{title}</h2>    {cf_summary} {/exp:channel:entries} {gv_footer}
  58. Tags & Local vars <html> <head>    <title>{gv_news_title}</title> </head> <body>

    {exp:channel:entries  channel="news"  limit="1"}    <h2>{title}</h2>    {cf_summary} {/exp:channel:entries} {gv_footer}
  59. <html> <head>    <title>{gv_news_title}</title> </head> <body>    <h2>Missing:  Kenny  Meyers</h2>

       <p>Last  seen  at  Pier  39  San  Francisco,          hanging  out  with  some  friends.</p> {gv_footer}
  60. User Defined Global vars <html> <head>    <title>{gv_news_title}</title> </head> <body>

       <h2>Missing:  Kenny  Meyers</h2>    <p>Last  seen  at  Pier  39  San  Francisco,          hanging  out  with  some  friends.</p> {gv_footer}
  61. <html> <head>    <title>The  News</title> </head> <body>    <h2>Missing:  Kenny

     Meyers</h2>    <p>Last  seen  at  Pier  39  San  Francisco,          hanging  out  with  some  friends.</p> <p>&copy;  {site_name}</p> </body> </html>
  62. Standard Global vars <html> <head>    <title>The  News</title> </head> <body>

       <h2>Missing:  Kenny  Meyers</h2>    <p>Last  seen  at  Pier  39  San  Francisco,          hanging  out  with  some  friends.</p> <p>&copy;  {site_name}</p> </body> </html>
  63. <html> <head>    <title>The  News</title> </head> <body>    <h2>Missing:  Kenny

     Meyers</h2>    <p>Last  seen  at  Pier  39  San  Francisco,          hanging  out  with  some  friends.</p> <p>&copy;  EE  Outsider</p> </body> </html>
  64. None
  65. Conditionals

  66. Conditional Types •Simple •Advanced •Special

  67. Simple Conditionals •Only work with early parsed variables * •Only

    work with one operand •Only work without if:else statement •If so, are parsed before tags *) Except Preload Replace variables
  68. Advanced Conditionals •All other conditionals * •Converted to PHP, then

    evaluated •Are parsed after tags *) Except special conditionals
  69. PUT CODE IN CONDITIONALS STILL PARSES ALL THE TAGS

  70. Special Conditionals •Parsed during and by tags •For example: •

    {if no_results} {/if} • {if next_page} {/if} • etc. •But most conditionals in tags are actually advanced conditionals
  71. simple  conditionals {exp:tags}  special  conditionals  {/exp:tags} advanced  conditionals All Conditionals

  72. Conditionals Example 1

  73. {if  segment_3  ==  'by-­‐category'}    {exp:channel:categories  channel="bars"}      

         <a  href="#">{category_name}</a>    {/exp:channel:categories} {if:else}  {exp:channel:entries        channel="bars{if  logged_in}|foos{/if}"}            <h2>{title}</h2>            {if  no_results}Nothing  found!{/if}    {/exp:channel:entries} {/if}
  74. Simple conditionals {if  segment_3  ==  'by-­‐category'}    {exp:channel:categories  channel="bars"}  

             <a  href="#">{category_name}</a>    {/exp:channel:categories} {if:else}  {exp:channel:entries        channel="bars{if  logged_in}|foos{/if}"}            <h2>{title}</h2>            {if  no_results}Nothing  found!{/if}    {/exp:channel:entries} {/if}    
  75. {if  segment_3  ==  'by-­‐category'}    {exp:channel:categories  channel="bars"}      

         <a  href="#">{category_name}</a>    {/exp:channel:categories} {if:else}  {exp:channel:entries        channel="bars{if  logged_in}|foos{/if}"}            <h2>{title}</h2>            {if  no_results}Nothing  found!{/if}    {/exp:channel:entries} {/if}
  76. Tags & Special conditionals {if  segment_3  ==  'by-­‐category'}    {exp:channel:categories

     channel="bars"}            <a  href="#">{category_name}</a>    {/exp:channel:categories} {if:else}  {exp:channel:entries        channel="bars{if  logged_in}|foos{/if}"}            <h2>{title}</h2>            {if  no_results}Nothing  found!{/if}    {/exp:channel:entries} {/if}
  77. {if  segment_3  ==  'by-­‐category'}            <a

     href="#">Brooklyn</a>            <a  href="#">Manhattan</a>            <a  href="#">Queens</a> {if:else}            Nothing  found! {/if}
  78. Advanced conditionals {if  segment_3  ==  'by-­‐category'}        

       <a  href="#">Brooklyn</a>            <a  href="#">Manhattan</a>            <a  href="#">Queens</a> {if:else}            Nothing  found! {/if}
  79.            Nothing  found!

  80. Conditionals Example 2

  81. {if  logged_in}    {exp:channel:entries  channel="bars"        {if  segment_3

     !=  'all'}limit="2"{/if}}            <h2>{title}</h2>            {if  cf_bar_is_awesome  ==  'y'}                This  bar  is  awesome!            {/if}    {/exp:channel:entries} {/if} {if  sn_show_foos  ==  'y'}    {exp:channel:entries  channel="foos"}        <h3>{title}</h3>        {if  no_results}No  foos  found!{/if}    {/exp:channel:entries} {/if}
  82. Simple conditionals {if  logged_in}    {exp:channel:entries  channel="bars"      

     {if  segment_3  !=  'all'}limit="2"{/if}}            <h2>{title}</h2>            {if  cf_bar_is_awesome  ==  'y'}                This  bar  is  awesome!            {/if}    {/exp:channel:entries} {/if} {if  sn_show_foos  ==  'y'}    {exp:channel:entries  channel="foos"}        <h3>{title}</h3>        {if  no_results}No  foos  found!{/if}    {/exp:channel:entries} {/if}
  83. {if  logged_in}    {exp:channel:entries  channel="bars"        limit="2"}  

             <h2>{title}</h2>            {if  cf_bar_is_awesome  ==  'y'}                This  bar  is  awesome!            {/if}    {/exp:channel:entries} {/if}    {exp:channel:entries  channel="foos"}        <h3>{title}</h3>        {if  no_results}No  foos  found!{/if}    {/exp:channel:entries}
  84. Tags & Special conditionals {if  logged_in}    {exp:channel:entries  channel="bars"  

         limit="2"}            <h2>{title}</h2>            {if  cf_bar_is_awesome  ==  'y'}                This  bar  is  awesome!            {/if}    {/exp:channel:entries} {/if}    {exp:channel:entries  channel="foos"}        <h3>{title}</h3>        {if  no_results}No  foos  found!{/if}    {/exp:channel:entries}
  85. {if  logged_in}            <h2>Bar  One</h2>  

             {if  'n'  ==  'y'}                This  bar  is  awesome!            {/if}            <h2>Bar  Two</h2>            {if  'y'  ==  'y'}                This  bar  is  awesome!            {/if} {/if}        <h3>Foo  One</h3>        <h3>Foo  Two</h3>        <h3>Foo  Three</h3>
  86. Advanced conditionals {if  logged_in}            <h2>Bar

     One</h2>            {if  'n'  ==  'y'}                This  bar  is  awesome!            {/if}            <h2>Bar  Two</h2>            {if  'y'  ==  'y'}                This  bar  is  awesome!            {/if} {/if}        <h3>Foo  One</h3>        <h3>Foo  Two</h3>        <h3>Foo  Three</h3>
  87.            <h2>Bar  One</h2>      

         <h2>Bar  Two</h2>                This  bar  is  awesome!        <h3>Foo  One</h3>        <h3>Foo  Two</h3>        <h3>Foo  Three</h3>
  88. Tags

  89. Tag Types •Module or Plugin •Single or Pair

  90. NOT SURE IF THAT'S A MODULE TAG OR A PLUGIN

    TAG
  91. Module Tags •Parsed from the document root inwards •Repeated until

    no tags are left
  92. Plugin Tags •Look inside for other tags first •If found,

    that tag is parsed first •Except if it's a plugin tag, then see previous •Cancel this behaviour with parse="inward"
  93. Tags Example 1

  94. {exp:channel:entries  channel="feeds"  limit="3"}    <h2>{title}</h2>    {exp:magpie  url="{feed_url}"}    

       {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie} {/exp:channel:entries}
  95. {exp:channel:entries  channel="feeds"  limit="3"}    <h2>{title}</h2>    {exp:magpie  url="{feed_url}"}    

       {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie} {/exp:channel:entries}
  96.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"}        {items}

               <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"}        {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}
  97.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"}        {items}

               <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"}        {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}
  98.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"}        {items}

               <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"}        {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}
  99.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"}        {items}

               <h3>{magpie:title}</h3>            {magpie:content}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"}        {items}            <h3>{magpie:title}</h3>            {magpie:content}        {/items}    {/exp:magpie}
  100.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"}        {items}

               <h3>{magpie:title}</h3>            {magpie:content}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"}        {items}            <h3>{magpie:title}</h3>            {magpie:content}        {/items}    {/exp:magpie}
  101.    <h2>First  title</h2>            <h3>Title  1.1</h3>

               <p>Lorem  ipsum  <a  href="#">dolor</a>  sit  amet</p>            <h3>Title  1.2</h3>            <p>The  lazy  dog  jumped  over  the  <em>fox</em></p>    <h2>Second  title</h2>            <h3>Title  2.1</h3>            <embeds  and  malicious  code>            <h3>Title  2.2</h3>            <blinks  and  marquees>
  102. Tags Example 2

  103. {exp:channel:entries  channel="feeds"  limit="3"}    <h2>{title}</h2>    {exp:magpie  url="{feed_url}"  parse="inward"}  

         {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie} {/exp:channel:entries}
  104. {exp:channel:entries  channel="feeds"  limit="3"}    <h2>{title}</h2>    {exp:magpie  url="{feed_url}"  parse="inward"}  

         {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie} {/exp:channel:entries}
  105.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"  parse="inward"}      

     {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"  parse="inward"}        {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}
  106.    <h2>First  title</h2>    {exp:magpie  url="http://one.com/rss"  parse="inward"}      

     {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}    <h2>Second  title</h2>    {exp:magpie  url="http://two.com/rss"  parse="inward"}        {items}            <h3>{magpie:title}</h3>            {exp:strip_html}{magpie:content}{/exp:strip_html}        {/items}    {/exp:magpie}
  107.    <h2>First  title</h2>            <h3>Title  1.1</h3>

               {exp:strip_html}<p>Lorem  ipsum  <a  href="#">            dolor</a>  sit  amet</p>{/exp:strip_html}            <h3>Title  1.2</h3>            {exp:strip_html}<p>The  lazy  dog  jumped  over            the  <em>fox</em></p>{/exp:strip_html}    <h2>Second  title</h2>            <h3>Title  2.1</h3>            {exp:strip_html}<embeds>{/exp:strip_html}            <h3>Title  2.2</h3>            {exp:strip_html}<marquees>{/exp:strip_html}
  108.    <h2>First  title</h2>            <h3>Title  1.1</h3>

               {exp:strip_html}<p>Lorem  ipsum  <a  href="#">            dolor</a>  sit  amet</p>{/exp:strip_html}            <h3>Title  1.2</h3>            {exp:strip_html}<p>The  lazy  dog  jumped  over            the  <em>fox</em></p>{/exp:strip_html}    <h2>Second  title</h2>            <h3>Title  2.1</h3>            {exp:strip_html}<embeds>{/exp:strip_html}            <h3>Title  2.2</h3>            {exp:strip_html}<marquees>{/exp:strip_html}
  109.    <h2>First  title</h2>            <h3>Title  1.1</h3>

               Lorem  ipsum  dolor  sit  amet            <h3>Title  1.2</h3>            The  lazy  dog  jumped  over  the  fox    <h2>Second  title</h2>            <h3>Title  2.1</h3>            ...            <h3>Title  2.2</h3>            ...
  110. A Parse Order Pro •...can identify markers •...knows when they

    are parsed •...knows how to nest them •...and will always...
  111. PARSE ALL THE THINGS! gotolow.com/parse-order

  112. Questions?

  113. Thank You! gotolow.com @low