格式化主题函数的 HTML 输出
我已经实现了自己的主题函数 (theme_my_theme_function
) 并实现了 hook_theme
来告诉 Drupal 我的函数。一切正常,我在页面上得到了我想要的输出,除了...
当我查看 HTML 源代码时,我的函数输出的所有 HTML 代码都在一长行中!如何让我的主题函数输出整洁且易于阅读的格式化 HTML?
I've implemented my own theme function (theme_my_theme_function
) and have implemented hook_theme
to tell Drupal about my function. Everything works correctly and I get the output I want on the page, except...
When I look at the HTML source code, all the HTML code outputted by my function is all in one long line! How do I get my theme function to output formatted HTML that is neat and easy to read?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望对 HTML 进行格式化,则必须在构建 HTML 字符串时自行完成。更简单的方法是使用模板文件,而不是手动构建 HTML 字符串(请记住,PHP 本质上是一种 HTML 模板语言)。这里有一篇关于如何做到这一点的很好的文章:http://drupal.org/node/165706
核心节点模块有一个很好的例子:
node.module:
node.tpl.php:
还有一种方法可以将 HTMLTidy 集成到 Drupal 中,以便它在输出之前“美化”标记,但我自己从未这样做过。
最后,我强烈建议不要担心 HTML 输出的格式。相反,请使用适用于 Firefox 的 Firebug 或适用于 Chrome/Safari 的 Inspector。它们都有一个“检查元素”工具,可以让您在一个漂亮的可浏览、可编辑的树中查看页面的标记。这对于 Web 开发来说是无价的。
*编辑*
theme_item_list
对 HTML 输出进行最少的格式化。下面是theme_item_list
生成的列表示例:在
theme_item_list
的代码中,您可以看到它只是在If you want the HTML to be formatted, you're going to have to do it yourself when you build the HTML strings. The easier way to do that is to use a template file as opposed to building strings of HTML manually (remember that PHP is essentially a HTML template language). There is a pretty good write up of how to do that here: http://drupal.org/node/165706
The core node module has a pretty good example:
node.module:
node.tpl.php:
There may also be a way to integrate HTMLTidy into Drupal so that it "beautifies" the markup before it's output, but I've never done that myself.
Ultimately, I would highly recommended not worrying about the formatting of your HTML output. Instead, use Firebug for Firefox or Inspector for Chrome/Safari. These both have an "Inspect Element" tool that lets you view the markup of the page in a nice browsable, editable tree. It's invaluable for web development.
*EDIT*
theme_item_list
does minimal formatting of HTML output. Here is an example of a list generated bytheme_item_list
:In the code for
theme_item_list
you can see that it just adds a "\n" after the <li>: