Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

7.x 2.x helper content render

Andy Truong edited this page Jan 17, 2014 · 17 revisions

Render a string

<?php
$render = at_container('helper.content_render');
$render->render('Hello Drupal!');
?>

Render content with function callback

<?php
$render = at_container('helper.content_render');
$render->render(array(
  'function' => 't', 
  'arguments' => array('Hello Drupal!')
));
?>

Render content with a Twig template

<?php
$render = at_container('helper.content_render');
$render->render(array(
  'template' => '@my_module/templates/hello.html.twig', 
  'variables' => array('name' => 'Drupal')
));
?>

Render content with class

<?php
$render = at_container('helper.content_render');
$render->render(array(
  'controller' => ['Foo', 'bar'],
  'arguments' => array('name' => 'Drupal')
));
?>

Render content with Twig string template

<?php
$render = at_container('helper.content_render');
$render->render(array(
  'template_string' => 'Hello {{ name|t }}', 
  'variables' => array('name' => 'Drupal')
));
?>

Cache rendered-output

If the output is expensive, we can cache it:

<?php
$render = at_container('helper.content_render');
$output = $render->render(array(
  'template_string' => '{{ view_name|drupalView }}',
  'variables' => array('view_name' => 'latest_products'),
  'cache' => array(
    'id' => 'products:latest:front',
    'ttl' => '+ 30 minutes',
    'tags' => ['node', 'products', 'home']
  )
));

Clone this wiki locally