I have file tpl in drupal 7 like this:

<div class="<?php print $classes; ?>" <?php { print $attributes; } ?>>
    $content_section = '<div class="'.$container_class.'">';
    foreach($columns as $key => $column) {
      $content_section .= render($column);
    $content_section .='</div>';
      $customHTML = str_replace('[content]', $content_section, $customHTML);
      print $customHTML;
    else {
      print $content_section;

I want convert two twig template. But i have problem with function render() and str_replace(). I know in twig have function replace. But don't know how to convert to twig.

  • The render function is run whether you print a render array in twig ({{ content }}) – Eyal Sep 13 '16 at 16:25

In Drupal 8 you can use the drupal |render twig filter:

{{ myvar|render }}

Replace might be used as:

{% set customHTML = customHTML|replace({'[content]': content_section}) %}

having the variables all ready.

To convert your whole template to twig have a look at loops, also.

| improve this answer | |

For render, you can do something like

{{ variable|render }}

For more information on converting .tpl to twig, kindly check out "Drupal Twig conversion instructions (tpl.php to html.twig)".

| improve this answer | |

Same with @Alex and @Suresh R

{{ myvar|render }}

you can follow document see more filter in drupal 8. Note if variable customHtml have tag html. You need use filter raw to print it.

{{ myvar|raw }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy