2016-11-15 170 views
1

我想从PHP中分离出我的HTML,所以在搜索后我发现了一个很棒的小型PHP类,只是做了伎俩。唯一的问题是我尝试将2个模板合并在一起,但它不起作用。简单的PHP模板类

原来这里是类,我从工作

$post = new Template("post.tpl"); 
$post->set("post_title", $post_title); 
$post->set("post_description", $post_description); 
$post->set("content", $post->output()); 
echo $post->output(); 

http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WCsa8CTy2ng

class Template { 
    /** 
    * The filename of the template to load. 
    * 
    * @access protected 
    * @var string 
    */ 
    protected $file; 

    /** 
    * An array of values for replacing each tag on the template (the key for each value is its corresponding tag). 
    * 
    * @access protected 
    * @var array 
    */ 
    protected $values = array(); 

    /** 
    * Creates a new Template object and sets its associated file. 
    * 
    * @param string $file the filename of the template to load 
    */ 
    public function __construct($file) { 
     $this->file = $file; 
    } 

    /** 
    * Sets a value for replacing a specific tag. 
    * 
    * @param string $key the name of the tag to replace 
    * @param string $value the value to replace 
    */ 
    public function set($key, $value) { 
     $this->values[$key] = $value; 
    } 

    /** 
    * Outputs the content of the template, replacing the keys for its respective values. 
    * 
    * @return string 
    */ 
    public function output() { 
     /** 
     * Tries to verify if the file exists. 
     * If it doesn't return with an error message. 
     * Anything else loads the file contents and loops through the array replacing every key for its value. 
     */ 
     if (!file_exists($this->file)) { 
      return "Error loading template file ($this->file).<br />"; 
     } 
     $output = file_get_contents($this->file); 

     foreach ($this->values as $key => $value) { 
      $tagToReplace = "[@$key]"; 
      $output = str_replace($tagToReplace, $value, $output); 
     } 

     return $output; 
    } 

    /** 
    * Merges the content from an array of templates and separates it with $separator. 
    * 
    * @param array $templates an array of Template objects to merge 
    * @param string $separator the string that is used between each Template object 
    * @return string 
    */ 
    static public function merge($templates, $separator = "\n") { 
     /** 
     * Loops through the array concatenating the outputs from each template, separating with $separator. 
     * If a type different from Template is found we provide an error message. 
     */ 
     $output = ""; 

     foreach ($templates as $template) { 
      $content = (get_class($template) !== "Template") 
       ? "Error, incorrect type - expected Template." 
       : $template->output(); 
      $output .= $content . $separator; 
     } 

     return $output; 
    } 
} 

码下面的网站上找到,即使我想循环,如果我添加它的工作代码精细。但后来我尝试合并两个模板文件一起

all_posts.tpl

<div class=”posts”> 
<h1>[@page_title]</</h1> 
[@display_posts] 
</div> 
display_posts.tpl 
<div class=”post”> 

<h2>[@display_title]</h2> 

<p>[@display_description]</p> 

</div> 

所以我想现在要做的就是按下display_posts.tpl到all_posts.tpl并更换标签[@display_posts]

所以在我的PHP我做了以下

$post = new Template("all_posts.tpl "); 
$post->set("page_title", "All Posts"); 
echo $post->output(); 

//created a array from a mysqli loop 

$post_set = array(); 

while ($post_row = mysqli_fetch_array($posts)){ 

    $post_title = $post_row['title']; 
    $post_description = $post_row['description']; 

    $post_set[] = array(
     "display_title" => $post_title, 
     "display_description" => $post_description 
); 

} 



foreach ($post_set as $posts) { 
     $row = new Template("list_users_row.tpl"); 

     foreach ($posts as $key => $value) { 
      $row->set($key, $value); 
     } 
     $postsTemplates[] = $posts; 
    } 

// here i try to merge template 

$postsContents = Template::merge($postsTemplates); 

$layout->set("content", $postsContents->output()); 

echo $layout->output(); 

但这摆脱一个错误集()不是一个功能。有人能帮我解决这个问题吗?我仍然在学习php类。

+1

'$ layout-> set'其中'$ layout'被定义了吗? –

+0

@mistermartin它是在这里'$ layout-> set(“content”,$ postsContents-> output());' – Jordyn

回答

1

$ layout-> set()不起作用的原因是因为$ layout不是模板对象。在你的代码中,你使用$ post = new Template(“all_posts.tpl”);。这使$发布一个Template对象,它可以在你的Template类中使用set()函数。

所以你应该这样做:$ layout = new Template(....)并且你可以调用$ layout-> set()。

在本教程中他们做同样的:

include("template.class.php"); 
$profile = new Template("user_profile.tpl"); 
$profile->set("username", "monk3y"); 
$profile->set("photoURL", "photo.jpg"); 
$profile->set("name", "Monkey man"); 
$profile->set("age", "23"); 
$profile->set("location", "Portugal"); 

$layout = new Template("layout.tpl"); 
$layout->set("title", "User profile"); 
$layout->set("content", $profile->output()); 

echo $layout->output(); 

希望这有助于。

+0

谢谢你阅读你的答案并试图一个小时左右工作。 – Jordyn