为菜单创建模板文件

发布于 2024-12-19 17:30:37 字数 212 浏览 6 评论 0原文

我想为菜单创建一个模板文件,我正在尝试这样的操作:

function MYTHEME_menu_tree__main_menu($variables) {
   return theme('mymainmenu', $variables);
}

我在 mymainmenu.tpl.php 中编辑主菜单的 HTML,但此代码不起作用。为什么?

I want to create a template file for a menu, and I am trying something like this:

function MYTHEME_menu_tree__main_menu($variables) {
   return theme('mymainmenu', $variables);
}

I edit the main menu's HTML in the mymainmenu.tpl.php, but this code is not working. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2024-12-26 17:30:37

我认为你在这里采取了错误的方法。在 Drupal 7 中,您根本不需要调用 theme() 方法。相反,您应该专注于可渲染数组并调用 drupal_render() ,而 drupal_render() 又会为您调用 theme() 。

解决这个问题后,让我们专注于您手头的问题:

您应该创建一个 my hook_theme() 实现。该挂钩定义您是使用函数还是模板文件来呈现 HTML。它还定义了要传递给函数/模板的变量。下面是 hook_theme() 实现的一个简短示例:

mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'theme_name' => array(
       'variables' => array(
          'options' => NULL,
       )
       'template' => 'theme-name'
   );
}

在这个示例中,当调用“theme_name”时,您还可以传递一个变量(选项)。

之后,创建 tpl.php 文件并使用所需的 HTML 和数据填充它。您可以创建模板文件或函数,但不能同时创建两者。

现在,在菜单的回调中,您希望返回一个可渲染数组,如下所示:

$output = array(
   '#theme' => 'theme-name',
   '#options' => $aVariable,
);
return $output;

正如 Clive 在评论中提到的,如果您发布所有涉及您的问题的方法,那么它会更有用,以便我们可以更好地评估您的问题。

I think you're taking the wrong approach here. In Drupal 7 you don't need to be calling the theme() method at all. You should instead focus on renderable arrays and calling drupal_render() which in turn calls theme() for you.

With that out of the way, lets focus on your problem at hand:

You should create a my hook_theme() implementation. This hook defines whether you are using a function or a template file for rendering your HTML. It also defines which variables to pass to the function/template. Here is a brief example of a hook_theme() implementation:

mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'theme_name' => array(
       'variables' => array(
          'options' => NULL,
       )
       'template' => 'theme-name'
   );
}

In this example, when calling 'theme_name' you will also be able to pass a variable (options).

After that, create your tpl.php file and populate it with the HTML and data you want. You either create a template file or a function but not both.

Now, in the callback for your menu, you want to return a renderable array like so:

$output = array(
   '#theme' => 'theme-name',
   '#options' => $aVariable,
);
return $output;

As Clive mentioned in the comment, it would be more useful if you post all your methods referring to your problem so we may better gauge your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文