با سلام خدمت تمامی دوستان ، من یک موتور قالب درست کردم و میخوام که اخبار سایتم رو با اون نمایش بدم ولی تو کد نویسی کمی مشکل دارم .
این فایل template.class.php هستش :
<?php class Template { protected $file; protected $values = array(); public function __construct($file) { $this->file = $file; } public function set($key, $value) { $this->values[$key] = $value; } public function output() { if (!file_exists($this->file)) { retu "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); }
retu $output; } static public function merge($templates, $separator = "n") { $output = ""; foreach ($templates as $template) { $content = (get_class($template) !== "Template") ? "Error, incorrect type - expected Template." : $template->output(); $output .= $content . $separator; } retu $output; } }
?>
کد HTML:
<article class="post"> <div class="wrapper"><header><span>[@title]</span><ul class="post-info"><li class="date">[@pdate]</li><li class="category">[@category]</li></ul></header><div class="pbod">[@content]</div><footer><a href="more.php?id=[@id]" title="[@title]" class="more">ادامه مطلب</a></footer></div></article>
<?php
include("template.class.php"); /** * Defines an array for the posts. * Each value of the array is an array itself with each user's data. * Typically this would be loaded from a database. */ include('../config.php'); $postQuery = mysql_query("SELECT * FROM posts ORDER BY id"); $postArrays = mysql_fetch_array($postQuery,MYSQL_ASSOC); $postArray = array( array("id" => "1", "title" => "Title", "content" => "Content", "pdate" => "2016", "category" => "category") ); /** * Loop through the posts and creates a template for each one. * Because each user is an array with key/value pairs defined, * we made our template so each key matches a tag in the template, * allowing us to directly replace the values in the array. * We save each template in the $postsTemplates array. */ foreach($postArray as $post) {// posts = postarray , user = post $row = new Template("post.tpl"); foreach ($post as $key => $value) { $row->set($key, $value); } $postsTemplates[] = $row; //postsTemplate = postsTemplatE } /** * Merges all our posts' templates into a single variable. * This will allow us to use it in the main template. */ $postsContents = Template::merge($postsTemplates); /** * Defines the main template and sets the posts' content. */ $postsList = new Template("list_posts.tpl"); $postsList->set("posts", $postsContents); echo $postsList->output(); ?>
$users = array( array("useame" => "monk3y", "location" => "Portugal") , array("useame" => "Sailor", "location" => "Moon") , array("useame" => "Treix!", "location" => "Caribbean Islands") );
Array( [id] => 4 [title] => title [content] => index [morecontent] => more [category] => 1,2,3 [author] => امیر محمد [pdate] => 2016-06-21 [tags] => title,index,more,new,first,news,cms)
برچسب:
نویسنده: محمد رضا جوادیان