tags:

views:

28

answers:

2

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?

Thanks in advance

+2  A: 

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?

Pekka
I originally built php includes, however it very quickly got untidy in the sense of having the html / php tags all over the place and I hate things being untidy lol.
Jon
@Jon I see. As a possible alternative, there is the heredoc notation: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Pekka
I shall have a read this evening when I get home... Just trying to build the best possible system for what I need. Using an open-source system isn't an option. I did notice the method I was attempting was a tad slow.
Jon
Wrikken
A: 

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.

Cfreak
Because this is purely for front end use, where I need multiple templates I guess I will have to just revert everything I did, and put it back to how you mentioned. Not that I like the mess with html/php together lol.
Jon