PHP and TWIG
Τα templates στο Drupal 8 δημιουργούνται πλέον με τον μηχανισμό TWIG.
Η αντιστοιχία τους φαίνεται παρακάτω:
1. Docblock
PHPTemplate:
<?php
/**
* @file
* File description
*/
?>
Twig:
{#
/**
* @file
* File description
*/
#}
2. File and function names
PHPTemplate file: node--article.tpl.php
Twig file: node--article.html.twig
PHPTemplate function: THEME_node_links()
Twig file: node-links.html.twig
3. Variables
Printing a variable:
PHPTemplate: <div class="content"><?php print $content; ?></div>
Twig: <div class="content">{{ content }}</div>
Printing a hash key item
PHPTemplate: <?php print $item; ?>
Twig: {{ item.alt }}
Assigning a variable:
PHPTemplate: <?php $custom_var = $content->comments; ?>
Twig: {% set custom_var = content.comments %}
Assigning an array:
PHPTemplate: <?php $args = array('@author' => $author, '@date' => $created); ?>
Twig: {% set args = {'@author': author, '@date': created} %}
4. Conditionals
PHPTemplate: <?php if ($content->comments): endif; ?>
Twig: {% if content.comments %} {% endif %}
PHPTemplate: <?php if (!empty($content->comments)): endif; ?>
Twig: {% if content.comments is not empty %} {% endif %}
PHPTemplate: <?php if (isset($content->comments)): endif; ?>
Twig: {% if content.comments is defined %} {% endif %}
PHPTemplate: <?php if ($count > 0): endif; ?>
Twig: {% if count > 0 %} {% endif %}
5. Control structures
PHPTemplate: <?php foreach ($users as $user) {} ?>
Twig: {% for user in users %} {% endfor %}
Πηγή: https://www.drupal.org/docs/8/theming/twig/comparison-of-phptemplate-and...