有没有办法在 PHP 函数中返回 HTML?(不将返回值构建为字符串)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:59:56  来源:igfitidea点击:

Is there any way to return HTML in a PHP function? (without building the return value as a string)

phpstringtemplating

提问by seanyboy

I have a PHP function that I'm using to output a standard block of HTML. It currently looks like this:

我有一个 PHP 函数,用于输出标准的 HTML 块。它目前看起来像这样:

<?php function TestBlockHTML ($replStr) { ?>
    <html>
    <body><h1> <?php echo ($replStr) ?> </h1>
    </html>
<?php } ?>

I want to return (rather than echo) the HTML inside the function. Is there any way to do this without building up the HTML (above) in a string?

我想返回(而不是回显)函数内的 HTML。有什么方法可以不用在字符串中构建 HTML(上面)?

回答by Paul Dixon

You can use a heredoc, which supports variable interpolation, making it look fairly neat:

您可以使用支持变量插值的heredoc,使其看起来相当整洁:

function TestBlockHTML ($replStr) {
return <<<HTML
    <html>
    <body><h1>{$replStr}</h1>
    </body>
    </html>
HTML;
}

Pay close attention to the warning in the manual though - the closing line must not contain any whitespace, so can't be indented.

但是请密切注意手册中的警告 - 结束行不能包含任何空格,因此不能缩进。

回答by Konrad Rudolph

Yes, there is: you can capture the echoed text using ob_start:

是的,有:您可以echo使用ob_start以下方法捕获ed 文本:

<?php function TestBlockHTML ($replStr) { ob_start(); ?>
    <html>
    <body><h1> <?php echo ($replStr) ?> </h1>
    </html>
<?php
    return ob_get_clean();
} ?>

回答by Lorien Brune

This may be a sketchy solution, and I'd appreciate anybody pointing out whether this is a bad idea, since it's not a standard use of functions. I've had some success getting HTML out of a PHP function without building the return value as a string with the following:

这可能是一个粗略的解决方案,我很感激有人指出这是否是一个坏主意,因为它不是函数的标准使用。我已经从 PHP 函数中获取 HTML 取得了一些成功,而无需将返回值构建为具有以下内容的字符串:

function noStrings() {
    echo ''?>
        <div>[Whatever HTML you want]</div>
    <?php;
}

The just 'call' the function:

只是“调用”函数:

noStrings();

And it will output:

它会输出:

<div>[Whatever HTML you want]</div>

Using this method, you can also define PHP variables within the function and echo them out inside the HTML.

使用此方法,您还可以在函数内定义 PHP 变量并将它们在 HTML 内回显。

回答by Rob

Create a template file and use a template engine to read/update the file. It will increase your code's maintainability in the future as well as separate display from logic.

创建模板文件并使用模板引擎读取/更新文件。它将增加您的代码在未来的可维护性以及将显示与逻辑分开。

An example using Smarty:

一个使用Smarty的例子:

Template File

模板文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>{$title}</title></head>
<body>{$string}</body>
</html>

Code

代码

function TestBlockHTML(){
  $smarty = new Smarty();
  $smarty->assign('title', 'My Title');
  $smarty->assign('string', $replStr);
  return $smarty->render('template.tpl');
}

回答by Cayde 6

Another way to do is is to use file_get_contents()and have a template HTML page

另一种方法是使用file_get_contents()并拥有一个模板 HTML 页面

TEMPLATE PAGE

模板页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>$title</title></head>
<body>$content</body>
</html>

PHP Function

PHP函数

function YOURFUNCTIONNAME($url){

$html_string = file_get_contents($url);
return $html_string;

}

回答by Wahid Lahouiter

Or you can just use this:

或者你可以只使用这个:

<?
function TestHtml() { 
# PUT HERE YOU PHP CODE
?>
<!-- HTML HERE -->

<? } ?>

to get content from this function , use this :

要从此函数获取内容,请使用:

<?= file_get_contents(TestHtml()); ?>

That's it :)

就是这样 :)

回答by marrion luaka

If you don't want to have to rely on a third party tool you can use this technique:

如果您不想依赖第三方工具,则可以使用此技术:

function TestBlockHTML($replStr){
  $template = 
   '<html>
     <body>
       <h1>$str</h1>
     </body>
   </html>';
 return strtr($template, array( '$str' => $replStr));
}