« powrót

Własna klasa do obsługi szablonów [PHP]

Opublikowano: 2009-11-07 , wyświetlono: 33364

Przedstawiam klasę do obsługi szablonów, której używam w swoich projektach. Poniżej kod, klasy, przykład użycia oraz szablon.

Kod źródłowy klasy


<?php

//=====================================================
//
// (c) ChinaSoft 2004
// template class
//
//=====================================================

class Template
{
  var $fileName;
  var $listTag;
  var $srcHtml;
  var $outHtml;
  var $path;

  // constructor
  //----------------------------------------------------------
  function Template($file = "")
  {
    $this->fileName = $file;
    $this->listTag = array();
    $this->path = "";

    if ($file != "")
    {
      $this->load();
    }
  }

  // set htmlText
  //----------------------------------------------------------
  function SetText($text)
  {
    $this->srcHtml = $text;
  }

  // set path
  //----------------------------------------------------------
  function SetPath($str)
  {
    $this->path = $str;
  }

  // set file
  //----------------------------------------------------------
  function SetFile($fname)
  {
    $this->fileName = $fname;
    $this->load();
  }

  // load file
  //----------------------------------------------------------
  function load()
  {
    $fname = $this->path . $this->fileName;
    
    // print "#load() " . $fname;
    
    $len = filesize($fname);
    if ($len > 0)
    {
      $fileHandle = fopen($fname, "r");
      $this->srcHtml = fread($fileHandle, filesize($fname));
      fclose($fileHandle);
    }
    else
      $this->srcHtml = "";
  }

  // parse and return template
  //----------------------------------------------------------
  function parse()
  {
    $this->outHtml = $this->srcHtml;
    foreach ($this->listTag as $tag => $value)
    {
      $this->outHtml = str_replace($tag, $value, $this->outHtml);
    }
    return $this->outHtml;
  }

  // assign tag with value
  //----------------------------------------------------------
  function Assign($tag, $value)
  {
    $strTag = "{\$" . $tag . "}";
    $this->listTag[$strTag] = $value;
  }

  // clear all pairs
  //----------------------------------------------------------
  function Clear()
  {
    $this->listTag = array();
  }


  // output html
  //----------------------------------------------------------
  function Out()
  {
    $this->parse();
    print ($this->outHtml);
  }

  // return html
  //----------------------------------------------------------
  function Get()
  {
    $this->parse();
    return ($this->outHtml);
  }

  //
  // biuld and return options tag
  function createOptions($valueList, $outputList, $selected)
  {
    $optionStr = "";

    for ($i = 0; $i < count($valueList); $i++)
    {
      $optionStr .= "<option value=\"" . $valueList[$i] . "\"";

      // check selection
      if ($selected == $valueList[$i])
        $optionStr .= " selected";

      $optionStr .= ">";

      if (isset($outputList[$i]))
        $optionStr .= $outputList[$i] . "</option>\n";
      else
        $optionStr .= "</option>\n";
    }

    return $optionStr;
  }

  // assign tag with options/values
  //----------------------------------------------------------
  function AssignOptions($tag, $valueList, $outputList, $selected = 0)
  {
    $strTag = "{\$" . $tag . "}";
    $this->listTag[$strTag] = $this->createOptions($valueList, $outputList, $selected);
  }

}
?>

Przykład użycia


<?php
include_once("chinasoft.template.php");
?>

<html>
<body>
<h1>Template class test</h1>
<hr>

<?php

$tpl = new Template("test_template.txt");
$tpl->Assign("name", "Jacek");
$tpl->Assign("id", 1);
$tpl->AssignOptions("combo_test", array(1, 2, 3), array("one", "two", "three"), 3);
$tpl->Out();

$tpl->Assign("name", "Pawel");
$tpl->Assign("id", "2.22");
$tpl->AssignOptions("combo_test", array("one", "two", "three"), array(1, 2, 3), "tow");
$tpl->Out();

$tpl->Clear();
$tpl->Out();

?>

<hr>
</body>
</html>

Użyty w przykładzie szablon


<p>
  to jest test {$name} jest rowne {$id}.
</p>
<hr>
<p>
  test combo box'a
</p>
<p>
  <form>
  Wybor: <select name="test" size="1">
    {$combo_test}
  </select>
  </form>
</p>


Komentarze: