【问题标题】:Is there any way to return HTML in a PHP function? (without building the return value as a string)有没有办法在 PHP 函数中返回 HTML? (不将返回值构建为字符串)
【发布时间】:2009-02-09 14:52:41
【问题描述】:

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

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

我想返回(而不是回显)函数内的 HTML。有没有什么方法可以做到这一点,而无需在字符串中构建 HTML(上图)?

【问题讨论】:

    标签: php string templating


    【解决方案1】:

    你可以使用heredoc,它支持变量插值,让它看起来相当整洁:

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

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

    【讨论】:

    • 另外,
    • 它可以任意缩进,但在标识符后面应该有一个换行符。
    • 如果 var 不在数组中,则可以避免使用大括号:

      $replStr

      {$array['var]}

    【解决方案2】:

    是的,有:您可以使用ob_start 捕获echoed 文本:

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

    【讨论】:

    • 没错,这种方式语法被突出显示,关键词被着色,就像它们是普通的 HTML 一样,而不是字符串中的内容。绝对是可维护性的更好答案,应该始终放在首位
    • 当这似乎工作正常时,为什么要使用输出缓冲区? &lt;?php function outputHTML($string) { ?&gt; &lt;h1&gt;&lt;?php echo $string; ?&gt;&lt;/h1&gt; &lt;p&gt;Lorem ipsum dolar&lt;/p&gt; &lt;? } ?&gt; &lt;?php outputHTML('A is for Apple'); ?&gt; &lt;?php outputHTML('B is for Ball'); ?&gt;
    • @Joren 因为这显然是 OP 不想要的。
    • 好的,如果你想回应它,我的例子就是要走的路吗?
    • @Joren 是的,基本上。
    【解决方案3】:

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

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

    只是'调用'函数:

    noStrings();
    

    它会输出:

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

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

    【讨论】:

    • 这在没有echo 的情况下可以正常工作,只需?&gt; html &lt;?php。检查fiddle
    【解决方案4】:

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

    一个使用Smarty的例子:

    模板文件

    <!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>
    

    代码

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

    【讨论】:

    • 使用一些旧的 PHP 代码。这个很稳,谢谢!我不得不使用 Smarty 的 display 方法而不是 render
    【解决方案5】:

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

    模板页面

    <!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 YOURFUNCTIONNAME($url){
    
    $html_string = file_get_contents($url);
    return $html_string;
    
    }
    

    【讨论】:

    • 这是最接近我正在寻找的东西。 @Cayde 6,您是否认为有一种方法可以拥有一个包含 php 函数和 html 的文件“template.php”?所以然后在我网站的任何地方,我只是'require_once('template.php')',然后在我需要该html出现的任何地方调用echo该函数。在 YOURFUNCTIONNAME($url) 中,$url 需要是它自己吗?我们怎么能做到这一点?谢谢
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      或者你可以使用这个:

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

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

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

      就是这样:)

      【讨论】:

      • file_get_contents():文件名不能为空。这应该如何工作?
      【解决方案8】:

      模板文件

      <h1>{title}</h1>
      <div>{username}</div>
      

      PHP

      if (($text = file_get_contents("file.html")) === false) {
          $text = "";
      }
      
      $text = str_replace("{title}", "Title Here", $text);
      $text = str_replace("{username}", "Username Here", $text);
      

      然后你可以将 $text 作为字符串回显

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        • 2022-12-31
        • 1970-01-01
        • 2019-08-10
        相关资源
        最近更新 更多