在不使用smarty等的情况下替换html页面中占位符的最佳做法是什么?

Below is a basic example of what I have done.

Grab html template

$template = file_get_contents("skins/".CSS_URL."/layouts/homepage.html");

Build array of items to search for

$search = array("|{THUMB_FEATURED_IMAGE}|", "|{LARGE_FEATURED_IMAGE}|", "|{PAGE_CONTENT}|");

Build array of items to replace the searched with

$replace = array($featured_image, $featured_image_l, $content);

Do the actual replace

$output = preg_replace($search, $replace, $template);

Then echo the output.

Is this bad practice and/or is there a better way without having to rewrite my entire CMS using Smarty?

I'd say it's not awfully terrible to do this way, although it's not very resource-friendly because you're effectively pulling the content twice.

Would simply having PHP tags inside the template <?php echo $featured_image; ?> and including the template file using include() not be a much, much easier option?

I think the best answer is to use PHP variables. Then you just include your "template" file and it has regular variables in it.

If you really want to try what you're doing. You'd be better off to use an actual HTML parser. It would probably be faster and much less error prone. Though implementing it would probably take as much time as implementing Smarty.

This is a old topic, but for anyone who may see this. While i'm not sure its best practice, What I've always done is return a include and parse place holders via array.

<?php //page.php
$html = "<html>
              <head>
                  <title>{PageTitle}<title>
              </head>
             ...
        </html>";
return $html;

then simply

  function parseHTML($pageContents, $placeholders){
        foreach($placeholders as $placeholder=>$value){
             str_replace("\{{$placeholder}\}",$value,$pageContents);
        }
        return $pageContents;
  }


$placeholders = ['PageTitle'=>'This is my page title'];

$pageContents = include "page.php";

$parsedHTML = parseHTML($pageContents, $placeholder);

the output would look like

        <html>
              <head>
                  <title>This is my page title<title>
              </head>
             ...
        </html>

Like i said though, I'm not 100% on how.. efficient this is. I use it because most of my projects are pretty small.