0

比如……</p>

这个 PHP 代码

<?php
echo '<html>';
echo '<body>';
echo '<h1>Header One</h1>';
echo '<p>Hello World!</p>';
echo '</body>';
echo '</html>';
?>

生成此 HTML 标记

<html><body><h1>Header One</h1><p>Hello World!</p></body></html>

是否有任何函数、库或配置选项可以使 PHP 根据输出中生成的 html 标记的嵌套自动应用一些简单的格式(换行符和缩进)?所以会生成这样的东西……</p>

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
4

7 回答 7

4

您可以尝试像SmartySavantPHP SugarVLib这样的模板引擎。

或者你可以去突击队处理输出(我认为这是一个黑客)。ob_start('ob_tidyhandler');

对于输出处理,可能未安装或启用 Tidy,通常您需要安装的软件包已命名php-tidy,您需要extension=tidy在 php.ini中取消注释

于 2012-04-03T21:13:49.390 回答
1

If I understand your question right, you want to pretty-print the HTML output.

This can be done by post-processing the output of your PHP script. Either by using PHP's output handling feature combined with the tidy extension­Docs:

ob_start('ob_tidyhandler');

Tidy is an extension specialized on cleaning up HTML code, changing indentation etc.. But it's not the only way.

Another alternative is to delegate the post-processing task to the webserver, e.g. output filters in Apache HTTP Server­Docs.

于 2012-04-03T21:25:48.700 回答
1

您可以将 html 放入您的 PHP 脚本中,而无需回显它。您可能还想寻找像smarty这样的 PHP 模板引擎,这样您就可以将视图与逻辑分开。

于 2012-04-03T21:02:15.630 回答
1

我更喜欢使用heredoc 字符串并自己格式化/缩进 HTML。在 PHP 代码中混合 HTML 字符串会很快导致代码不可读、不可维护。这种方法的一些优点:

  • 引号不需要转义。
  • 您可以将变量放在 heredoc 字符串中。
  • 即使使用循环代码,您也可以在 heredoc 字符串中输出 HTML。如果这些字符串相对于您的其他 HTML 块正确缩进,您仍将获得具有良好缩进的 HTML 代码。
  • 它迫使您考虑何时要打印 HTML,并以块的形式打印它,而不是在代码中散布许多小块(非常难以阅读和维护)。

最好尽可能将 PHP 代码与 HTML 分开,无论这意味着使用模板引擎还是只是将所有代码放在所有 HTML 之前。然而,仍然有最容易混合 PHP 和 HTML 的时候。

这是一个例子:

<?php
$text = 'Hello World!';
echo <<<HTML
<html>
    <body>
        <h1>Header One, with some '"quotes"'</h1>
        <p>$text</p>
    </body>
</html>

HTML;
?>
于 2012-04-03T21:05:05.770 回答
0

...或者您可以使用“<<<”运算符,您可以在其中自己设置队形:

<?php

echo <<<DATA
<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
DATA;
于 2012-04-03T21:04:02.637 回答
0

你可以这样做(我的偏好):

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
            <?php echo '<p>Hello Hello!</p>'; ?>
      </body>
</html>

或者:

<?php

$html = '<html><body><h1>Header One</h1><p>Hello World!</p></body></html>';

$tidy = new tidy();
$tidy->parseString($html, array('indent'=> true,'output-xhtml'=> true), 'utf8');
$tidy->cleanRepair();

echo $tidy;

?>";

...将打印:

<html>
      <body>
            <h1>Header One</h1>
            <p>Hello World!</p>
      </body>
</html>
于 2012-04-03T21:04:14.337 回答
0

如果下面的代码看起来对使用 php 生成 html 很有用,请试试这个库 https://github.com/raftalks/Form

Html::make('div', function($html))
{
    $html->table(function($table)
    {
        $table->thead(function($table)
        {
            $table->tr(function($tr)
            {
                $tr->th('item');
                $tr->th('category');
            });
        });


        $table->tr(function($tr)
        {   
            $tr->td()->ng_bind('item.name','ng-bind');
            $tr->td()->ng_bind('item.category','ng-bind');

            $tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
        });

        $table->setClass('table');
    });

   $html->setClass('tableContainer');
});
于 2013-01-15T16:47:58.527 回答