Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP in Templates: Pain or Pleasure

PHP in Templates: Pain or Pleasure

The pros and cons of using PHP in ExpressionEngine templates.

Lodewijk Schutte

June 02, 2010
Tweet

More Decks by Lodewijk Schutte

Other Decks in Technology

Transcript

  1. ExpressionEngine Templates - CSS, JavaScript, RSS Page, Static, XML and

    Web Page - Not just for presentation, but contain logic - Are parsed by EE’s Template Parser in a certain order
  2. EE’s Parse 1.Snippets, global-, segment- and embed variables 2.PHP on

    Input 3.Segment-, embed- & global simple conditionals 4.Preload replace variables 5.Module and plugin tags 6.PHP on Output 7.Advanced conditionals 8.Embedded templates 9.Other variables
  3. {exp:channel:entries   channel="news"   status="open{if  segment_2  ==  'all'}|featured{/if}"   dynamic="no"}

      <!-­‐-­‐  News  items  -­‐-­‐> {/exp:channel:entries} Will work:
  4. {exp:channel:entries   channel="news"   status="open{if  member_group  ==  '5'}|featured{/if}"   dynamic="no"}

      <!-­‐-­‐  News  items  -­‐-­‐> {/exp:channel:entries} Won’t work:
  5. #5: exp-tag processing - Tags at root level are parsed,

    rinse, repeat - Plugin tags look inside for other tags and parse those first, recursively - This order can be reversed with parse="inward" on any plugin tag
  6. Low Variables example Name Type Early parsing {show_warning} Checkbox Yes

    {warning_message} Text input No {warning_level} Radio group No
  7. Low Variables example {if  show_warning  ==  'y'} <div  class="warning  level-­‐{warning_level}">

    <h2>Warning!</h2> {exp:textile}      {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div> {/if}
  8. Low Variables example {if  show_warning  ==  'y'} <div  class="warning  level-­‐{warning_level}">

    <h2>Warning!</h2> {exp:textile}      {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div> {/if}
  9. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

     {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div>
  10. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

     {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div>
  11. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

     {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div>
  12. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

     Look  behind  you,  a  {warning_level}-­‐headed  monkey! {/exp:textile} </div>
  13. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

     Look  behind  you,  a  {warning_level}-­‐headed  monkey! {/exp:textile} </div>
  14. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> <p>    

     Look  behind  you,  a  {warning_level}-­‐headed  monkey! </p> </div>
  15. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> <p>    

     Look  behind  you,  a  {warning_level}-­‐headed  monkey! </p> </div>
  16. Low Variables example <div  class="warning  level-­‐three"> <h2>Warning!</h2> <p>    

     Look  behind  you,  a  three-­‐headed  monkey! </p> </div>
  17. Low Variables For EE 1.6.8+ and EE 2.0.2+ Get it

    now at loweblog.com or devot-ee.com
  18. PHP

  19. Why (not) use PHP? Pros Cons Increase performance Possible security

    issues Extend EE without add-ons Specific customization More control and flexibility
  20. Some best practices - Save templates as files, use editor

    - Logic first, parse variables later - Use alternative syntax - Never assume
  21. - Idea: better advanced conditionals - Problem: EE still parses

    code in {if  ...}  {if:else}   {/if} conditionals, which impacts performance - Solution: use PHP on input to handle conditionals, thus only generating EE code that should be parsed Example #1
  22. - Idea: fully hackable URL - Problem: superfluous segment, the

    template name - Solution: use PHP in index to select the right embed Example #2
  23. - Idea: show channel entries with title beginning with X

    - Problem: {exp:channel:entries} tag cannot do this out of the box - Solution: use PHP on input to get entry_ids and feed those to the {exp:channel:entries} tag Example #3
  24. - Idea: track where 404s are coming from - Problem:

    cannot do this out of the box or without third party software - Solution: hijack Referrer module and use PHP in the 404 template to add records to the database Example #4
  25. - Idea: show banners in between blog posts (post, banner,

    post, post, banner, post, post, banner...) - Problem: cannot do this elegantly out of the box - Solution: use PHP on output to store banners in array and display them at the right position Example #5
  26. - Idea: customized RSS feed - Problem: cumbersome if not

    impossible out of the box - Solution part 1: use PHP on input to catch form and generate a custom URL - Solution part 2: use PHP on input to read segments and set parameters in RSS feed Example #6