I have been using includes for my entire career as a web programmer, and have not yet touched templates. I'm wondering if I should.
I was just wondering if all you out there could help me weight the pro's and con's of each.
I have been using includes for my entire career as a web programmer, and have not yet touched templates. I'm wondering if I should.
I was just wondering if all you out there could help me weight the pro's and con's of each.
No...
if you have specifically came up with the need for templates, have thought about writing your own one, and know what is going on... then looking at other templating systems might be worthwhile...
however... if you have just seen a lot about templates out there on the web and thought "gee, am i missing out on the new stuff nowadays" then the answer is NO
templates are more expensive by a lot over includes... they give you the edge that in basic usage as the syntax to make a page is usually smaller than the syntax to embed php... however they are more expensive to process...
you tell me which looks like better php
# data files
# template.data.php
hello there your variable is <?=$var1?>
hello there your variable is <?=$var1?>
hello there your variable is <?=$var1?>
# template.data
hello there your variable is {var1}
hello there your variable is {var1}
hello there your variable is {var1}
<?php
header('Content-type: text/plain');
# Include way of ttemplates
$var1 = 'Foo';
include('template.data.php');
echo "\n-----------------------\n\n";
# Template way of templates
$template = file('template.data');
$vars_to_replace = array(
'var1'=>'foo'
);
foreach ( $template as $line_num=>$line ) {
foreach ( $vars_to_replace as $name=>$value ) {
$line = preg_replace( "/\{$name\}/", $value, $line );
echo $line;
}
}
?>
infact the templating engines will make this even more complex by wrapping up the seconde chunck of my code in an object... simplifies typeing for you.. but it just hides the enormous dont-need-atude of template from you as it looks simple...
now i grant you in the template files... {var1} is slightly easier on the eyes than <?=$var1?> or even <?php echo $var1; ?>, however not enough to warrent the second chunck of code...
now templates also need to take into consideration loops... so there are ways to mimick your for, do.while, foreach, etc in the templating systems.. each of which has to be parsed... put into valid php form.. and then eval()ed... into operation...
punchline:
I'm wondering if I should
absolutely not unless you have to... there may come a situation where you are working with a team of html developers who don't know php, and you think it best to give them the {var1} because they can't be trusted with the <?=$var1?>... then you should look into templates....