PHP Classes

File: class.template.php

Recommend this page to a friend!
  Classes of Aleksandar Zivanovic   Simple PHP Template Engine   class.template.php   Download  
File: class.template.php
Role: Class source
Content type: text/plain
Description: TemplateEngine Class/Core
Class: Simple PHP Template Engine
Template engine based on text replacement
Author: By
Last change: now you can add html tag with addTag function
Date: 14 years ago
Size: 6,205 bytes
 

Contents

Class file image Download
<?php

/**
 * Description of TemplateEngine
 * Date: Friday, 9 Jul 2010,
 * Time: 18:50,
 * This is a simple TemplateEngine that helps you to parse php into html
 *
 * @author Coa
 */
class Template {

    protected
$_templatefolder;
    protected
$_extension;
    protected
$_template;
    protected
$_text;

   
/*
      __construct here define Templatefolder and Template extension;
      example: $tpl = new Template('templates','tpl');
      this will look into folder templates which is in that same folder where the file that call loadTemplate;
     */

   
public function __construct($templatefolder, $extensiton) {
       
$this->_templatefolder = $templatefolder;
       
$this->_extension = $extensiton;
    }

   
/*
      loadTemplate here set which template to load,
      example: $tpl = new Template('templates','tpl');
      $tpm->loadTemplate('test');
      it will look into folder templates for file called test.tpl, if file doesn't exist,
      it will return an error.
     */

   
public function loadTemplate($template) {
        if (
$this->_templatefolder == '') {
            if (
file_exists($template . '.' . $this->_extension)) {
               
$this->_template = $template . '.' . $this->_extension;
            } else {
                echo
'<span style="color: red">Template ' . $template . '.' . $this->_extension . ' doesn\'t exist.</span>';
            }
        } else {
            if (
file_exists($this->_templatefolder . '/' . $template . '.' . $this->_extension)) {
               
$this->_template = $this->_templatefolder . '/' . $template . '.' . $this->_extension;
            } else {
                echo
'<span style="color: red">Template ' . $template . '.' . $this->_extension . ' in folder ' . $this->_templatefolder . ' doesn\'t exist.</span>';
            }
        }
        return
1;
    }

   
/*
     * Function that opens form with given parameters.
     * example $tpl->openForm('test', '1', 'post', '');
     */

   
public function openForm($name, $id, $method, $action) {
        echo
'<form method=' . $method . ' action=' . $action . ' name=' . $name . ' id=' . $id . '>';
        return
1;
    }

   
/*
     * Function that close form
     *
     */

   
public function closeForm() {
        echo
'</form>';
        return
1;
    }

   
/*
     * Function that make tags with parameters that are given by user
     * example $tpl->addTag(array(
     * 'input_submit' => arrray(
     * 'input' => array(
     * 'type' => 'submit',
     * 'value' => 'register',
     * 'newline' => '1'
     * )
     * )
     * ));
     * it will create now this <input type="submit" name="input_submit" id="input_submit" value="register" /><br />
     * for textarea: $tpl->addTag(array(
     * 'textarea' => array(
     * 'textarea1' => array(
     * 'cols' => 10,
     * 'rows' => 10,
     * 'value' => 'test'
     * )
     * )
     * ));
     * it will create now this <textarea id="textarea1" name="textarea1" cols=10 rows=10>test</textarea>
     * you can create more than one tag by calling only once this function
     * example $tpl->addTag(array(
     * 'input_text' => array(
     * 'input' => array(
     * 'type' => 'text',
     * 'value' => 'test',
     * 'newline' => '1'
     * )
     * ),
     * 'text_area1' => array(
     * 'textarea' => array(
     * 'cols' => 10,
     * 'rows' => 20,
     * 'value' => 'test2'
     * )
     * )
     * ));
     * it will create now this
     * <input type="submit" name="input_submit" id="input_submit" value="register" /><br />
     * <textarea id="text_area1" name="text_area1" cols=10 rows=10>test2</textarea>
     */

   
public function addTag($infos) {
        foreach (
$infos as $key => $value) {
           
$new_line = '';
           
$show = '';
           
$data = '';
           
$id = $key;
            foreach (
$value as $key => $value) {
               
$tag = $key;
                foreach (
$value as $key => $value) {
                    if (
$key == 'newline' && $value == '1') {
                       
$new_line = '<br />';
                    } else if (
$key == 'newline') {
                       
$data .= '';
                    } else if (
$key == 'value') {
                       
$show = 'value="' . $value . '"';
                    } else {
                       
$data .= $key . '="' . $value . '" ';
                    }
                }
            }
            switch (
$tag) {
                case
'textarea':
                   
$to_return = '<textarea ' . $data . ' id="' . $id . '" name="' . $id . '" >' . substr($show, 7, -1) . '</textarea>' . $new_line;
                    break;
                default :
                   
$to_return = '<' . $tag . ' ' . $data . ' ' . $show . ' id="' . $id . '" name="' . $id . '" />' . $new_line;
            }
            echo
$to_return;
        }
        return
0;
    }

   
/*
      set here replace the text in tpl file that is loaded,
      example
      test.tpl content:
      [hello]<br />
      [name]<br />
      [time]
      to replace the text write $tmp->set(array(
      'hello' => 'Hello World',
      'name' => 'John Smith',
      'time' => date("H:i",time())
      ));
      after this we call for output function
     */

   
public function set($replace = array()) {
       
$what = array();
       
$with = array();
        foreach (
$replace as $key => $value) {
           
$what[] .= '[' . $key . ']';
           
$with[] .= $value;
        }
       
$this->_text = str_replace($what, $with, file_get_contents($this->_template));
        return
1;
    }

   
/*
      output show html content in this case that we used in set webbrowser will show
      Hello World
      John Smith
      19:05
     */

   
public function output() {
        echo
$this->_text;
        return
1;
    }

}
?>