tags:

views:

1356

answers:

6

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?

+2  A: 

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?

Greg Hewgill
sorry I meant faster than the regular templating approach like the Smarty way.No I'm not running into performance problems, just curiosity. Thanks.
Shawn
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.
Greg Hewgill
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.
Greg Hewgill
smarty parses the template once to generate php code.the php code is executed then for each request
Bernd Ott
A: 

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.

sirlancelot
+1  A: 

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 ) 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.

Bernd Ott
+1  A: 

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( $str_template, array('{the_title}', '{the_content}'), array($title, $content));

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

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

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}.

Gumbo
A: 

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. :-)

PhiLho