Jump to content

Made template class, how to incorporate into overall layout?


Zurev

Recommended Posts

So I made a template class, it's very simple, pretty much allows the usage like so:

$template = new template("blogpost");
$template->fillBraces(array(
    "TITLE"  => "How to bathe your chimpanzee",
    "AUTHOR" => "Jim Bo James"
));
$template->render();

 

Simple, uses tpl files, similar to what phpbb used a long time back, not sure if they still do.

 

My main goal for this is to have a file that has something like this:

{HEADER}

{MAIN_CONTENT}

{FOOTER}

 

and the above usage to stay the same, however that would fill the main_content part, and the header and footer would be grabbed from header/footer tpl files. Any idea on suggestions? The full class is posted below.

 

 

class template
{
    protected $_viewContents;

    function __construct($viewName)
    {
        $this->_viewContents = file_get_contents(TEMPLATE.$viewName.TEMPLATE_EXT);
        return;
    }

    function sourceOf($fileName)
    {
        return @file_get_contents(TEMPLATE.$fileName.TEMPLATE_EXT);
    }

    function fillLayout()
    {
        
    }

    function fillBraces($information, $replacement=NULL)
    {
        if (is_array($information))
        {
            foreach ($information as $key => $val)
            {
                $this->_viewContents = str_ireplace("{".$key."}", $val, $this->_viewContents);
            }
            return;
        } elseif ($replacement!=NULL)
        {
            $this->_viewContents = str_ireplace("{".$information."}", $replacement, $this->_viewContents);
            return;
        }
    }

    function render()
    {
        echo $this->_viewContents;
    }
}

 

 

Thanks!

Link to comment
Share on other sites

Firstly, I'll just let you know that I think template classes like this (and any others really) are a complete waste of resources (IMO). There are much more efficient methods of creating templates using pure php.

 

Anyway, to do what your looking for you would need another method to replace render() that simply returns the parsed template. You would then parse the main_content.tpl template and store it within a variable ready to use in your layout.tpl.

Link to comment
Share on other sites

Firstly, I'll just let you know that I think template classes like this (and any others really) are a complete waste of resources (IMO)

 

I felt it might be seeing as how it's taking a partially filled file's contents, editting them, and then re-rendering it. Seems like an awful lot of running around to do, however I'm unsure of another way to do it.

 

There are much more efficient methods of creating templates using pure php.

 

Care to elaborate on that bit?

Link to comment
Share on other sites

include() does a pretty good job, and you can simply use a naming convention and some simple functions that support finding the right files by naming convention, and supporting the idea of loading of partials inside a template.  A couple of simple wrappers around include, that use PATH variables to make sure that the inclusions are safe, and you'll have a lot of what you need.

 

There's also tried and true template libraries like those available in zf  (Zend_view), in PEAR and that oldie but goodie smarty.  They each have a point of view and a reason for being, so it very much depends on what your goals are. 

Link to comment
Share on other sites

Care to elaborate on that bit?

 

I'm more inclined to stick with a little bit of php within my templates. Your template class means that a designer would still need to learn a new syntax anyway, so why not have them learn a little bit of php, it's allot more efficient. Especially considering you haven't even implemented loops or anything yet.

 

Let's create a simple 'blog' example.

 

header.php

<html>
  <head>
    <title><?php echo $this->title; ?></title>
  </head>
  <body>

 

footer.php

  </body>
</html>

 

blog.php

<?php include 'header.php'; ?>
<?php foreach ($this->blogs as $blog): ?>
  <div>
    <span class="title"><?php echo $blog->title; ?></span>
    <span class="content"><?php echo $blog->content; ?></span>
    <span class="footer"><a href="/blog/comment/add/id/<?php echo $blog->id; ?>">Leave a comment</a></span>
  </div>
<?php endforeach; ?>
<?php include 'footer.php'; ?>

 

Now, the View class.

 

<?php
class View
{
  public function render($view)
  {
    ob_start();
    include $view;
    echo ob_get_clean();
  }
}

 

Now, your client code.

 

<?php
if ($result = mysql_query("SELECT title, content, id FROM blogs")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_object($result)) {
      $blogs[] = $row;
    }
    $view = new View;
    $view->title = 'blogs'; // page title
    $view->blogs = $blogs;
    $view->render('blogs.php');
  }
}

 

Of course this is a VERY simplified version, but hopefully you get the idea. This is roughly how view work in allot of the frameworks around.

Link to comment
Share on other sites

Agree fully with Pikachu..sorry, thorpe. PHP *is* a templating language, it is meant for stuff like this. Originally, this was the languages primary purpose. The above example is very well thought out explanation and example.

 

Another idea might be to actually review some of the frameworks (like Zend) and apps (like Wordpress) and see how their MVC layer works. This will give you a more in depth look.

Link to comment
Share on other sites

Agree fully with Pikachu..sorry, thorpe. PHP *is* a templating language, it is meant for stuff like this. Originally, this was the languages primary purpose. The above example is very well thought out explanation and example.

 

Another idea might be to actually review some of the frameworks (like Zend) and apps (like Wordpress) and see how their MVC layer works. This will give you a more in depth look.

 

Yeah the view thorpe gave me insight to is obviously very similar to the usage of codeigniter, so I may just have to take a look at how it works!

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.