twig
#twig 1
1: 2
2
Examples 2
API 2
? 3
3
2: 5
5
Examples 5
C 5
3: 6
6
Examples 6
6
6
6
6
7
For Loop 7
(If-Then-Else ) Null-Coalescing 8
( ?: :) 8 null ( ??: :) 8
9
4: 10
10
Examples 10
/ 10
Twig_Extension 10
11
5: 12 Examples 12
12
base.twig.html 12
12
article.twig.html 12
articles.twig.html 13
13
parent.html.twig 13 child.html.twig 13
, , , , 13
13
14
14
15
15
17 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: twig
It is an unofficial and free twig ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official twig.
The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ko/home 1 1:
, .
. .
Examples
API
. .
require '/path/to/lib/Twig/Autoloader.php'; Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$options = array( 'strict_variables' => false, 'debug' => false, 'cache'=> false );
$twig = new Twig_Environment($loader, $options);
Twig_Environment . .
• debug ( boolean , false )
true __toString() .
• charset ( , utf-8 )
.
• base_template_class ( , Twig_Template )
.
• cache ( string false , false )
false ().
• auto_reload ( boolean , : debug )
Twig . auto_reload .
• strict_variables ( boolean , false )
false , Twig ( / / ) null . true Twig throw.
https://riptutorial.com/ko/home 2 • autoescape ( string boolean , true )
true HTML .
Twig 1.8 (html, js, false ) .
Twig 1.9 (css, url, html_attr "filename" PHP - ).
Twig 1.17 ( ).
• ( , -1 )
. set to -1 to enabled all optimalizations set o 0 to disable all optimalitazations
Twig PHP (C ) , PHP .
?
PHP . HTML . PHP .
:
• HTML (XSS ) • ( ) • •
:
{% extends "base.html" %}
{% block sidebar %} {{ parent() }} Sidebar content specific to this page {% endblock sidebar %}
{% block body %}
Select an item:
- {% for item in list %}
- {{ item.name }} {% else %}
- No items yet. {% endfor %}
Smarty , Django Jinja Twig . PHP .
.
• Fast : Twig PHP . PHP . https://riptutorial.com/ko/home 3 • : . Twig .
• : . DSL .
Twig Symfony , Drupal , eZPublish Slim , Yii , Laravel , Codeigniter , silex Kohana .
Twig Composer .
PHP 5.x
composer require "twig/twig:~1.0"
PHP 7.x
composer require "twig/twig:~2.0"
: https://riptutorial.com/ko/twig/topic/1100/--
https://riptutorial.com/ko/home 4 2:
Twig PHP7 1.x . Twig 2.x C 2.1 . PHP 7 C .
Examples
C
C Twig . ext/twig Twig . PHP :
cd ext/twig phpize ./configure make make install php.ini . , Twig .
: https://riptutorial.com/ko/twig/topic/8696/-
https://riptutorial.com/ko/home 5 3:
Examples
Twig {{ variableName }} .
Hello {{ name }}
Twig . PHP {{ array[key] }} . array
Hello {{ user['name'] }}
. 'Dot'(.) {{ object.propertyName }} . object
Hello {{ user.name }}
. | :
{{ variable|filterName }}
.
{{ variable|upper }}
. (,) .
{{ variable|filterName(param1, param2, ...) }} round round 2 . ( : 0), ( : ) . https://riptutorial.com/ko/home 6 1 {{ number|round(1, 'common') }} {{ number|round(1) }} .
.
{{ array['key'] | upper }} {{ object.text | upper }}
.
{% set array = "3,1,2"|split(',') %}
{{ array | sort | first }}
.
. If . if . true / .
{% if enabled == false %} Disabled {% endif %} enabled false Disabled . elseif else .
{% if temperature < 10 %} It's cold {% elseif temperature < 18 %} It's chilly {% elseif temperature < 24 %} It's warm {% elseif temperature < 32 %} It's hot {% else %} It's very hot {% endif %}
For Loop
For TWIG .
.
{% set array = "3,1,2" %}
. . 3 h1 .
{% for current in array %}
This is number {{ current }} in the array
{% endear %}{{ current }} array . https://riptutorial.com/ko/home 7 : https://twigfiddle.com/mxwkea/2
. , getter setter 'name' Entity . TWIG .
{% for currentObject in ArrayOfObjects %} {{ currentObject.name }} {% endfor %}
JSON : https://twigfiddle.com/mxwkea .
(If-Then-Else ) Null-Coalescing
( ?: :)
Twig 1.12.0 .
{{ foo ? 'yes' : 'no' }}
:
if foo echo yes else echo no
{{ foo ?: 'no' }}
{{ foo ? foo : 'no' }}
:
foo , no
{{ foo ? 'yes' }}
{{ foo ? 'yes' : '' }}
:
if foo echo yes else echo .
null ( ??: :)
{{ foo ?? 'no' }}
: https://riptutorial.com/ko/home 8 foo no .
HTML (, , ...) spaceless .
{% spaceless %}
(-) . :
{% set value = 'foo bar' %}
{{- value }} {# produces 'foo bar ' #}
{{ value -}} {# produces ' foo bar' #}
{{- value -}} {# produces 'foo bar' #}
{# produces '' #}
{# produces '' #}
: https://riptutorial.com/ko/twig/topic/8697/--
https://riptutorial.com/ko/home 9 4:
Twig PHP ?
Examples
/
/ twig . Twig_Function Twig_Function Twig_Filter Twig_Filter , .
/* You can chain a global function */ $twig->addFilter(new Twig_SimpleFilter('floor', 'floor'));
/* You can specify a custom function */ $twig->addFilter(new Twig_SimpleFilter('money', function($value, $currency, $prefix = false, $decimals = 2, $dec_point = "." , $thousands_sep = ",") { $value = number_format($value, $decimals, $dec_point, $thousands_sep); if ($prefix) return $currency.' '.$value; return $value.' '.$prefix; });
/* You can chain an object's method */ $twig->addFilter(new Twig_SimpleFilter('foo_bar', array($foo, 'bar')));
Twig_Extension
/ filters / tests / ... Twig_Extension .
ProjectTwigExtension
class ProjectTwigExtension extends Twig_Extension {
public function getFunctions() { return array( new Twig_SimpleFunction('twig_function_name', array($this, 'getTwigFunctionName')), new Twig_SimpleFunction('twig_function_foo', array($this, 'getTwigFunctionFoo')),
);
} public function getFilters() { return array( new Twig_SimpleFilter('twig_filter_name' , array($this, 'getTwigFilterName')), new Twig_SimpleFilter('twig_filter_foo' , array($this, 'getTwigFilterFoo')), ); } https://riptutorial.com/ko/home 10
public function getName() { return 'ProjectTwigExtension'; } }
$twig = new Twig_Environment($loader); $twig->addExtension(new ProjectTwigExtension());
.
...
1 -
use \Twig_Extension
class dobToAge extends \Twig_Extension {
2 - getFilters ()
public function getFilters() { return array( 'age' => new \Twig_Filter_Method($this, 'getAge'), ); }
3 - .
public function getAge($date) { if (!$date instanceof \DateTime) { // turn $date into a valid \DateTime object or let return return null; }
$referenceDate = date('01-01-Y'); $referenceDateTimeObject = new \DateTime($referenceDate); $diff = $referenceDateTimeObject->diff($date); return $diff->y; } }
.
{{ yourDateOfBirthInstance | age }}
: https://riptutorial.com/ko/twig/topic/4923/--
https://riptutorial.com/ko/home 11 5:
Examples base.twig.html
Lorem ipsum dolor sit amet.
{% extends base.twig.html %} {% block navigation %} Page 2 Page 3 Page 4 {% endblock %} {% block content %} This is my first page {% endblock content %} article.twig.html
{{ article.content }}{{ article.title }}
{% block content %}
{# use default template for article #} {% for article in articles %} {% include "article.twig.html" %} {% endfor %}
{# use custom template for article #} {% for article in articles %} {% embed "article.twig.html" %} {% block content %} {{ parent() }} {% endblock %} {% endembed %} {% endfor %} parent.html.twig
This is the {{ title_variable }} Page
child.html.twig{% extends "parent.html.twig" %} {% set title_variable = "Child" %}
, , , ,
Twig .
. base.html.twig header.html.twig footer.html.twig .
https://riptutorial.com/ko/home 13 header.html.twig
base.html.twig
{% include 'header.html.twig' %}
. homepage.html.twig about.html.twig base.html.twig base.html.twig . base.html.twig
{% include 'header.html.twig' %}
{% extends 'base.html.twig' %}
{% block main %}
You are at the homepage
{% endblock %} about.html.twig{% extends 'base.html.twig' %}
{% block main %}
You are at the about page
{% endblock %}. single.product.html.twig (extends product.layout.html.twig ) single.service.html.twig ( 'service.layout.html.page') sidebar.product.html.twig . ( ) sidebar.html.twig
single.product.html.twig
{% extends 'product.layout.html.twig' %} https://riptutorial.com/ko/home 14
{% use 'sidebar.html.twig' %} {% block main %}
You are at the product page for product number 123
{% endblock %} single.service.html.twig{% extends 'service.layout.html.twig' %}
{% use 'sidebar.html.twig' %} {% block main %}
You are at the service page for service number 456
{% endblock %}. . form.html.twig
{{{name}} "value ="{{value | e}} "{input} = {{ (, , )}} "}}"/> {% endmacro %} profile.service.html.twig
{% import "forms.html.twig" as forms %}
. Use Include . product.table.html.twig service.table.html.twig pagination.html.twig product.table.html.twig . pagination.html.twig
{% set min, max = 1, products.itemPerPage %}
{% embed 'pagination.html.twig' %} {% block first %}First Product Page{% endblock %} {% block last %}Last Product Page{% endblock %} {% endembed %} https://riptutorial.com/ko/home 15 service.table.html.twig
{% set min, max = 1, services.itemPerPage %}
{% embed 'pagination.html.twig' %} {% block first %}First Service Page{% endblock %} {% block last %}Last Service Page{% endblock %} {% endembed %}
( pagination.html.twig ) ( min , max ) . . pagination.html.twig
{{ count }} items
{% set min, max = 1, products|length %}
{% embed 'pagination.html.twig' with {'count': products|length } %} {% block first %}First Product Page{% endblock %} {% block last %}Last Product Page{% endblock %} {% endembed %}
: https://riptutorial.com/ko/twig/topic/2922/-
https://riptutorial.com/ko/home 16 S. Contributors No
4444, Community, DarkBee, Icode4food, insertusernamehere, 1 mleko, Object, Paulpro, user1950896
2 Icode4food, mleko, Ricardo Velhote
3 jkucharovic, mleko, PastyAndPeas, Trix
4 DarkBee, Object
5 DarkBee, Ḟḹáḿíṅḡ ỏḿƀíé, nicolallias, PastyAndPeas, Trix
https://riptutorial.com/ko/home 17