Pogledajte određenu poruku
Staro 04. 06. 2006.   #1
Ilija Studen
Direktor Kombinata
Invented the damn thing
 
Avatar Ilija Studen
 
Datum učlanjenja: 07.06.2005
Poruke: 2.669
Hvala: 44
119 "Hvala" u 64 poruka
Ilija Studen će postati "faca" uskoroIlija Studen će postati "faca" uskoro
Default Mala Template klasa

Nije template jezik već jednostavna klasa koja izoluje template od ostatka aplikacije i omogućava lako baratanje sa njima. Daklen:

PHP kôd:
<?php

  
include 'Template.class.php';
  
  
tpl_assign('firstname''Ilija');
  
tpl_assign('lastname''Studen');
  
tpl_display('templates/something.php');  

?>
A something.php izgleda:

PHP kôd:
<?php if($firstname && $lastname) { ?>
<p>Dobrodosao <?= $firstname ?> <?= $lastname ?></p>
<?php } else { ?>
<p>Ne znam tvoje ime i prezime :(</p>
<?php ?>
Pored display() koji printuje kod ima i fetch() metod koji otvara output buffer, poziva display i vraća sadržaj output buffera nakon što je template pozvan (tj. vraća šta god je template printao kao string). Tu je i assign_by_ref funkcija.

I... to je to Skoro godinu dana radim sa ovom klasom i nije mi se ukazala potreba za bilo čim složenijim. Maleno, jednostavno, radi posao i ne smeta.

PHP kôd:
<?php

  
/**
  * Template class
  *
  * This class is template wrapper, responsible for forwarding variables to the
  * templates and including them.
  * 
  * @version 1.0
  * @author Ilija Studen <ilija.studen@gmail.com>
  */
  
class Template {
    
    
/**
    * Array of template variables
    *
    * @var array
    */
    
var $vars = array();
    
    
/**
    * Assign variable value to the template
    *
    * @access public
    * @param string $name Variable name
    * @param mixed $value Variable value
    * @return boolean
    */
    
function assign($name$value) {
      if(
trim($name) == '') return false;
      
$this->vars[trim($name)] = $value;
      return 
true;
    } 
// assign
    
    /**
    * Assign value by reference
    *
    * @access public
    * @param string $name
    * @param mixed $value
    * @return boolean
    */
    
function assignByRef($name, &$value) {
      if(
trim($name) == '') return false;
      
$this->vars[trim($name)] = $value;
      return 
true;
    } 
// assignByRef
    
    /**
    * Display template and retur output as string
    *
    * @access public
    * @param string $template Absolute path template path
    * @return string
    */
    
function fetch($template) {
      
ob_start();
      
$display $this->display($template);
      if(
$display === false) {
        
ob_end_clean();
        return 
false;
      } 
// if
      
return ob_get_clean();      
    } 
// fetch
    
    /**
    * Display template
    *
    * @access public
    * @param string $template Template path or path relative to templates dir
    * @return boolean
    */
    
function display($template) {
      return 
$this->includeTemplate($template);
    } 
// display
    
    /**
    * Include specific template
    *
    * @access public
    * @param string $template Absolute template path
    * @return null
    */
    
function includeTemplate($template) {
      if(
file_exists($template)) {
        foreach(
$this->vars as $k => $v) {
          if(!isset($
$k)) $$k $v;
        } 
// foreach
        
        
include $template;
        return 
true;
      } else {
        return 
false;
      } 
// if
    
// includeTemplate
    
    /**
    * Return template service instance
    *
    * @access public
    * @param void
    * @return Template
    */
    
function &instance() {
      static 
$instance;
      if(!
is_object($instance) || !is_a($instance'Template')) {
        
$instance = new Template();
      } 
// if
      
return $instance;
    } 
// instance
  
  
// Template
  
  // ==============================================================
  //  Shortcut methods
  // ==============================================================
  
  /**
  * Assign template variable
  *
  * @access public
  * @param string $varname Variable name
  * @return boolean
  */
  
function tpl_assign($varname$varvalue) {
    
$template_engine =& Template::instance();
    return 
$template_engine->assign($varname$varvalue);
  } 
// tpl_assign
  
  /**
  * Assign variable by reference
  *
  * @access public
  * @param string $varname
  * @param mixed $varvalue
  * @return boolean
  */
  
function tpl_assign_by_ref($varname, &$varvalue) {
    
$template_engine =& Template::instance();
    return 
$template_engine->assignByRef($varname$varvalue);
  } 
// tpl_assign_by_ref
  
  /**
  * Render template and return it as string
  *
  * @access public
  * @param string $template Template that need to be rendered
  * @return string
  */
  
function tpl_fetch($template) {
    
$template_engine =& Template::instance();
    return 
$template_engine->fetch($template);
  } 
// tpl_fetch
  
  /**
  * Render specific template
  *
  * @access public
  * @param string $template Template that need to be rendered
  * @return boolean
  */
  
function tpl_display($template) {
    
$template_engine =& Template::instance();
    return 
$template_engine->display($template);
  } 
// tpl_display

?>
Priloženi fajlovi
Tip fajla: zip template_test.zip (1,8 KB, 1218 pregleda)

Poslednja izmena od Ilija Studen : 04. 06. 2006. u 18:01.
Ilija Studen je offline   Odgovorite uz citat