【问题标题】:Template for custom page Drupal 8自定义页面模板 Drupal 8
【发布时间】:2016-07-09 16:46:03
【问题描述】:

我的模块中的自定义页面的模板有问题。 我使用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 主题注册表。)。

【问题讨论】:

  • 你得到的树枝文件是什么名字,你把它放在哪里?
  • Twig 文件名:testtwig.html.twig 我把这个文件放在modules/custom/first/templates
  • 我发现哪里有问题:) 所以我有另一个同名文件。感谢您的帮助

标签: templates drupal drupal-8


【解决方案1】:

//.模块文件

 <?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'
        ];

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2019-07-28
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多