Showing posts with label if else syntax in twig. Show all posts
Showing posts with label if else syntax in twig. Show all posts

Friday 29 March 2019

How to Check the Condition in Twig Template?

It's easy process to check the condition of our 'id' exist or not for particular variable render time in Twig template.
Like we have to check any "Product type" exist or not and then variable call there, So we are giving an example:
Example 1:
{% if node.field_product_type.value not in ['0', '2', '4', '5', '7'] %}
 /*** print variable here ***/
{% endif %}

Description: Here our condition is, if Product type of node value is not in these id's ['0', '2', '4', '5', '7'], then our variable will show the result otherwise not.

Example 2:
{% set node_id = node.id %}
{% if node_id in ['216','217','9','1258'] %} 
 /*** print variable here ***/
{% endif %}

Description: Here our condition is, if node id of node is in these id's ['216','217','9','1258'], then our variable will show the result otherwise not.


Note: If have any suggestions or issue regarding 'How to Check the Condition in Twig Template?' then you can ask by comments.

Wednesday 4 July 2018

Check AND /OR use in twig file.

Check AND /OR use in twig file, look at the syntax..

For OR Syntax..

{% if content.field_machine_name|render or content.field_machine_name|render %}
        /* write comment here */
{% endif %}

For AND Syntax..
 
{% if content.field_machine_name|render and content.field_machine_name|render %}
        /* write comment here */
{% endif %} 

Note: If have any suggestions or issue regarding 'Check AND /OR use in twig file' then you can ask by comments.

Check twig file field is empty or not.

For check Twig file field is empty or not, look at the syntax..
 
{% if content.field_machine_name[0] is not empty %}
           /* write comment here */
{% else %}
          /* write comment here */
{% endif %}

                                 OR

{% if content.field_machine_name|render %}
       /* write comment here */
{% endif %}


Note: If have any suggestions or issue regarding 'Check twig file field is empty or not' then you can ask by comments.

Friday 29 June 2018

Twig file feature in Drupal 8.

We use '.tpl.php' extension for druapl 7 but now in case of drupal 8 extension is '.html.twig'.
Lets talk about twig file..
  1. For node id in twig file syntax..  {{ node.id }}
  2. For node title in twig file syntax.. {{ node.label }}
  3. For node body in twig file syntax.. {{ node.body }} 
  4. For node any field.. {{ content.field_machine_name[0] }} or {{ content.field_machine_name.0 }}
if else syntax..

{% if ... %}
            /* write comment here */
{% else %}
           /* write comment here */
{% endif %}


For create variable in twig file syntax..
 
{% set variable name = "variable value" %}  then write {{  variable name }} and use it easily.

 Note: If have any suggestions or issue regarding 'Twig file feature in Drupal 8' then you can ask by comments.

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...