PHP Template way of coding for wordpress theme development

here is the simple PHP template system i have found

The Class (template.class.php)

Read More
         <?
         class Template {
         public $template;
         function load($filepath) {
          $this->template = file_get_contents($filepath);
          }
         function replace($var, $content) {
          $this->template = str_replace("#$var#", $content, $this->template);
         }
         function publish() {
          eval("?>".$this->template."<?");
           }
          }
         ?>

The Template File (design.html)

This file will contain the design of your web site and the blank fields that will be merged with content data.

             <html>
            <head>
            <title>#title#</title>
            </head>
             <body>
            <h3>Hello #name#!</h3>
           <p>The time is: #datetime#</p>
          <? echo "<p>Embedded PHP works too!</p>"; ?>
          </body>
         </html>

Usage (index.php)

Now we will create a script that load the template and use the class to merge the data.

       <?
        include "template.class.php";

        $template = new Template;
        $template->load("design.html");
        $template->replace("title", "My Template Class");
        $template->replace("name", "William");
        $template->replace("datetime", date("m/d/y"));
        $template->publish();
         ?>

Now for a simple wordpress post loop index.php

           <?php
          get_header();
        if (have_posts()) :
         while (have_posts()) :
         the_post();
           the_content();
          endwhile;
          endif;

          ?>

Question is, how to use above wordpress loop and the top PHP template system together,
this is to convert an html template into a wordpress theme.

Related posts

Leave a Reply

2 comments

  1. My first answer to your question is another question: Why?

    PHP is a template language. You need this separation only in cases where the Designer and the programmer have two very different jobs. But someone writing WordPress themes has to know PHP anyway: Just look at the custom header and background set ups, at add_theme_support, wp_enqueue_style and similar cases where PHP, HTML and CSS are tied together.

    To answer the how … take a look at TwentyEleven’s single.php. There is a part where you could load a template engine:

    <?php get_template_part( 'content', 'single' ); ?>
    

    You could instead use:

    locate_template( 'php/template.class.php', TRUE, TRUE );
    $template = new Template;
    $template->load( 'templates/content-single.html' );
    $template->replace( 'title', get_the_title() );
    

    Some template functions aren’t build to be used like this, for example the_content(): It offers no parameter to just return a string and it changes the output of get_the_content(). So you would have to rewrite some functions just use your extra template engine.
    For loops, nav menus or widgets it is even harder to separate logic from markup. Yes, it would be nice if there were some separation … but I don’t see a way to do this in WordPress.

  2. here is a quick modification of your class to handle a basic loop:

    class Template {
        public $template;
        //new var to hold the loop items
        public $loop_items;
        //new var to hold loop template
        public $loop_template;
    
        function load($filepath) {
            $this->template = file_get_contents($filepath);
            $this->loop_template = substr($this->template, strlen("#Loop_Start#")+strpos($this->template, "#Loop_Start#"), (strlen($this->template) - strpos($this->template, "#Loop_End#"))*(-1));
            $this->loop_items = array();
        }
    
        function replace($var, $content) {
            $this->template = str_replace("#$var#", $content, $this->template);
        }
    
        //store item from loop
        function do_loop(){
            if (have_posts()){
                while (have_posts()){
                    the_post();
                    $this->loop_items[] = array('title' => get_the_title(), 'content' => apply_filters("the_content",get_the_content()));
                }
            }else{
                $this->loop_items[] = array("title" => "Not Found", 'content' => "Sorry, nothing found");
            }
        }
    
        function replace_loop(){
            $loop_html = '';
            foreach($this->loop_items as $key -> $arr){
                $temp = str_replace("#loop_title#",$arr['title'],$this->loop_template);
                $temp = str_replace("#loop_content#",$arr['content'],$temp);
                $loop_html .= $temp;
            }
            $this->template = str_replace($this->loop_template,$loop_html,$this->template);
        }
    
        function publish() {
            eval("?>".$this->template."<?");
        }
    }
    

    Now just make sure your html has this tokens:

    • #Loop_Start# – place before your loop template.
    • #Loop_End# – place after your loop template.
    • #loop_title# – place where you want the item title inside your loop template.
    • #loop_content# – place where you want the item content inside your loop template.

    for example:

    <html>
        <head>
            <title>#title#</title>
        </head>
         <body>
            #Loop_Start#
            <h3>Hello #loop_title#</h3>
            <p>#loop_content#</p>
            <? echo "<p>Embedded PHP works too!</p>"; ?>
            #Loop_End#
        </body>
    </html>
    

    and you should call it like this:

    include "template.class.php";
    
    $template = new Template;
    $template->load("design.html");
    $template->replace("title", "My Template Class");
    $template->do_loop();
    $template->replace_loop();
    $template->publish();