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.

A347386377e120e3230403d2558c1a0d?s=128

Lodewijk Schutte

June 02, 2010
Tweet

Transcript

  1. Probing ExpressionEngine’s Parse Order EECI2010, June 2nd, San Francisco, USA

    PHP in templates: Pain or Pleasure?
  2. Hi! I’m Low! Lodewijk Schutte @low

  3. 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
  4. None
  5. 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
  6. {exp:channel:entries   channel="news"   status="open{if  segment_2  ==  'all'}|featured{/if}"   dynamic="no"}

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

      <!-­‐-­‐  News  items  -­‐-­‐> {/exp:channel:entries} Won’t work:
  8. #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
  9. Low Variables Parse at stage #1, #5 or #9. You

    choose!
  10. Low Variables example Name Type Early parsing {show_warning} Checkbox Yes

    {warning_message} Text input No {warning_level} Radio group No
  11. Low Variables example

  12. 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}
  13. 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}
  14. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> {exp:textile}    

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

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

     {exp:low_variables:parse  var="warning_message"} {/exp:textile} </div>
  17. 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>
  18. 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>
  19. Low Variables example <div  class="warning  level-­‐{warning_level}"> <h2>Warning!</h2> <p>    

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

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

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

    now at loweblog.com or devot-ee.com
  23. embed embed segment variables exp-tags advanced conditional s user defined

    global variables
  24. PHP

  25. Input Output {exp} $php <html> {exp} $php <html>

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

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

    - Logic first, parse variables later - Use alternative syntax - Never assume
  28. - 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
  29. None
  30. - Idea: fully hackable URL - Problem: superfluous segment, the

    template name - Solution: use PHP in index to select the right embed Example #2
  31. None
  32. - 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
  33. None
  34. - 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
  35. None
  36. - 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
  37. None
  38. - 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
  39. None
  40. None
  41. Questions?

  42. http://www.flickr.com/photos/thebees/3412843560/ http://www.flickr.com/photos/arne-list/2934797540/ http://www.flickr.com/photos/abennett96/3625684918/ http://www.flickr.com/photos/goopymart/291244864/ Look for slides at slideshare.net/lodewijkschutte