[devtalk] Straw poll - templating

Matthew Macdonald-Wallace matthew at truthisfreedom.org.uk
Fri Mar 20 07:26:28 GMT 2009


Quoting David Precious <davidp at preshweb.co.uk>:

> Matthew Macdonald-Wallace wrote:
>> I'm working on the development of a content and document management
>> system and I need a bit of advice on how to structure the templating
>> system.
>>
>> At the moment, there is a file called page.class.php and within this
>> class is a function that holds the xHTML for the site template.  This
>> is less than ideal so I'm thinking about moving to a more suitable
>> template format.
>
> It sounds almost as though you're trying to write your own templating
> system?
>
> If that's the case, I'd strongly suggest that it's a wheel that doesn't
> need re-inventing.
>
> Have a look at Smarty, which should take care of templating for you.
>
> (Personally I'm a Perl man and I use Template::Toolkit (or
> HTML::Template at work), but if I had to do something in PHP, I'd look
> to Smarty first for templating.

OK, thanks Dave, I'll take a look.

The code in it's current form is as follows:

===========================================================
/**
      * Displays the page (outputs HTML). This is the closest we have  
to a template, so any edits to the html here are reflected across the  
entire site.
      * @return none
      */
     function display()
     {
?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
     <head>
         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
         <meta name="description" content="<?php echo  
$this->description; ?>" />
         <meta name="keywords" content="<?php echo $this->keywords; ?>" />
         <meta name="author" content="<?php echo $this->author; ?>" />
         <link rel="stylesheet" type="text/css" href="<?php echo  
BASE_URL; ?>library/default.css" media="screen" />
         <title>
             <?php
             echo SITETITLE." - ".$this->pageTitle;
             ?>
         </title>
     </head>
     <body>
         <div id="content_wrapper">
             <div id="header">
                 <?php
                 echo SITETITLE;
                 ?>
                 <p>
                     <?php
                     echo SITETAG;
                     ?>
                 </p>
             </div>
             <div id="maincontent">
                 <h2>
                     <?php
                     echo $this->pageTitle;
                     ?>
                 </h2>
                 <?php
                 echo $this->pageContent;
                 ?>
             </div>
             <div id="nav">
                 <ul>
                     <?php
                     if ($_SESSION['userId'] == "")
                     {
                         echo "<li style=\"text-align:right;\"><a  
href=\"".BASE_URL."login.php?action=sf\">Login</a></li>";
                     }
                     else
                     {
                         echo "<li style=\"text-align: right;\">Logged  
in as <br />".$_SESSION['userName'].".<br /> <a  
href=\"".BASE_URL."login.php?action=logout\">Log out</a></li>";
                     }
                     ?>
                 </ul>
             </div>
             <div id="footer">
                 <?php
                 /**
                  * if gacode is set, show the google analytics code,  
otherwise...dont... :o)
                  */
                 if ($gacode != "")
                 {
                 ?>
                 <script type="text/javascript">
                     var gaJsHost = (("https:" ==  
document.location.protocol) ? "https://ssl." : "http://www.");
                     document.write(unescape("%3Cscript src='" +  
gaJsHost + "google-analytics.com/ga.js'  
type='text/javascript'%3E%3C/script%3E"));
                 </script>
                 <script type="text/javascript">
                     try {
                         var pageTracker = _gat._getTracker("<?php  
echo GACODE ?>");
                         pageTracker._trackPageview();
                     }
                     catch (err) {
                     }
                 </script>
                 <?php
                 } //end if
                 ?>
             </div>
         </div>
     </body>
</html>
<?php
}
==============================

The variables are setup in either the page that calls this class  
(using methods such as $p->setTitle(String title) or  
$p->setContent(String content) ) or the main config file for the  
system, however having to edit the core of the system just to change  
the layout of the site seems like a bit of a PITA if this is to be  
used across many sites.

I'll see if I can adapt it to one of the templating systems, although  
I was hoping to make it even more accessible to people who are not  
necessarily PHPDev's by not requiring people to learn a complete  
templating system.  Maybe that's not the way to go on this one, I  
don't know.  I'm meeting one of the people that's working with me on  
this today to discuss this issue so I thought I'd ask those that are  
"in the know" to take some ideas with me!

Cheers,

M.

-- 
Matthew Macdonald-Wallace
matthew at truthisfreedom.org.uk
http://www.truthisfreedom.org.uk/


More information about the devtalk mailing list