为菜单创建模板文件
我想为菜单创建一个模板文件,我正在尝试这样的操作:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你在这里采取了错误的方法。在 Drupal 7 中,您根本不需要调用 theme() 方法。相反,您应该专注于可渲染数组并调用 drupal_render() ,而 drupal_render() 又会为您调用 theme() 。
解决这个问题后,让我们专注于您手头的问题:
您应该创建一个 my hook_theme() 实现。该挂钩定义您是使用函数还是模板文件来呈现 HTML。它还定义了要传递给函数/模板的变量。下面是 hook_theme() 实现的一个简短示例:
在这个示例中,当调用“theme_name”时,您还可以传递一个变量(选项)。
之后,创建 tpl.php 文件并使用所需的 HTML 和数据填充它。您可以创建模板文件或函数,但不能同时创建两者。
现在,在菜单的回调中,您希望返回一个可渲染数组,如下所示:
正如 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:
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:
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.