tags:

views:

81

answers:

5

I'm new to PHP, and I'm looking to do what I'd normally do with Struts Tiles in JSP. I'm looking for something like:
Template.php:

<html><body>
 <div class="header"></div>
 <div class="contents">
  <#insert sectionName="pageContents">
 </div>
</body></html>

Content.php:

<#template name="Template.php">
 <#section name="pageContents">
  <div>
   <?php doSomething(); ?>
  </div>
 </#section>
</#template>

Such that a request for Content.php returns the contents of Content.php included into Template.php appropriately. I'm looking for the fastest solution in terms of minimal installation/configuration time.

+3  A: 

Have a look at Smarty: Template Engine

echo
+2  A: 

I think Smarty might be a bit overkill in your case, since you ask for a basic PHP templating.

You could take a look at Lerdorf's article on the No-framework MVC framework or you could go for one of these template engines:

  • PHPTAL (although some aspects of the PHPTAL syntax is on the edge of insanity)
  • Twig
  • Savant3

Without knowing anything about Struts I think I'd check out Twig first. Although it is a fairly new project I think that it has some really nice features that you might be interested in.

phidah
I really appreciate the use of Twig
Thomas
+1  A: 

Personally I've found every PHP templating overkill. The price of learning and keeping up with the templating engine is just too high. It's often much easier to do it in PHP, with enabled short (ASP-style) tags if you're into that.

If you want, you can wrap the PHP templating in a simple class yourself, or use this:

Still want a "real template"? Check out H2O, it looks nice (but I still think it's overkill)

Good luck!

dyve
+2  A: 

You can use any number of php templating systems, but I'd recommend you use php's native capabilities. There's no additional performance overhead, and nothing to install or configure.

For instance to mimic the example above, you could do something like:

Template.inc:


<html><body>
 <div class="header"></div>
 <div class="contents">
  <?php print $pageContents
  //Or if your php installation supports short tags
  //<?=$pageContents?>
  ?>
 </div>
</body></html>

Content.php


<?php
  $something = doSomething();

  $pageContents = <<<CONTENT

<div>
  $something
</div>

CONTENT;

  include "template.inc";

Of course, there are lots of other ways of accomplishing the example above. That's another upside to using native capabilities - you can arrange and configure your own templating system to work the way you prefer.

emmychan
Awesome. Kills my syntax highlighting, but it suits my purposes for the time being, and it should be easy enough to move to another template engine once I've done some more reading.
T.R.
+1 for not using a template engine like Smarty
Gordon
One more thing, you should really look at configuring php to support short tags: http://www.php.net/manual/en/ini.core.php#ini.short-open-tagAlso, I would highly recommend using the alternative syntax for control structures embedded into templates:http://us2.php.net/manual/en/control-structures.alternative-syntax.php
emmychan
A: 

PHP Tiles is a clone of Apache Tiles.

It defaults to using PHP as it's "template engine", but you can use what ever you like (like how Tiles supports JSP or Velocity, etc).

Evan