Giới thiệu Twig template trong Drupal 8 Themes

Giới thiệu Twig template trong Drupal 8 Themes

An Introduction to Twig in Drupal 8 ThemesPHPTemplate is the engine that has been driving Drupal templates since 2005.

After nearly 10 years of honorable service, PHPTemplate is about to be replaced by Twig in Drupal 8.

This will be the biggest overhaul of Drupal theming in a decade.

What is Twig? How will it change Drupal themes? Read on and find out ...

What is Twig?

Twig is a template framework and is a direct replacement for PHPTemplate. 

Unlike PHPTemplate which was developed in-house by Drupal developers, Twig comes from the wider PHP world.

Twig was created created by SensioLabs, the same people who develop the Symfony2 framework. Drupal 8 is using Symfony2 to overhaul its codebase. Because Twig is natively supported by Symfony2, it was a logical choice to use for Drupal themes.

You can find the Twig website at http://twig.sensiolabs.org.

Who uses Twig?

Except for Drupal 8, it's not a very distinguished list yet:

Without a doubt, Drupal 8 will be by far the largest platform using Twig.

Is there any Twig documentation?

However, as you saw above, there aren't too many people using Twig yet. There are no books available and there's currently a shortage of online tutorials. I suspect that will start to change when Drupal 8 arrives.

How has Twig changed Drupal 8 themes?

Maybe it would be easier to answer that question in reverse ... how has Twig not changed Drupal 8 themes?

Let's compare two snippets from Bartik in Drupal 7 and 8. These snippets have an identical function.

This is Drupal 7 and the node.tpl.php file:

<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<a href="/<?php print $node_url; ?>"><?php print $title; ?></a>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>

This is Drupal 8 and the new node.html.twig file:

 

{{ title_prefix }}
{% if not page %}
<h2{{ title_attributes }}>
<a href="/{{ node_url }}">{{ label }}</a>
</h2>
{% endif %}
{{ title_suffix }}

The only things that haven't changed in those snippets are the H2 and a tags.

Compare the two side-by-side:

  • <?php print $title_attributes; ?> has been replaced by {{ title_attributes }}
  • <?php print $node_url; ?> has been replaced by {{ node_url }}
  • <?php print render($title_suffix); ?> has been replaced by {{ title_suffix }}

Looking at it that way, it's fair to say that Twig has produced much more readable code than the PHP did.

Are there any Drupal 8 themes to test?

Yes, in addition to Bartik and Stark, there's just over 35 themes with Drupal 8 versions.

Why Twig in Drupal 8? #1. Syntax

The five following point are all shamelessly plagiarized from Jen Lampton, who leads the Twig project. She's given a whole series of presentations on Twig in Drupal

Jen outlines 5 pain points with Drupal 7 themes. The first is syntax.

Drupal 7 mixes up data types including strings, objects and arrays. It also has multiple different ways of printing variables such as $node->nid and then $node_url.

Drupal 8 will access all variables consistently, for example {{ node.nid }} and {{ node_url }}

Why Twig in Drupal 8? #2. Inconsistency

Sometimes Drupal 7 relies on template files, other times it relies on functions. This can make it hard to understand how to override some parts of the Drupal core.

Drupal 8 only uses template files.

Why Twig in Drupal 8? #3. Complexity

Jen produced two images to illustrate how much simpler Drupal 8's theme system will be.

Here's Drupal 7:

media_1395329425112.png

And here's Drupal 8:

media_1395329451848.png

Why Twig in Drupal 8? #4. Redundancy

There's a lot of duplicated code in Drupal 7 themes.

Drupal 7 would have multiple files with the same lines of code, including:

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

Drupal 8 uses lines like this: {% extends "node.html.twig" %}. This means you can use proper inheritance inside your theme files and eliminate a lot of duplicate code.

Why Twig in Drupal 8? #5. Security

Drupal 7 would often print out unsanitized data. You could even run database queries from directly inside the theme files.

Drupal 8 can automatically sanitize variables and won't permit unsafe functions to run.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Ứng dụng nhắn tin miễn phí WhatsApp lập kỉ lục 27 tỉ tin nhắn trong 24 giờ

Ứng dụng nhắn tin miễn phí WhatsApp lập kỉ lục 27 tỉ tin nhắn trong 24 giờ

Ứng dụng nhắn tin miễn phí WhatsApp vừa xác lập kỉ lục mới khi xử lí 10 tỉ tin nhắn trong nội bộ người dùng và 17 tỉ tin nhắn đến từ bên ngoài, trong vong 24 giờ. Thông tin này được hãng thông báo trên Twitter của mình.

Các khái niệm trong Drupal cho ngừơi mới bắt đầu

Các khái niệm trong Drupal cho ngừơi mới bắt đầu

Khi mới bắt đầu tìm hiểu về Drupal các bạn cần tìm hiểu khái niệm của những thành phần có trong nó. Để có một cái nhìn tổng quan nhất về những gì mình cần phải tìm hiểu tiếp theo.Dưới đây là một số khái niệm cơ bản trong Drupal

Sub faceboook là gì? Cách tăng Sub như thế nào?

Trong bài viết này mình sẽ nói sơ qua về Sub facebook hay còn gọi là Fllow 

Công ty diệt chuột T&C

 

Diet con trung