Child.Html.Twig 13

Child.Html.Twig 13

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() }} <span>Sidebar content specific to this page</span> {% endblock sidebar %} {% block body %} <p>Select an item:</p> <ul> {% for item in list %} <li><a href="/items/{{ item.id }}">{{ item.name }}</a> {% else %} <li>No items yet. {% endfor %} </ul> {% endblock body %} 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 }} . <!DOCTYPE html> <html> <body> <span>Hello {{ name }}</span> </body> </html> Twig . PHP {{ array[key] }} . array <!DOCTYPE html> <html> <body> <span>Hello {{ user['name'] }}</span> </body> </html> . 'Dot'(.) {{ object.propertyName }} . object <!DOCTYPE html> <html> <body> <span>Hello {{ user.name }}</span> </body> </html> . | : {{ 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 %} <h1>This is number {{ current }} in the array </h1> {% 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 %} <div> <span>foo bar </span> </div> {% endspaceless %} {# produces output <div><strong>foo bar </strong></div> #} (-) . : {% set value = 'foo bar' %} <span> {{- value }} </span> {# produces '<span>foo bar </span>' #} <span> {{ value -}} </span> {# produces '<span> foo bar</span>' #} <span> {{- value -}} </span> {# produces '<span>foo bar</span>' #} <span {%- if true %} class="foo"{% endif %}> {# produces '<span class="foo">' #} <span {%- if false %} class="foo"{% endif %}> {# produces '<span>' #} : 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 , . <?php $twig = new Twig_Environment($loader); /* 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 <!DOCTYPE html> <html> <head> <title>{{ title | default('Hello World') }}</title> <link rel="stylesheet" type="text/css" href="theme.css"> {% block css %} {% endblock %} </head> <body> {% block body %} <nav> {% block navigation %} <a href="#">Link</a> <a href="#">Link</a> <a href="#">Link</a> {% endblock navigation %} </nav> <section id="container"> <section id="content"> {% block content %} <p> Lorem ipsum dolor sit amet. </p> </section> {% endblock content %} </section> {% endblock body %} </body> </html> page.twig.html {% extends base.twig.html %} {% block navigation %} <a href="page2.html">Page 2</a> <a href="page3.html">Page 3</a> <a href="page4.html">Page 4</a> {% endblock %} {% block content %} This is my first page {% endblock content %} article.twig.html <article> https://riptutorial.com/ko/home 12 <h1>{{ article.title }}</h1> {% block content %} <p>{{ article.content }}</p> {% endblock %} </article> articles.twig.html {# 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 %} <img src="{{ article.image }}" alt="{{ article.title }}" /> {{ parent() }} {% endblock %} {% endembed %} {% endfor %} parent.html.twig <!DOCTYPE html> <html> <head> <title>{{ title_variable | default('Normal Page') }}</title> </head> <body> <h1>This is the {{ title_variable }} Page</h1> </body> </html> 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 <nav> <div>Homepage</div> <div>About</div> </nav> base.html.twig {% include 'header.html.twig' %} <main>{% block main %}{% endblock %}</main> . homepage.html.twig about.html.twig base.html.twig base.html.twig . base.html.twig {% include 'header.html.twig' %} <main>{% block main %}{% endblock %}</main> homepage.html.twig

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us