如何从自定义 Drupal 模块将变量注入到页面模板中?
我们创建了一个自定义模块来组织和发布我们的新闻通讯内容。
我现在遇到的问题——我是主题和 Drupal 模块开发的新手,所以它可能只是一个知识问题,而不是 Drupal 问题——是如何让每个新闻通讯都有主题。
此时,我们新闻通讯的 URL 结构将是:
/newsletters/{newsletter-name}/{edition-name}/{issue-date} 这意味着我们可以使用像 page-newsletters- 这样的文件名在我们的主题中创建模板文件{newsletter-name}-{edition-name}.tpl.php,非常棒。我遇到的一个问题是所有内容都来自主题的 $content 变量。我希望将其作为不同的变量来实现(以便我可以在主题内部将某些内容放置在某些区域中。)
是否有正确的方法来执行此操作?
编辑:回答一些问题:问题是一个节点(有问题、版本和新闻通讯节点),并且使用带有通配符和路由器的 hook_menu 设置路径。
We've created a custom module for organizing and publishing our newsletter content.
The issue I'm running into now -- and I'm new to theming and Drupal module development, so it could just be a knowledge issue as opposed to a Drupal issue -- is how to get each newsletter themed.
At this point the URL structure of our newsletter will be:
/newsletters/{newsletter-name}/{edition-name}/{issue-date} which means that we can create template files in our theme using filenames like page-newsletters-{newsletter-name}-{edition-name}.tpl.php, which is great. The one issue I'm running into is that all of the content comes through in the $content variable of the theme. I'd like to have it come through as different variables (so that I can, inside the theme, place certain content in certain areas.)
Is there a proper way for doing this?
Edit: To answer some questions: The issue is a node (there are issue, edition and newsletter nodes) and the path is being set using hook_menu with wildcards and a router.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能找到的最好答案是在 phptemplate_preprocess_page 内部添加一个检查,以将变量发送回模块并更新它们。
像这样:
然后在我的 test_module.module 文件中我创建了这个函数:
它似乎有效。我希望有一种方法可以做到这一点,而不必触摸主题的 template.php 文件,但除此之外,这效果很好。
The best answer I could find was to add a check inside of phptemplate_preprocess_page to send the vars back to the module and have them be updated.
Like so:
then in my test_module.module file I created this function:
It seemed to work. I wish there was a way to do this without having to touch the theme's template.php file, but otherwise this works well.
如果有更好的模板预处理函数文档,Drupal 将会更容易访问 - 事实上,您需要将来自许多不同解释的信息拼凑在一起。一个起点是这里:
http://drupal.org/node/223430
但如果你采取花时间完成下面的教程,您会发现您可以做大多数事情:
http://11heavens.com/theming-the-contact-form-in-Drupal-6
If there were better documentation for template preprocess functions, Drupal would be a lot more accessible - as it is, you need to piece together the information from a lot of different explanations. One place to start is here:
http://drupal.org/node/223430
but if you take the time to work through the tutorial below, you'll find you can do most things:
http://11heavens.com/theming-the-contact-form-in-Drupal-6
这是一篇旧帖子,OP的问题似乎已经解决了。
但是,对于通过 Google(或其他方式)找到此内容的其他人:
使用 Devel Themer 遍历 $content 变量并找到您需要提取的内容。
那里有很多 Devel/Themer 文档/教程,但它的用法非常简单。但请注意,在主题中打印之前需要对其中的某些内容进行清理。
将节点显示为视图然后修改视图模板的建议听起来很疯狂,尽管它会起作用。
This is an old post, and the OP's issues seems to have been solved.
However, just for others finding this through Google (or otherwise):
Use Devel Themer to go through the $content variable and find what you need to pull out.
There are a bunch of Devel/Themer docs/tuts out there, but its usage is pretty straightforward. Note, though, that some stuff in there will need to be sanitized before printing in the theme.
The suggestion to show the node as a View and then modifying the view templates sounds pretty crazy, though it'll work.