Enough about Classes, Let's Talk Templates

Enough about Classes, Let's Talk Templates

6074dbe5dfa361c9888a705f40a49af4?s=128

Jonathan Reinink

May 20, 2015
Tweet

Transcript

  1. 1.
  2. 2.

    Jonathan Reinink Software developer from Canada Been writing PHP for

    about 15 years Marketing agency for over a decade Started contract development this year Leadership group of The PHP League Wrote the Plates template library Wrote the Glide image library I <3 open source
  3. 6.
  4. 12.

    “This blog post is not for the faint- hearted! Some

    people will strongly disagree with me and some others will probably want to kill me at the upcoming Zend Conference.” —Fabien Potencier
  5. 13.

    “I'm not suggesting that native PHP templates are better than

    compiled templates (like Twig).” —Me
  6. 14.

    “oh... puts the pitchfork down” —Friendly Reddit reader “hides torch

    behind back…” —Another friendly Reddit reader
  7. 17.

    <html> ! <h1>Hello, <?=$name?></h1> ! <ul> <?php if ($stmt =

    $mysqli->prepare("SELECT name FROM friends")) { $stmt->execute(); $stmt->bind_result($name); ! while ($stmt->fetch()) { echo '<li>'.$name.'</li>'; } } ?> </ul>
  8. 19.

    While PHP has evolved into a mature, object oriented language,

    it hasn’t improved much as a templating language.
  9. 20.
  10. 21.

    {% extends "template.html" %} ! {% block title %}User Profile{%

    endblock %} ! {% block content %} <h1>User Profile</h1> <p>Hello, {{ name }}</p> {% endblock %}
  11. 23.

    You can write good template code with most modern templating

    libraries. Twig, Blade, Smarty, Moustache, Plates, Aura.View, Zend\View, etc…
  12. 25.

    Choose a templating language with an easy learning curve. Easy

    for both server-side developers and front-end developers.
  13. 27.

    <?php if ($items): ?> <?php foreach ($items as $item): ?>

    <li><?php echo $item ?></li> <?php endforeach ?> <?php else: ?> <li>No items found.</li> <?php endif ?> {% for item in items %} <li>{{ item }}</li> {% else %} <li>No items found.</li> {% endfor %} Native PHP Twig
  14. 30.

    Never use blocks of PHP. <?php foreach ($users as $user)

    { echo $user->name; } ?> <?php foreach ($users as $user): ?> <?=$user->name?> <?php endforeach ?>
  15. 31.

    Only one statement per PHP tag. <?php if ($users) {

    foreach ($users as $user) { ?> <?=$user->name?> <?php } } ?> <?php if ($users): ?> <?php foreach ($users as $user): ?> <?=$user->name?> <?php endforeach ?> <?php endif ?>
  16. 32.

    Never use curly brackets. <?php foreach ($users as $user) {

    ?> <?=$user->name?> <?php } ?> <?php foreach ($users as $user): ?> <?=$user->name?> <?php endforeach ?>
  17. 34.

    <?php $this->layout('template', ['title' => 'User Profile']) ?> ! <h1>Hello <?=$this->e($name)?></h1>

    ! <h2>Friends</h2> <ul> <?php foreach($friends as $friend): ?> <li> <a href="/profile/<?=$this->e($friend->id)?>"> <?=$this->e($friend->name)?> </a> </li> <?php endforeach ?> </ul> ! <?php if ($invitations): ?> <h2>Invitations</h2> <p>You have some friend invites!</p> <?php endif ?>
  18. 36.

    <div class="item" tal:repeat="value values"> <div class="title"> <span tal:condition="value/hasDate" tal:replace="value/getDate"/> <a

    tal:attributes="href value/getUrl" tal:content="value/getTitle"/> </div> <div id="content" tal:content="value/getContent"/> </div>
  19. 46.

    1. HTML escaping 2. HTML attribute escaping 3. CSS escaping

    4. JavaScript escaping 5. URL escaping Be aware of the different types of escaping:
  20. 49.

    Without automatic escaping, you MUST manually escape every single variable.

    And you will probably forget once in a while!!!
  21. 54.

    class Template { protected $values; ! public function __get($name) {

    return $this->escape( $this->values[$name] ); } }
  22. 57.

    class Template { protected $values; ! public function __get($name) {

    return new TemplateProxy( $this->values[$name] ); } }
  23. 58.

    class TemplateProxy { protected $value; ! public function __get($name) {

    return new TemplateProxy($this->name); } ! public function __call($name, $args) { return new TemplateProxy(...); } ! public function __toString() { return $this->escape($this->value); } }
  24. 60.
  25. 64.
  26. 69.

    <ul> {% for item in items %} <li>{{ item }}</li>

    {% else %} <li>No items found.</li> {% endfor %} </ul>
  27. 76.

    So what exactly is basic formatting then? • Date formatting

    • Case changes • String concatenation • String trimming • Number formatting • URL encoding
  28. 79.

    namespace App\ViewModels; ! class UsersViewModel { public function users() {

    return [ ['name' => 'Jonathan'], ['name' => 'Amy'], ['name' => 'Joey'], ]; } }
  29. 91.

    Rule #4: Controllers can instantiate only one object. Therefore, views

    can only know about one instance variable and views should only send messages to that object. — Sandi Metz, rules for developers
  30. 95.

    {% include 'partials/header.html' with {'title': 'Home'} %} ! <p>Your page

    content.</p> ! {% include 'partials/footer.html' %}
  31. 97.

    <select> <?php foreach ($users as $user): ?> <option <?php if

    ($current_user === $user) echo 'selected' ?> value="<?=$user->id?>"> <?=$user->name?> </option> <?php endforeach ?> </select>
  32. 98.

    <select> <?php foreach ($users as $user): ?> <?php if ($current_user

    === $user): ?> <option selected value="<?=$user->id?>"><?=$user->name?></option> <?php else ?> <option value="<?=$user->id?>"><?=$user->name?></option> <?php endif ?> <?php endforeach ?> </select>
  33. 100.

    template.html {% extends "template.html" %} ! {% block title %}Profile{%

    endblock %} {% block content %} <h1>Profile</h1> <p>Hello, {{ name }}</p> {% endblock %} profile.html <html> <head> <title> {% block title %} Default title {% endblock %} </title> </head> <body> ! <main> {% block content %} <p>Default content</p> {% endblock %} </main> ! </body> </html>