Search code examples
phpsmarty

What is the best practice of replacing placeholders in an html page without using smarty etc?


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?


Solution

  • 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?