когато имаме един html template как да изкараме само дадени неща в него, примерно имаме някакво menu и човека се логва, и не трябва да му изкарва линка за log in. как се прави това?или пък попълва някаква форма и прави грешка в някое поле, искаме това поле, когато се върне да го оправи да е с червен цвят.как става?
(сами си пишем template engine-a, а не някой от готовите)
eто едно примерче
template.html
template.php (template engine)
index.php (това показва формата на екрана)
сега искам примерно, ако човека въведе число бекграунда на формата да стане червен (примерно).обаче в template файла да няма php.
мерси
(сами си пишем template engine-a, а не някой от готовите)
eто едно примерче
template.html
Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="#" method="post">
<input type="text" name="text" value="{value}" />
<input type="submit" value="submit" />
</form>
</body>
</html>
Код:
<?
class Page
{
var $page;
function Page($template = "template.html") {
if (file_exists($template))
$this->page = join("", file($template));
else
die("Template file $template not found.");
}
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? join("", file($data)) : $data;
$this->page = eregi_replace("{" . $tag . "}", $data,
$this->page);
}
else
die("No tags designated for replacement.");
}
function output() {
echo $this->page;
}
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}//end class
?>
Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once("template.php");
@$text=$_POST['text'];
$page = new Page("template.html");
$page->replace_tags(array(
"value"=>$text
));
$page->output();
?>
</body>
</html>
мерси