11

I think the basic principle of a PHP templating system is string replacing, right? So can I just use a string to hold my html template code like

$str_template = "<html><head><title>{the_title}</title><body>{the_content}</body></html>"

and in the following code simply do a str_replace to push the data into my template variable like

str_replace( $str_template, '{the_title}', $some_runtime_generated_title );
str_replace( $str_template, '{the_content}', $some_runtime_generated_content );

then at last

echo $str_template; 

Will this hopefully make the whole variable passing process a bit faster? I know this could be a weird question but has anybody tried it?

9 Answers 9

6

Yes, that is the basic idea behind a templating system. You could further abstract it, let an other method add the brackets for example.

You could also use arrays with str_replace and thus do many more replaces with one function call like this

str_replace(array('{the_title}', '{the_content}'), array($title, $content), $str_template);

The system I work with is Spoon Library, their templating system is pretty rock solid, including compiled templates, which are a huge performance gain.

Sign up to request clarification or add additional context in comments.

2 Comments

Is Spoon Lib faster than smarty?
Arrays are very nice in this function. As for smarty, pretty much anything is a faster and better option.
4

That is generally the basic idea for a templating system. Real templating systems have many more capabilities, but at the core this is what they do.

You ask whether this will "make the whole variable passing process a bit faster". Faster than what? Are you running into performance problems with something you're doing right now?

4 Comments

sorry I meant faster than the regular templating approach like the Smarty way. No I'm not running into performance problems, just curiosity. Thanks.
Using Smarty will likely be more efficient than what you have proposed. Consider that every time you call str_replace(), once for each variable to substitute, the entire template string must be scanned to look for curly braces to replace. Smarty will do that operation more efficiently.
Oh, another difficulty with the simple string replace is, what happens if your $some_runtime_generated_title contains something that looks like a variable replacement, like "where does {the_content} go?" The next substitution for {the_content} will incorrectly replace that in the title too.
smarty parses the template once to generate php code. the php code is executed then for each request
4

I would build a parser that breaks the code into it’s tokens (variables and plain text). As an easy approach, you could use the pre_split function to do this:

$tokens = preg_split('/(\{[^\}]+\})/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

Now you can iterate the tokens and check whether it is a variable or plain text:

foreach ($tokens as $token) {
    if ($token[0] === '{' && $token[strlen($token)-1] === '}') {
        // token is a variable
    } else {
        // token is plain text
    }
}

The advantage of this approach is that variables are not replaced in text that has already been replaced. So:

$foo = '{bar}';
$bar = 'bar';
$template = 'Lorem {foo} {bar} sit amet …';

Here the text would be replaced by Lorem bar {bar} sit amet … and not Lorem bar bar sit amet … as it would happen with your code, when {bar} is being replaced after {foo}.

Comments

2

It's a good Idea to use a template engine.

But your idea gets slow when the html is larger and get slower if you are need more variables. remember the replace is executed for every request.

you can use html files containing php tags for output (like <?=$var?>) and loops only. include them via "include" to your php code. thats much faster.

take a look at smarty. this is my favorite template engine for php. smarty has many good features like caching and it is extendable by plugins.

if you understand german take a look here. there is a small introduction into smarty.

Comments

1

Sure, you need to have the arguments in the right order though. str_replace()

Also might be a good idea to put the templates in their own file instead of keeping them in a string. Then just read them via file_get_contents() or similar.

Comments

1

If you do only variable expansion, you can do:

$str_template = "<html><head><title>$the_title</title><body>$the_content</body></html>";

It will work as well...
Several people wonder why there are template system when PHP is already a template system...

Of course, there are other advantages to these systems, like more secure access to variables, perhaps easier to handle by designers (or to generate by tools), limitation of logic in the view layer... Which is a common criticism of heavy template systems like Smarty, with so much logic that you can almost write another template system with it! ;-)

Some template systems, like Savant, takes a middle road. :-)

Comments

0

You simply can't make a template this way.
A real life template will require not only plain variables to fill but also several control structures, or at least some sort of blocks to control certain areas of the page (to make a loops for example).
So, you can't make a template with str_replace only. Some regexps and sophisticated parsing will be required too.

Thus, it's better to stick with a template engine which already at your disposal - a PHP itself!

1 Comment

PHP itself... The best answer!
0

Actually the answer from "Your Common Sense" is the right one. You could use PHP to use the current context and replace variables. Instead of writing the output, you could do ob_start() to hold it in a buffer and get the replaced content. Easy and elegant!

Comments

0

basically the idea is that one, but template are something that offers lot more, for example RainTPL is WYSIWYG, because it replace relative paths into the template with the server paths, in this way you design your template in local and it works on the server without needs to change img/css.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.