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

后端 未结 8 1764
误落风尘
误落风尘 2020-12-12 11:55

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


          


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 12:15

    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:

    Template File

    
    
    {$title}
    {$string}
    
    

    Code

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

提交回复
热议问题