Jump to content

Template Engine Recommendations/Comments/Advice


samona

Recommended Posts

Hi,

 

I was wondering what PhpFreaks users would recommend as far as template engines.  I would like to build a small website and I was thinking of using the PEAR template engine ITX.  However, I'm not sure if this would be the best way.  I heard about Drupal and about how flexible it is, and was wondering if anyone thinks that that would be the best route. 

 

Thanks in advance for your advice and comments.

Firstly, Drupal is a CMS and framework, not exactly a template engine.

 

As for template engines.... why do you want to use one? All they really do is add another layer to your application which usually effects performance. A well designed application separates business logic from presentation logic and could use just a small subset of php itself within html to create much more efficient (and IMO easier to maintain) templates.

... A well designed application separates business logic from presentation logic and could use just a small subset of php itself within html to create much more efficient (and IMO easier to maintain) templates.

 

How do you separate business logic from presentation logic without a Template Engine? 

You might want to look at the MVC design pattern, its one easy way to separate your application from presentation.

 

But a simple example (without even going down the mvc road) could be something as simple as....

 

index.php

<?php
  $sql = "SELECT title, article FROM articles":
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $articles = array();
      while ($row = mysql_fetch_object($result)) {
        $articles[] = $row;
      }
    }
  }
  include 'template.php';
?>

 

template.php

<html>
<head>
  <title>articles</title>
</head>
<body>
  <?php foreach($articles as $article): ?>
  <div class="article">
   <div class="title"><?php echo $article->title; ?></div>
   <div class="content"><?php echo $article->content; ?></div>
  </div>
  <?php endforeach; ?>
</body>
</html>

 

Of course that a really simple example but you can easily see how you could build on and improve the idea.

I have made something like what you might want. Its at myphptemplate.co.cc. It has no editor, it just builds a menu from the files in the content folder and displays them in the content area. You can use headers for each page if desired. All the display and menuing is automatic, all you need to do is make the fragments and upload them to the right folders. The config file is fairly well commented. You can get it from myphptemplate.co.cc at the download page.

 

 

HTH

Teamatomic

You might want to look at the MVC design pattern, its one easy way to separate your application from presentation.

 

But a simple example (without even going down the mvc road) could be something as simple as....

 

Yes! I see what you're saying.  I'll also check out the MVC design pattern because it's the first of heard of it.  Thanks!

As thorpe pointed out, a template engine is not the same as a CMS.  You should make sure you really understand what they each are before you possibly confuse yourself.

 

Anyways, PHP is a template engine.  There's absolutely no reason to use any template language / engine such as smarty.  Any person that can learn a template language can just as easily learn the four or five language elements of PHP required to use it instead.  And since they'll be collaborating with a PHP developer anyways, the PHP developer will usually be on hand to answer simple questions about PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.