twig templating

49
Twig Templating John Rudolph Bautista October 26th 2013 The Twig logo is © Sensio Labs Software Engineer

Upload: rj-bautista

Post on 11-May-2015

1.492 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Twig Templating

Twig Templating

John Rudolph Bautista

October 26th 2013The Twig logo is © Sensio Labs

Software Engineer

Page 2: Twig Templating

PHP AS TEMPLATE

Page 3: Twig Templating

Why TWIGWAS

born?

Page 4: Twig Templating

WAYBACK 2009

Page 5: Twig Templating

FABIEN potencier

- Symfony- SwiftMailer- Silex- Pimple- Twig

…. many morehttps://github.com/fabpot

Page 6: Twig Templating

TWIGThe Twig logo is © Sensio Labs

Page 7: Twig Templating

Twig

- Provides Pattern Consistency- Solves Complex Control Flow- Template Inheritance & Macros- Fully Extendable

Page 8: Twig Templating

Twig

- Well Documented- Unit Tested- Secure- Fast (has a caching layer by default)

Page 9: Twig Templating

Syntax Highlighting

Page 10: Twig Templating

Installation

Page 11: Twig Templating

VIa Composer

Page 12: Twig Templating

VIA Composer

http://getcomposer.org/

Page 13: Twig Templating

https://github.com/fabpot/Twig/tags

- Unpack- Move files to your project folder

VIA TARBALL OR ZIP

Page 14: Twig Templating

https://github.com/fabpot/Twig/tags

git clone git://github.com/fabpot/Twig.git

VIA GIT

Page 15: Twig Templating

USING TWIG API

Page 16: Twig Templating

Delimiters

{{ ... }}

{% ... %}

{# ... #}

- displays something

- evaluates an expression (loops, tags, cond.)

- comments code blocks

Page 17: Twig Templating

Twig Template FILE

Page 18: Twig Templating

Twig Template FILE ← Comment

Page 19: Twig Templating

Twig Template FILE

← FILTER

Page 20: Twig Templating

Twig Template FILE

← TAG

Page 21: Twig Templating

Twig Template FILE

← VARIABLE

Page 22: Twig Templating

Twig Template FILE

← FILTER WITH ARGUments

Page 23: Twig Templating

THE BASICS

Page 24: Twig Templating

LITERALS

String = “Manong Johny”Integer = 100000000Float = 100000000.00Array = [“pdaf”, “dap”]Hash = {“key” : “value”}Boolean = true/falseNull = null

Page 25: Twig Templating

SETTING VARIABLES

{% set str = 'value' %}

{% set hash = {'a' : 1, 'b': 2 } %}

{% set arr = [1, 2, 3] %}

{% set combine = ['a' : { 'b': 2 }] %}

Page 26: Twig Templating

PRINTING VARIABLES

{{ var }}

{{ var.elem }}

{{ var.prop }}

{{ var['elem'] }}

Page 27: Twig Templating

FILTERS

- Modifies a variable- Uses “|” (pipe)- Chainable- Can accept arguments

Page 28: Twig Templating

FILTERS

{{ employee|title }}

{{ tags|join(', ') }}

Result : “Janet Napoles”

Result : “ph, rich, more fun”

Page 29: Twig Templating

MACROS

- acts like functions in regular programming language- default argument values are defined by using the default() filter- arguments are always optional

Page 30: Twig Templating

MACROS

Page 31: Twig Templating

ESCAPING

- escapes a string for safe insertion into the final output

{{ var|escape }}{{ var|e }}{% autoescape %}

….{% endautoescape %}

Page 32: Twig Templating

ESCAPING

Default Escape : {{ str|escape }}<p>Hello World!</p>

JS Escape : {{ str|escape('js ') }}\x3Cp\x3EHello\x20World\x21\x3C\x2Fp\x3E

CSS Escape : {{ str|escape('css ') }}\3C p\3E Hello\20 World\21 \3C \2F p\3E

URL Escape: {{ str|escape('url' ) }}%3Cp%3EHello%20World%21%3C%2Fp%3E

Page 33: Twig Templating

IF, ELSEIF, ELSE

Page 34: Twig Templating

FOR LOOP

- loop each item in a sequence

Page 35: Twig Templating

FOR LOOP

Page 36: Twig Templating

FOR WITH CONDITION

Page 37: Twig Templating

FOR LOOP VARIABLES

Page 38: Twig Templating

TEMPLATEINHERITANCE

Page 39: Twig Templating

BLOCK

- are used for inheritance and act as placeholders and replacements.

{% block content %}…..

{% endblock %}

Page 40: Twig Templating

NAMED BLOCK END-TAGS

Page 41: Twig Templating

EXTENDS

- can be used to extend a template from another one.

{% extends “layout.twig” %}

Page 42: Twig Templating

EXTENDS & BLOCK

Page 43: Twig Templating

EXTENDS & BLOCK

Page 44: Twig Templating

EXTENDS & BLOCK

{% block content %}

{% endblock content %}

Page 45: Twig Templating

INCLUDE

- includes a template and return the rendered content of that file into the current namespace.

Page 46: Twig Templating

INCLUDE

Page 47: Twig Templating

INCLUDE

Page 48: Twig Templating

TWIG

DRUPAL +

Drupal 8 & Twig Road Map : http://lb.cm/twig

Page 49: Twig Templating

<THANK YOU!