Jump to content

Generic Theming


dbo

Recommended Posts

I'm just full (or fool?) of thoughts today.

 

I'm wondering what approach you guys use for generic theming needs. I inherit a lot of sites that have something like this:

 

getHeader("About Us");

//page specific content

getFooter();

 

The problem with this is that I've usually got a custom index page and then a slightly different design for second level pages etc.

 

Something like Smarty is really overkill for me. I like CSS but am not sold on it completely, so I'd like to be able to swap out designs fairly easily.

 

Dunno what I'm asking exactly, just some general ideas. I'm all about ideas :)

Link to comment
Share on other sites

Smarty is just a big beast with it's own language. I think I want something generic that uses PHP as it's scripting language.

 

The next question is what makes the most sense for populating it. Do I use views to populate the template? Does the template system itself replace the view? I dunno yet. Trying to think through this carefully before I start writing a lot of code.

 

The way I look at it, I've got a solid datamodel that provides me a shitton of flexibility... so the hard part is done. Now I just need to think through the best way of bringing that to life.

Link to comment
Share on other sites

I don't believe template systems are required at all. They add extra overhead for very little gain IMO. As you stated yourself, you pretty much need to learn another language to learn the template engine, why not simply use minimal amount of php within your views instead.

 

Back to your question though (I think), there is no harm including views within views. Hell, you could even include another controller (generating more views) within a view if your framework is setup for it.

Link to comment
Share on other sites

Yeah, maybe I just need to think through a better way to construct the views.

 

I just like the idea of being able to drop in a new directory (theme) to change the entire look of a site. Though I guess, I could technically do the same thing with different views, it's just a matter of how the views get called.

Link to comment
Share on other sites

Yeah, I'm not on board with CSS. Maybe when we get standards compliant browsers that all do what they're told I will be :) It doesn't seem practical to me to spend a bunch of time tricking a particular browser to look correctly (*cough* ie *cough*) when I can drop stuff in a table and it looks right across the board.

 

That being said, I do think CSS has a place and I do use it. I'm just not committed to doing pure CSS layouts yet. There's just too much ambiguity.

Link to comment
Share on other sites

For anyone who is interested here's the very basic template class that I wrote up last night. It's based sort of off Smarty, Drupal, and an article found here: http://www.sitepoint.com/article/beyond-template-engine

 

class DTemplate
{
   private $path;
   private $vars;
   
   function DTemplate($path)
   {
      $this->path = $path;
      $vars = Array();
   }
   
   public function set($key, $value)
   {
      $this->vars[$key] = $value;
   }
   
   public function load()
   {
      extract($this->vars);
      ob_start();
      include($this->path);
      $contents = ob_get_contents();
      ob_end_clean();
      return $contents;
   }
}

 

Example template

<html>
<head>
<title><?php echo $page_title; ?></title>
</head>

<body>

<h1><?php echo $title; ?></h1>

<?php echo $content; ?>

</body>
</html>

 

And example usage:

$master = new DTemplate("masterpage.tpl.php");
$master->set("page_title", "Template Test");
$master->set("title", "My Title");
$master->set("content", "Content");
echo $master->load();

Link to comment
Share on other sites

