amansrivastava/d9-dev

#19 ­- Twig Templating

Opened this issue · 0 comments

Objective

In this session, we will take a look at basics of Twig Templating, that has replaced the PHPTemplate engine in Drupal.

Exercise

  1. Download and install this module. This module provides a very basic block whose template is "day19/templates/day19‐twig‐test.html.twig". The build() of the block provides variables­ "var1, var2, classes, myclasscount" that are available in the template to be used.
  2. Place the block provided by the module ("My Block") onto a region on your site from block admin interface, so you can view the block.
  3. If you see the block display "Your template goes here.."­ you are all set now. Go further.
  4. Now modify the template as below
  • If "var1" is set, print the value of "var1".
  • Move the output of "var1" to a "" whose class names are those provided by "classes" variable passed to the template (Hint:­ Ensure that the class names are cleaned enough so they deserve to be class names).
  • Print the variable "$var2[3][‘g’]".
  • Print the word "Hello", such that it can be translated using the admin translation interface.
    -Move the "Hello" string into a "
    " such that the div has all classes in the pattern "minion0, minion1, minion2, minion3, minion4.....till minionX", where "X" is provided by the value of "myclasscount" variable.
  • Try out "{{ dump() }}" and "{{ dump(var1) }}" on the template to see how the printed variables show up on the screen.
  1. With the above changes, the markup of the rendered block is expected to besomething like this.

Bonus Exercise

The provided solution has a bug wherein the classes added to the span enclosing "var1", are also added as classes to the div enclosing the translatable "Hello" String. Fix the template.