Quite significant changes were introduced in the theme system between Drupal 8 and 7. This guide contains information that applies to Drupal 8 theming.
18. What is Twig?
Twig is a template framework that has replaced PHPTemplate
in Drupal 8.
fast, secure and flexible.
http://twig.sensiolabs.org/documentation
30. less code than PHP functions
TWIG
theme_image becomes image.html.twig
31. less code than PHP functions
TWIG
theme_username becomes username.html.twig
32. TWIG
• PHP template reads files from disk on every use
Twig templates are read once & compiled into
classes
Rendering should get much faster when the same
content element appears multiple times on the
page.
Consolidating many similar templates will result in
an additional gain.
Possible performance gains
33. TWIG
• Saving PHP code in the database is a HUGE no-
no.
• Twig is not PHP, and is safe to store!
In-browser template editing finally safe.
34. TWIG
One template can return markup for both your
PHP generated pages, as well as pages
generated vi JS in AJAX callbacks.
We can use other open source libraries like
TwigJS.
(https://github.com/schmittjoh/twig.js)
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.
Twig template files can be used on the front
end, too.