发新话题
打印

HTML_Template_Flexy学习笔记(二)

HTML_Template_Flexy学习笔记(二)

page_render类
复制内容到剪贴板
代码:
<?php   

require_once 'PEAR.php';
/* configure the application - probably done elsewhere */
require_once 'HTML/Template/Flexy/Element.php';
require_once 'HTML/Template/Flexy.php';
$options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
$config = parse_ini_file('example.ini',TRUE);
$options = $config['HTML_Template_Flexy'];


/* the page controller class */

class page_render
{
   
    var $template = "home.html"; // name of template
    var $title;                  // this relates to {title};
    var $numbers = array();      // this relates to {numbers} , used with foreach
    var $anObject;
   
    var $element = array();      // this is where the elements are stored
   

   //for test

v  ar $a = array(
      "dog" => "cat",
     "fire" => "water"
     );

     var $b = array('a','b','c');


    /* start section - deals with posts, get variables etc.*/

    function page_render()
    {
        $this->start();
        $this->output();
    }


    function start()
    {
        // the title
        $this->title = "page's title";
        
        // store an object.
        $this->anObject = new StdClass;
        
        // assign a value to a member.
        $this->anObject->member = 'Object Member';
        
        // create an HTML Element for the form element.
        $this->elements['input'] = new HTML_Template_Flexy_Element;
        
        // assign a value to it
        $this->elements['input']->setValue('Hello~~~~~');
        
        
        for ($i = 1;$i< 5;$i++) {
            $this->numbers[$i] = "Number $i";
        }
    }
   
    /* output section - probably best to put this in the default_controller class */
   
    function output() {
        $output = new HTML_Template_Flexy();
        $output->compile($this->template);
        $output->outputObject($this,$this->elements);
    }
   
   
    function someMethod() {
        echo "<B>Hello From A Method</B>";
    }
   
   
   
}

/* 实际情况中,下面的page_render类的实例化,可以在主page controller 类中,用一个工厂函数搞定*/

new page_render;

?>
template文件:home.html
复制内容到剪贴板
代码:
<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
  <H1>{title}</H1>
  
  
  
    <form name="form">
    <table>
      <tr>
        <td>
        Input Box: <input name="input">
        </td>
      </tr>
      
       <tr flexy:foreach="numbers,number,string">
        <td>
        <a href="mypage.html?id=%7Bnumber%7D">{string}</a>
        </td>
      </tr>            
    </table>
</form>
   
     this is number 2 : {numbers[2]}
   
   
    This is a {anObject.member}
   
    {someMethod()}
   
    {someMethod():h}

<table border>
  <tr flexy:foreach="a,k,v">
    <td border>k is {k}, and v is {v}</td>
  </tr>
</table>
<table border>
  <tr flexy:foreach="b,v">
    <td border>v is {v}</td>
  </tr>
</table>
      
  </body>
</html>
专注于DEDE采集和数据深度挖掘

TOP

发新话题