菜鸟试图创建PHP模板引擎

时间:2017-08-01 09:47:01

标签: php templates



<?php

//The below is for testing only - no longer required
$contentExample = array(
	array(
		"name" => "First Product",
		"price" => "€24.99",
		"description" => "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Second Product",
		"price" => "€64.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Third Product",
		"price" => "€74.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Fourth Product",
		"price" => "$19.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Fifth Product",
		"price" => "€12.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Sixth Product",
		"price" => "€84.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	)
);

class Template{
	private $template;
	private $content = array();
	private $hasNext;
	private $noOfResults;

	public function __construct($template,$content){
		//Should validate arguments before we continue
		$this->load($template,$content);
	}


	public function __GET($val){
		if($val=="hasNext"){
			return $this->$val;
		}
		else if($val !== "noOfResults"){
			return $this->$val;
		}
		else{
			die("Cannot access private property Template::$val");
		}
	}
	public function output(){
		if($this->hasNext){
			$record = current($this->content);
			$html = $this->template;

			//test case: $key = "name", $val = "First Product"

			foreach($record as $key=>$val){
				$html = str_replace('{'.$key.'}',$val,$html);
			}
			if(!next($this->content)){
				$this->hasNext = FALSE;
			}
			return $html;
		}
		else{
			return "";
		}
	}

	public function load($template,$content){
		$this->template = file_get_contents("templates/$template",true);
		//$this->template = $template;
		$this->content = $content;
		$this->hasNext = TRUE;
		$this->noOfResults = sizeof($this->content);
		if($this->noOfResults == 0){
			$this->hasNext = FALSE;
		}
	}
}

/*$test = new Template("product_thumbnail.html",$contentExample);

while($test->hasNext){
echo $test->output();
}*/
&#13;
&#13;
&#13;

我尝试构建而不是使用预先存在的临时引擎的原因是我可以看到它是如何工作的。什么是使用代码的重点我不明白??

我最终收到此错误

注意:未定义的变量:第124行的C:\ xampp \ htdocs \ Shawpify \ Shawpify \ ShawpifyV5.0 \ index.php中的内容 无法访问私人财产Template :: noOfRresults

造成这种情况的代码是:

public function __GET($val){
    if($val=="hasNext"){
        return $this->$val;
    }
    else if($val =="noOfResults"){
        return $this->$val;
    }
    else{
        die("Cannot access private property Template::$val");
    }
}

我检查过拼写错误,所有文件都退出,检查sytax错误但是训练

Thanx提前获得任何建议或解决方案

2 个答案:

答案 0 :(得分:0)

您的错误消息正在尝试访问该属性,因为它位于双引号(install.packages('png'))之间。 将错误消息放在单引号中,这样就不会解析它。

"

答案 1 :(得分:0)

未来参考

我发现错误并修复了错误,即语法错误和命名错误在这里是正确的答案

<?php

//The below is for testing only - no longer required
$contentExample = array(
	array(
		"name" => "First Product",
		"price" => "€24.99",
		"description" => "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Second Product",
		"price" => "€64.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Third Product",
		"price" => "€74.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Fourth Product",
		"price" => "$19.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Fifth Product",
		"price" => "€12.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	),
	array(
		"name" => "Sixth Product",
		"price" => "€84.99",
		"description" => "This is another description. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
	)
);

class Template{
	private $template;
	private $content;
	private $hasNext;
	private $noOfResults;

	public function __construct($template,$content){
		//Should validate arguments before we continue
		$this->load($template,$content);
	}

	public function __get($val){
		if($val=="hasNext"){
			return $this->$val;
		}
		else if($val=="noOfResults"){
			return $this->$val;
		}
		else{
			die("Cannot access private property Template::$val");
		}
	}
	public function output(){
		$record= current($this->content);
        $html =$this->template;

        foreach($record as $key=>$val){
            $html = str_replace("{".$key."}",$val, $html);
        }
        if(!next($this->content)){
            $this->hasNext=FALSE;
        }
        return $html;
	}

	public function load($template,$content){
		$this->template = file_get_contents("http://localhost/Shawpify/ShawpifyV5.0/templates/$template",true);
		//$this->template = $template;
		$this->content = $content;
		$this->hasNext = TRUE;
		$this->noOfResults = sizeof($this->content);
		
	}
}
/*$test = new Template("product_thumbnail.html",$contentExample);

while($test->hasNext){
    echo $test->output();
}*/