views:

88

answers:

2

Hello friend

I want to use a custom template system in my php application,

What I want is I want to keep away my php codes from design, I would like to use a tpl file for designs and a php file for php codes

I dont want to use any ready maid scripts. Can any one point out some links link or useful info how to build a php templating system to achieve this

Thank you

+1  A: 

The way I do it is to create a template file(.tpl if you wish) and insert markers which will be replaced with str_replace in PHP. The code will look something like this:

For template.tpl file

<body>
  <b>Something: </b> <!-- marker -->
</body>

For the PHP

$template = file_get_contents('template.tpl');
$some_data = 'Some Text'; //could be anything as long as the data is in a variable
$template = str_replace('<!-- marker -->', $some_data, $template);
echo $template;

That's it in a nutshell but it can get a lot more complex. The marker can be anything as long as it's unique.

Meloth
You will need to quote that filename, of course. :)
alex
Thanks, quotes added.
Meloth
that is absolutely unusable in the real world.
Col. Shrapnel
at least you will need blocks to make it usable
Col. Shrapnel
Please explain. I use this method every day and it works perfectly.
Meloth
+1  A: 

I want to keep away my php codes from design, I would like to use a tpl file for designs

...and mix your tpl codes with "design"!
what's the difference then? :)

PHP itself is efficient templating system.
And nowadays most developers agreed that dividing your PHP code to business logic part and display logic part is most preferable way.
It can be very limited subset of PHP of course. You will need an output operator (<?=$var?>) one, a condition <? if(): ?>...<? endif ?>, a loop <? foreach(): ?>...<? endforeach ?> and include.

An example of such a template:

<table>
<? foreach ($data as $row): ?> 
  <tr>
    <td><b><?=$row['name'] ?></td>
    <td><?=$row['date'] ?></td>
  </tr>
  <tr>
    <td colspan=2><?=$row['body'] ?></td>
  </tr>
  <? if ($row['answer']): ?>
  <tr>
    <td colspan=2 valign="top">
      <table>
        <tr>
          <td valign="top"><b>Answer: </b></td>
          <td><?=$row['answer'] ?></td>
        </tr>
      </table>
    </td>
  </tr>
  <? endif ?>
  <? if($admin): ?>
  <tr>
    <td colspan=2>
  <? if($row['del']): ?>
      <a href="/gb/?action=undelete&id=<?=$row['id']?>">show</a>
  <? else: ?>
      <a href="/gb/?action=delete&id=<?=$row['id']?>">hide</a>
  <? endif ?>
      <a href="/gb/?action=edit&id=<?=$row['id']?>">edit</a>
    </td>
  </tr>
  <? endif ?>
<? endforeach ?>
</table>
Col. Shrapnel
+1 Yep, this is a good answer. The only thing I'd add is to mention `short_tags` and that this won't work with it switched off.
alex
@alex there are many things than won't work if switched off. display_errors, PDO support, mod_rewrite - anything. That's not a reason to mention it. That's the purpose of such a configuration option - to be switched on and off. Nothing bad or even worth mentioning I'd say.
Col. Shrapnel