简体   繁体   English

PHP:打印HTML友好代码?

[英]PHP: Printing HTML-Friendly Code?

I am wondering if there is any technique out there for making HTML code look good when printing that HTML (multiple lines) from a PHP function? 我想知道是否有任何技术可以使从PHP函数打印HTML(多行)时的HTML代码看起来不错?

In particular, new lines and tabs. 特别是新的行和制表符。 Obviously, I want the HTML tags behind-the-scenes to look good with the proper new lines and tabs. 显然,我希望幕后的HTML标签在适当的新行和标签下看起来不错。 However, depending on when I call the PHP function that is printing the HTML, I might need to start the tabs at 1, 2, 3, 4, etc. I don't want to have to pass the starting tab count to the PHP function that is printing the HTML. 但是,根据我何时调用打印HTML的PHP​​函数,我可能需要在1、2、3、4等处启动选项卡。我不需要将启动选项卡数传递给PHP打印HTML的函数。

So, does anyone know a general-purpose way to print HTML-friendly code with PHP? 那么,有谁知道用PHP打印HTML友好代码的通用方法吗? Or is there a design principle that I am missing that says, "Don't print multiple HTML lines from a PHP function." 还是有我遗漏的设计原则,即“不要从PHP函数中打印多行HTML”。 ... ...

Thanks, 谢谢,

Steve 史蒂夫

I wouldn't bother. 我不会打扰的。 With DOM-inspection tools like Firebug readily available, there's no point wasting time on making something designed for a computer to read look good. 有了诸如Firebug之类的DOM检查工具,就可以浪费时间在为计算机阅读而设计的外观上。 See this question for other people's opinions on the matter too. 也请参阅此问题以获取其他人对此事的看法。

Markup Cleanup 标记清理

There are some wonderful codetools out there that will cleanup (ie, adjust spacing) and repair (ie, close open tags). 有一些很棒的代码工具可以清理(即调整间距)和修复(即关闭打开的标签)。

Tidy is one of the more popular html cleanup/repair tools. Tidy是较流行的html清理/修复工具之一。 You can parse your script's output through Tidy on-the-fly using php's output buffers. 您可以使用php的输出缓冲区通过Tidy即时解析脚本的输出。

Zend Dev Zone Intro - http://devzone.zend.com/article/761 Tidy Docs in PHP Manual - http://www.php.net/manual/en/book.tidy.php Zend公司开发园区简介- http://devzone.zend.com/article/761 PHP手册中整理文档- http://www.php.net/manual/en/book.tidy.php

And now, a trivial example, yay! 现在,这是一个简单的例子,是的!

<?php
ob_start();

echo '<div><b> messy</b> <div> <i>blarg</I></div>';
echo '  <div>code rawr!</div>';

$output = ob_get_clean();

$tidy = tidy_parse_string($output);
tidy_clean_repair($tidy);
echo $tidy;
?>

Templating System 模板系统

You should be using a templating system to separate the logic (php code) and presentation (html/markup/etc) parts of your application. 您应该使用模板系统来分离应用程序的逻辑部分(php代码)和表示部分(html / markup / etc)。

Using this practice has many advantages including, among other things, making your code easier to maintain and debug. 使用这种做法有很多优点,其中包括使代码更易于维护和调试。

This is a huge topic of discussion, and almost always goes into frameworks and MVC. 这是一个巨大的讨论主题,几乎总是涉及到框架和MVC。 There are thousands of resources, here are a few :P 有成千上万的资源,这里有一些:P

Writing a Template System in PHP - http://www.codewalkers.com/c/a/Display-Tutorials/Writing-a-Template-System-in-PHP/ 用PHP编写模板系统-http : //www.codewalkers.com/c/a/Display-Tutorials/Writing-a-Template-System-in-PHP/
Separation of Business Logic from Presentation Logic in Web Applications - http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21 Web应用程序中业务逻辑与表示逻辑的分离-http : //www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

if you're generating valid xhtml, you can buffer your output and at the end, do that: 如果要生成有效的xhtml,则可以缓冲输出 ,最后,执行以下操作:

$dom = new DomDocument (('1.0', 'utf-8');
$dom->loadHtml (ob_get_clean ());
$dom->formatOutput = true; 
echo $dom->saveXML ();

if you're using a framework you can probably make this work as a plugin or filter. 如果您使用的是框架,则可以将其用作插件或过滤器。 this way it's also work if you embed template into other template or use output from helper 这样,如果您将模板嵌入其他模板或使用帮助程序的输出,它也可以工作

使用模板系统生成HTML并保持模板井然有序是一种简单的方法

You shouldn't be printing html with php. 您不应该使用php打印html。 Use php's shorthand syntax inside of a template. 在模板内部使用php的速记语法。

<h1><?= $page_title ?></h1>
<p><?= page_content ?></p>

instead of 代替

<?php

echo "<h1>" . $page_title . "</h1>";
echo "<p>" . $page_content . "</p>";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM