Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Inspect variables in template file in Drupal 8

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 594
    Comment on it

     

    Lets see how can we discover the dynamic content of any template twig file.

    we will use dump(), kint() functions to inspect the variables of the template file.

     

    Each core template contains document at the top in the file those document contains the information about the variables available.

    But this documentation doesn't contain all the variables. The best way to inspect the variables in a template file is by using the the twig's {{ dump() }} function.

     

    Steps describing the twig's dump() function use

     

    1- Enable the debug mode of your setup.

    2- Locate file to in which we need to inspect the variables.

    3- Add the code in the file {{ dump() }} at the bottom of the page.

    4- Examine the output. The output of this will be like the var_dump() format.

    5- Optionally, if we need to check an individual variable we can do so:

    {{ dump(variable_name) }}.

     

    to make the output in readable format use <pre> tage as

    <pre>{{ dump() }}</pre>

     

     

    The kint is a tool which is similar to the twig's dump() function, but it has the better output format.

    To use the kint() function we need to download the devel module in our setup which contains the kint module and the kint php library.

     

    Steps describing the using kint() function with the devel module use

     

    1- Download the Devel module and install it.

    2- Enable Kint module. It can be done in two ways first using Drupal UI or drush en kint.

    3- In the template file instead of the {{ dump() }} we can use {{ kint() }}.

     

    Preprocessed Functions

     

    In the twig templates the variables are provided through the preprocessed functions.

    To get in deeper to know about the variable we can look into the preprocessed function of that template file.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: