Slide 1

Slide 1 text

Curious about TWIG? Lightning talk Peter Shackelford @pixelplow

Slide 2

Slide 2 text

Twig the flexible, fast, and secure template engine for PHP

Slide 3

Slide 3 text

This is a lightning talk for more check out: Timber: upstatement.com/timber/ github.com/jarednova/timber/wiki and TWIG: twig.sensiolabs.org

Slide 4

Slide 4 text

have_posts() ) : ?> have_posts() ) : $the_query->the_post(); ?>

$args = array( 'numberposts' => 3, 'post_type' => 'post', ); Ye olde standard loop

Slide 5

Slide 5 text

In the next 15 minutes: Syntax Routing/Controller Template inheritance

Slide 6

Slide 6 text

Logic View HTML WP_Query Conditionals Routing .php .twig WP template hierarchy or function.php themename/views

Slide 7

Slide 7 text

Twig defines three types of special syntax: {{ ... }} {% ... %} {# ... #}

Slide 8

Slide 8 text

{{ ... }} "Says something": prints a variable or the result of an expression to the template.

Slide 9

Slide 9 text

{{ post.title }}

{{ post.content }}

Slide 10

Slide 10 text

{% ... %} "Does something": a tag that controls the logic of the template; it is used to execute statements such as for-loops and if statements.

Slide 11

Slide 11 text

{% if not loop.last %} →Stuff happens here {% endif %} {% for post in posts if post.has_term('dairy') %}

This product contains cow water.

{% else %}

I am dairy free!

{% endfor %}

Slide 12

Slide 12 text

{# ... #} "Comment something": it's the equivalent of the PHP /* comment */ syntax The content of the comments isn't included in the rendered pages.

Slide 13

Slide 13 text

Twig also contains 31+ filters, which modify content before being rendered. The following makes the title variable all uppercase before rendering it: {{ title|upper } The slice filter extracts a slice of a sequence, a mapping, or a string. This returns the first four posts: {{ posts|slice(0,3) }}

Slide 14

Slide 14 text

{# Get first two posts in array, print title in title case, if there is a subtitle print the subtitle #} {% for post in posts|slice(0,1) %}

{{ post.title|title }}

{% if post.subtitle %} {{ post.subtitle }} {%endif%} {% endfor %}

Slide 15

Slide 15 text

Controller/Routing: - defines what - how many - where Using WP_Query + Twig

Slide 16

Slide 16 text

Logic View HTML WP_Query Conditionals Routing .php .twig WP template hierarchy or function.php themename/views can be configured

Slide 17

Slide 17 text

index.php // Get standard theme and WP info $context = Timber::get_context(); //Get posts + args $args = array( 'numberposts' => 3, 'post_type' => 'post', ); $context['posts'] =Timber::get_posts($args); //Twig file that will render the data Timber::render('index.twig', $context);

Slide 18

Slide 18 text

have_posts() ) : ?> have_posts() ) : $the_query->the_post(); ?>

IN CONTROLLER: PHP IN TWIG FILE: TWIG $args = array( 'numberposts' => 3, 'post_type' => 'post', ); $context['posts'] = Timber::get_posts($args); {% for post in posts %}

{{ post.title }}

{% endfor %}

Slide 19

Slide 19 text

Template inheritance

Slide 20

Slide 20 text

BASE LAYOUT COMPONENT

Slide 21

Slide 21 text

The extends tag can be used to extend a template from another one. {% extends 'component.twig' %}

Slide 22

Slide 22 text

Blocks are used for inheritance and act as placeholders and replacements at the same time. {% block title %} {% endblock title %}

Slide 23

Slide 23 text

When a template uses inheritance, it's possible to render the contents of the parent block when overriding a block by using the parent function {{ parent() }} Great for prepending or appending to an existing block.

Slide 24

Slide 24 text

component.twig {# A component twig with a thing block and a boondoggle block #} {% block thing %}

This is a great big {{ thing }}.

{% endblock thing %} {% block boondoggle %}

Come get your {{ thing }}.

{% endblock boondoggle %}

Slide 25

Slide 25 text

component-copyright.twig {# gets component twig and appends copyright and date after thing block #} {% extends 'component.twig'%} {% block thing %} {{ parent() }}

copyright {{ "now"|date("Y") }}

{% endblock thing %}

Slide 26

Slide 26 text

base.twig {{ function( 'get_header' ) }}
{% block content %} {% endblock content%}
{{ function( 'get_footer' ) }}

Slide 27

Slide 27 text

main-sidebar.twig {% extends 'base.twig' %} {% block content%} {% embed "article.twig"%} {% endembed "article.twig"%} {% embed "sidebar.twig"%} {% block author%} {% endblock author%} {% endembed "sidebar.twig"%} {% endblock content%}

Slide 28

Slide 28 text

http://twig.sensiolabs.org/ https://wordpress.org/plugins/timber-library/ http://github.com/jarednova/timber/wiki http://wordpress.org/plugins/debug-bar-timber http://www.slideshare.net/javier.eguiluz/twig-tips-and- tricks http://notlaura.com/the-twig-for-timber-cheatsheet http://hugogiraudel.com/2013/11/12/themes-layouts- twig/