I am trying to render a template in Drupal 8. In Drupal 7, I would create a theme with hook_theme, make a templates directory in my module, drop the template file in and I could display it with theme(). How do I do the eqivilent with Drupal 8? I tried the following, but it errors out:
In twitter_pull.module
/** * Implements hook_theme(). */ function twitter_pull_theme($existing, $type, $theme, $path) { return array( 'twitter_pull_tweet_listing' => array( 'variables' => array( 'description' => array(), ), 'template' => 'templates/twitter_pull_tweet_listing', ), ); }
Inside twitter_pull/templates: the twitter_pull_tweet_listing.html.twig
Inside my block:
$tweet_template = array('#theme' => 'twitter_pull_tweet_listing', '#attributes' => array('params' => $params)); $output = drupal_render($tweet_template, array('params' => $params)); print $output;
I get the following error in my logs:
User error: "content" is an invalid render array key in Drupal\Core\Render\Element::children() (line 89 of C:\xampp\htdocs\drupal\core\lib\Drupal\Core\Render\Element.php).
EDIT: Another error:
Twig_Error_Loader: Unable to find template "modules/custom/twitter_pull/templates/templates/twitter_pull_tweet_listing.html.twig" (looked into: C:\xampp\htdocs\drupal). in Twig_Loader_Filesystem->findTemplate() (line 202 of C:\xampp\htdocs\drupal\core\vendor\twig\twig\lib\Twig\Loader\Filesystem.php).
I imagine this error is happening because I'm not using namespaces and autoloading correctly. Where do I put my template file? Based on the error message, I even tried to put it at C:\xampp\htdocs\drupal\templates\ but it still did not work.
drupal_render()
? It's expecting a boolean – Clive♦ Nov 7 '14 at 15:37templates/templates/
part in the 2nd error message makes me think you should try'template' => 'twitter_pull_tweet_listing',
instead of'template' => 'templates/twitter_pull_tweet_listing',
inhook_theme()
– Clive♦ Nov 7 '14 at 17:44