Drupal 7 - 可以在模板文件中编写 php 代码吗?
我正在为我的新项目学习 Drupal 7。
这是我想要做的
- 创建一个名为“video”的新内容类型
- 创建一个名为“video”的新模块
- make http:// domain.com/video 可访问。
- 当用户访问 /video 时,然后执行从视频表(由“视频”内容类型创建)中查询视频行的查询,然后使用自定义模板显示查询。
我已经学会了如何做#1 ~ #3,但我对#4有点困惑。
假设我的自定义模板的文件名是
video.tpl.php 中的“video.tpl.php”,我是否编写 php 函数来查询视频行?
我认为这不是一个好的做法。相反,我想编写一个模块并在加载 video.tpl.php 时调用模块中的函数。
我该怎么做?
I am learning Drupal 7 for my new project.
Here is what I want to do
- create a new content type called 'video'
- create a new module called 'video'
- make http://domain.com/video accessible.
- when a user access /video, then execute a query that queries video rows from video table (created by 'video' content type'), then display queries with a custom template.
I have learned how to do #1 ~ #3, but I am a little bit confused with #4.
let's assume that my custom template's filename is 'video.tpl.php'
in the video.tpl.php, do I write php functions to query video rows?
I don't think that is a good practice. Instead, I want to write a module and call a function in the module when the video.tpl.php is loaded.
How do I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 4,您首先需要创建一个模块,实现 hook_menu(),使用页面回调定义“视频”菜单项。如果您不知道如何做到这一点,那么可能已经有很多关于此的问题)。在页面回调中,您需要做 3 件事。
加载nids,类似
$nids = db_query("SELECT nid FROM {node} WHERE type = 'video' ORDER BY 创建的 DESC")->fetchCol();
加载节点。
$nodes = node_load_multiple($nids);
构建它们。
return node_view_multiple($nodes);
但同样,如果您想学习 API,您应该仅这样做。视图将为您完成所有这些工作,您只需将其一起单击即可。
For 4, you first need to create a module, implement hook_menu(), define a menu item for 'videos' with a page callback. If you don't know how to do that, there are likely already a lot of questions about that). Inside the page callback, you need to do 3 things.
Load the nids, something like
$nids = db_query("SELECT nid FROM {node} WHERE type = 'video' ORDER BY created DESC")->fetchCol();
Load the nodes.
$nodes = node_load_multiple($nids);
Build them.
return node_view_multiple($nodes);
But again, you should only do this if you want to learn the API. Views will all of this do for you, you just need to click it together.
您应该使用视图
You should use views
视图绝对是一条出路。你可以编写一个模块,但这就像重新发明轮子一样。学习视图,您将一次又一次地使用它......
教程:
视图教程
另一个视图教程
Views is definitely the way to go. you could write a module, but it would be like re-inventing the wheel. Learn Views and you will use it again and again....
tutorials:
a views tutorial
Another views tutorial