[英]Template for custom page Drupal 8
我的模块中的自定义页面模板有问题。 我使用hook_theme()
定义我的树枝文件。 当我签入hook_theme_registry_alter()
我看到了我的新模板文件,但是当我尝试使用它时却无法正常工作。
我的代码:
文件:first.module
/**
* Implement hook_theme().
*/
function first_theme($existing, $type, $theme, $path) {
return array(
'testtwig' => array(
'template' => 'testtwig',
'variables' => array('test_var' => NULL),
),
);
}
控制器:
/**
* @file
* Contains \Drupal\first\Controller\FirstController.
*/
namespace Drupal\first\Controller;
use Drupal\Core\Controller\ControllerBase;
class FirstController extends ControllerBase {
public function content() {
return array(
'#theme' => 'testtwig',
'#test_var' => t('sss'), //$output,
);
}
}
错误:
未定义模板“ modules / custom / first / templates / testtwig.html.twig”(Drupal \\ Core \\ Template \\ Loader \\ ThemeRegistryLoader:无法在其中找到模板“ modules / custom / first / templates / testtwig.html.twig” Drupal主题注册表。)。
//.module文件
<?php
/**
* Implements hook_theme().
*/
function MODULE_theme($existing, $type, $theme, $path) {
return [
'site_framework_display' => [
'variables' => ['test_var' => NULL],
'template' => 'page--site-framework',
],
];
}
//控制器
<?php
namespace Drupal\MODULE\Controller;
use Drupal\Core\Controller\ControllerBase;
class MODULEController extends ControllerBase {
public function getVersion() {
return [
'#theme' => 'site_framework_display',
//'#test_var' => \DRUPAL::VERSION,
'#test_var' => 'hello guys'
];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.