i like the way CakePHP do it (sure, it's not original, but Cake first introduced me to the concept) - they have a master "layout" as well as individual views. The master layout contains the main structure of the page, ie what's consistent through the site. in the final 'render' method, the template requested is merged in with the layout and the final result returned.

 

i have to admit tho (and thorpe (i think) might be the one to agree) I love the way Django handle views and templates. Whilst some of the code may appear alien at first, the manual should give you another approach that is pretty good as far as building output goes - especially in terms of template inheritance.

 

http://www.djangoproject.com/documentation/templates/

 

 

Link to comment
Share on other sites

Great stuff. Thanks for the reply.

 

I'm looking into the article you posted right now... and I'm very much considering the approach, that you mentioned, Cake uses. It's actually something I've been pondering the last couple of days before I even started the endeavor.

 

getHeader();

getFooter();

 

This is clunky and  you have like an opening tag in getHeader that doesn't get closed up until get footer.

 

I think what I really need is a solid approach for my views. Having a master view that then calls additional views would probably work just fine. I think I just need to see a clean working example.

Link to comment
Share on other sites

i dont use this one for everything, and this may be an old one, but it has done me good for many things. here's my full class, based on the "Beyond Template Engines" article mentioned above:

 

<?php
class View
{
    public $vars = array();
    public $html;
    
    public $layout = null;
    private $config = array();
    
    
    function __construct() {    
        // config settings - eg, gzip, etc
        $this->config = Config::load('view');
        
        if (!isset($this->config['layout'])) {
            $this->config['layout'] = 'master.tpl';
        }
        $this->layout = $this->config['layout'];
        
        // i have some vars that can be set as default - eg, copyright messages, etc
        $pagevars = Config::load('page');
        
        if (sizeof($pagevars)) {
            $this->vars = array_merge($this->vars, $pagevars);
        }
    }
    
    /*
     * loads a template file and returns the output
     *
     */
    function load($filename) {
        extract($this->vars);
        ob_start();
        include(TEMPLATE_PATH . '/' . $filename);
        $this->html = ob_get_clean();
                
        return $this->html;
    }

    /**
     * loads/parses a template but puts the output into a template var
     *
     */
    function loadtovar($varname, $filename) {
        if ($html = $this->load($filename)) {
            $this->set($varname, $html);
        }
        else {
            return false;
        }
    }

    /*
     * sets a view var
     *
     */
    function set($varname, $value = '') {
        if (!is_array($varname)) {
            $this->vars[$varname] = $value;
        }
        else {
            foreach($varname as $key=>$var) {
                $this->vars[$key] = $var;
            }
        }
    }
        
    /*
     * displays the final output
     *
     */
    function render($return = false) {
        $this->vars['pagebody'] = $this->html;

        if ($this->layout) {
            ob_start();

            echo $this->load($this->layout);
            $html = ob_get_clean();
        }
        else {
            $html = $this->html;
        }

        if (!$return) {
            // gzip enabled by config?
            if (isset($this->config['gzip']) && $this->config['gzip'] == true) {
                ob_start();
                ob_start('ob_gzhandler');
                echo $html;
                ob_end_flush();
                header('Content-Length: '.ob_get_length());
                ob_end_flush();        
            }
            else {
                echo $html;
            }
        }
        else {
            return $html;
        }
    }
}
?>

 

i stripped out some code for sake of easier grasp, and it depends on a few things, so wont work "as is", but i'm sure you could work it out. TEMPLATE_PATH and the Config settings being the two main dependencies that shouldnt take much work do figure out how to remove.

 

usage:

 

$tpl = new View();
$tpl->set('title', 'Hello World!');
$tpl->loadtovar('body', 'mybody.tpl');
$tpl->load('mytemplate.tpl');

$out = $tpl->render();

 

the following is a layout example. master.tpl:

 

<html>
   <head>
   </head>
   <body>
      <?=$pagebody ?>
   </body>
</html>

 

and the following is an individual "view" template. mytemplate.tpl:

 

<h1><?=$title ?></h1>
<p><?=nl2br($body) ?></p>

 

and the following is an individual snippet, just used to show how 'loadtovar' would be used. mybody.tpl:

 

I am a page body.

Link to comment
Share on other sites

I *highly* reccomend smarty.

 

However, at times (on smaller projects) I tend to use a simple templating system so my customers can arrange their layouts however they wish.

 

Here is a very very simple template system that I wrote, and use ALL the time.

 

test.php

<?php
function gettemplate($template,$ending="html")
{
return str_replace("\"","\\\"",implode("",file($template.".".$ending)));
}

for($i = 0; $i < 20; $i++)
{
$title = "TITLE RETRIEVED THROUGH SOME QUERY".$i;
$text  = "I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE";
eval("\$entries .= \"".gettemplate("someentry")."\";");
}

eval("echo \"".gettemplate("entries")."\";");


?>

 

someentry.html

<br>
<table align=center width=200>
<tr>
<td><b>$title</b></td>
</tr>
<tr>
<td>$text</td>
</tr>
</table>
<br>

 

entries.html

<html>
<head>
</head>
<body align=center bgcolor=green>
$entries
</body>
</html>

 

 

Output:

outputqj2.jpg

<html>
<head>
</head>
<body align=center bgcolor=green>
<br>
<table align=center width=200>
<tr>
<td><b>TITLE RETRIEVED THROUGH SOME QUERY0</b></td>
</tr>
<tr>
<td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td>
</tr>
</table>
<br>
<br>

<table align=center width=200>
<tr>
<td><b>TITLE RETRIEVED THROUGH SOME QUERY1</b></td>
</tr>
<tr>
<td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td>
</tr>
</table>
<br>
<br>
<table align=center width=200>
<tr>
<td><b>TITLE RETRIEVED THROUGH SOME QUERY2</b></td>
</tr>

<tr>
<td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td>
</tr>
</table>
<br>
*** SNIP ***
<br>
<table align=center width=200>
<tr>
<td><b>TITLE RETRIEVED THROUGH SOME QUERY19</b></td>
</tr>
<tr>
<td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td>

</tr>
</table>
<br>

</body>
</html>

 

 

This may look as though it has security flaws, but a recent blog program I wrote for somebody, if someone enters in $dbname for intance, it just displays $dbname.

 

 

Whoever suggested not seperating design and code logic is completely wrong.  Every thing you do should be about seperating logic so that it's easier for designers and maintainers.

 

 

Also, yea Smarty, I've just recently started playing with it and it's really nice.

Link to comment
Share on other sites

Thanks for the reply.

 

I've actually been using the little templating system that I wrote on the last few things I've been working on. So far it's working out very well, but I need to play with it more to be sure. It's basically doing the same thing your system does, but uses output buffering and includes rather than eval.

Link to comment
Share on other sites

I'm familiar with CSS Zen garden, it's pretty cool. My thing is just that there's not enough cross browser support to get things to work correctly without doing hacks to get it to look right. It seems silly to me to spend endless hours trying to use CSS for a certain display, if I can do it quickly with tables and know that it's going to look right w/o any hacks... especially if I have a good templating system that allows me to change things from a single location to quickly change designs.

Link to comment
Share on other sites

Actually you just shift the burden of where the countless hours are spent :)). Maintaining a template engine for basic markup will be much more tedious than maintaining a CSS.

 

But, hey, I get it.. I am no CSS fan either.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.