วันอาทิตย์ที่ 19 ธันวาคม พ.ศ. 2553

layout-views or master-contents page

master-page.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>{$title}</title>
{$head}
</head>

<body>
<h1>Welcome to my site</h1>
<table width="800" border="2"><tr><td bgcolor="#FFCC00">
{$content1}
</td><td bgcolor="#FFFFCC">
{$content2}
</td></tr>
</table>
<hr/>
easy master page by num |
<a href="normal-page.php">page1</a> |
<a href="normal-page2.php">page2</a>
</body>
</html>


normal-page.php

<?php require 'util.php'; $MasterPage = 'master-page.php';?>
<?php if (0):?><meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<link type="text/css" rel="stylesheet" href="style.css"/><?php endif;?>

<?php sb('title'); echo 'test-title'; eb();?>

<?php sb('head');?>
<script type="text/javascript">
window.onload = function(){
document.getElementById('test').innerHTML = 'javascript text';
}
</script>
<?php eb();?>

<?php sb('content1');?>
<b>hello world</b>
<div id="test"></div>
<?php eb();?>

<?php sb('content2');?>
lksldfjksjdklfjk lsjdlkfj s dfjsldkjfks jdklfjkljsdl fklsjdklfjlksd
<?php eb();?>


<?php render();?>


normal-page2.php

<?php require 'util.php'; $MasterPage = 'master-page.php';?>
<?php if (0):?><meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<link type="text/css" rel="stylesheet" href="style.css"/><?php endif;?>

<?php sb('title'); echo 'สวัสดีครับ'; eb();?>

<?php sb('head');?>
<?php eb();?>


<?php sb('content1');?>
page2
<?php eb();?>

<?php sb('content2');?>
this is page 2.
<?php eb();?>



<?php render();?>


util.php

<?php

$_vars = array();
function sb($name){
static $sName;
if (empty($name)) return $sName;
$sName = $name;
ob_start();
}
function eb(){
global $_vars;
$name = sb(0);
$_vars[$name] = ob_get_clean();
}
function render(){
global $_vars;
ob_start();
require ($GLOBALS['MasterPage']);
$page = ob_get_clean();
foreach($_vars as $name=>$var)
$page = str_replace('{$'.$name.'}',$var,$page);
echo $page;
}

?>



/* CSS Document */

*{margin:0;padding:0;}
body {background-color:#ccc;}

1 ความคิดเห็น: