This repository was archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
7.x 2.x helper content render
Andy Truong edited this page Feb 25, 2014
·
17 revisions
Note that,
$renderused below is object fromat_container('helper.content_render').
<?php
$render->render('Hello Drupal!');<?php
$render->render(array(
'function' => 't',
'arguments' => array('Hello Drupal!')
));<?php
$render->render(array(
'template' => '@my_module/templates/hello.html.twig',
'variables' => array('name' => 'Drupal')
));<?php
// Usage #1
$render->render(['controller' => ['Foo', 'bar', ['name' => 'Drupal']],]);
// Usage #2
$render->render([
'controller' => ['Foo', 'bar'],
'arguments' => ['name' => 'Drupal']
]);<?php
$render->render(array(
'template_string' => 'Hello {{ name|t }}',
'variables' => array('name' => 'Drupal')
));To avoid complex logic in our templates/controller, we can pre-process the variables.
All callbacks have to render keyed-array.
<?php
$render->render(array(
'template' => '@my_module/templates/hello.html.twig',
'variables' => 'service_name:service_method',
'variables' => 'my_callback',
'variables' => [$obj, 'method'],
'variables' => ['Class_Name', 'method']
'variables' => ['Class_Name::staticMethod']
));If we use controller to render content, and no 'variables' defined in $data
array, we can define getVariables() method in our controller.
<?php
class My_Controller {
public function render($variables) {
return "Hello {$variables['name']}";
}
public function getVariables() {
return array('name' => 'Drupal');
}
}
// Output: Hello Drupal
echo $render->render(array('controller' => ['My_Controller', 'render']));If the output is expensive, we can cache it:
<?php
$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']
)
)); ___ ___________ _ _
/ _ \_ _| ___ \ | | | |
/ /_\ \| | | |_/ / __ _ ___ ___ _ __ ___ ___ __| |_ _| | ___
| _ || | | ___ \/ _` / __|/ _ \ | '_ ` _ \ / _ \ / _` | | | | |/ _ \
| | | || | | |_/ / (_| \__ \ __/_| | | | | | (_) | (_| | |_| | | __/
\_| |_/\_/ \____/ \__,_|___/\___(_)_| |_| |_|\___/ \__,_|\__,_|_|\___|
- Caching
- Twig template
- twig Service
- twig_string Service
- Recipes
- Extends
- Easy Block
- Easy Routing
- Easy Breadcrumb
- Entity Template
- Drush Commands:
- Functions:
- Misc
- Status
- Tools for Development enviroment
- /devel/php improved
- /at/sql
- /at/twig
- Kint integration