PHP HTML Template with Loop capabilities

前端 未结 3 505
故里飘歌
故里飘歌 2021-01-07 07:28

I would like to ask some help and ideas on how to implement a loop inside the template. I can do foearch below but how can i include it to the template and show it in the re

3条回答
  •  礼貌的吻别
    2021-01-07 07:46

    Just don't re-invent the wheel, PHP works wonderfully as a templating language:

    The template class

    template = $filepath;
       }
       function replace($var, $content)
       {
          $this->vars[$var] = $content;
       }
       function publish()
       {
           extract($this->vars);
           include($this->template);
       }
    }
    ?>
    

    The template design.phtml

    
    
    <?php echo $title ?>
    
    
    
      

    Hello

    The time is:

    Embedded PHP works too!

    "; ?>

    The use is pretty much the same, just assign more than one row to make use of it:

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

    Hope this is helpful.

提交回复
热议问题