forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
mpage
Derek Jones edited this page Jul 5, 2012
·
7 revisions
You know, django is a good python framework.And in django, you can master page. For example:
base.html (Our layout page)
<html>
<head><title>{% block title %}{%endblock%}</title></head>
<body>
text text text
lorem ipsom dolar is amet bla bla bla
{% block content %}{% endblock%}
Footer code bla bla
</html>
a page simple.html We can include layout page like this
{% extends 'base.html'%}
# after
{%block title%}Page Title{%endblock%}
{%block content%}This is the content{%endblock%}
OK.We use that in page with mPage library:
You can add this to your construct function or a function :D of course into your controller
$this->load->library("mpage");
// Defining base page
$this->mpage->base("base"); # Will be: views/base.php
// a function
function alpcan() {
$data['variable'] = "i love CI";
$data['second'] = "php is the best";
// not $this->load->view("simple", $data);
$this->mpage->render("simple", $data);
}
And last base.php and simple.php in views directory base.php:
<html>
<head>
<title>
<block=title></block>
</title>
</head>
<body>
text text text
lorem ipsom dolar is amet bla bla bla
<block=content></block>
</body>
</html>
simple.php
<block=title>Page Title</block>
<block=content>This is the content</block>
If you don't know master page you can look at to google :P Finally sorry for my bad english Alpcan AYDIN - alpcanaydin1992@gmail.com
And mPage.php:
<?php
/**
* @author: Alpcan AYDIN <alpcanaydin1992@gmail.com>
* @copyright: 2008 Batikon Internet Technologies(c)
* @version: v0.1
*
* For your suggestions send a mail to alpcanaydin1992@gmail.com
*/
// We create a class which name is mPage
class mPage {
/**
* Definition: Base Page
*
* @var string
*/
private $base = "";
/**
* Definition: Sub Page
*
* @var string
*/
private $subPage;
/**
* Definition: Error Text
*
* @var string
*/
private $errorText;
/**
* Definition: CodeIgniter
*
*/
private $CI;
/**
* Definition: Construct Function
*
* @param none
*/
public function __construct() {
// We include CodeIgniter Libraries
$this->CI = get_instance();
}
/**
* Definition: Error Function
*
* @param errorText
*/
protected function error($errorText) {
if(!empty($errorText)) {
$this->errorText = $errorText;
$styleText = "padding:3px; margin:3px; border:2px solid #C00606; background:#FCFBA7;";
$styleText.= "color:#cc0000; font-family:verdana; font-size:11px;";
echo "<div style='$styleText'><b>mPage Error:</b> $this->errorText</div>";
}
}
/**
* Definition: Set Base Page
*
* @param base
*/
public function base($base) {
// Control: is file exist?
if(file_exists(APPPATH.'views/'.$base.EXT)) {
// Defined base path
$this->base = $base;
}else {
// Give error
self::error("Can't found <b>$base".EXT."</b> in views directory");
}
}
/**
* Definition: Render
*
* @param subPage, tags, content
*/
public function render($subPage, $content = array()) {
// Base Page
$basePage = $this->CI->load->view($this->base, $content, TRUE);
// if sub page exist
if(file_exists(APPPATH.'views/'.$subPage.EXT)) {
$subPageSOURCE = $this->CI->load->view($subPage, $content, TRUE);
preg_match_all('/\<block\=(.*?)\>(.*?)\<\/block\>/s', $subPageSOURCE,$subTag);
for($i=0; $i<=count($subTag[1]); $i++) {
$tags = $subTag[1][$i];
$tagContent = $subTag[2][$i];
$basePage = str_replace('<block='.strtolower($tags).'></block>', $tagContent, $basePage);
}
preg_match_all('/\<block\=(.*?)\>(.*?)\<\/block\>/s', $basePage,$exTags);
for($i=0; $i<=count($exTags[1]); $i++) {
$exTag = $exTags[1][$i];
$basePage = str_replace('<block='.strtolower($exTag).'></block>', "", $basePage);
}
}else {
self::error("Can't found <b>$subPage".EXT."</b> in views directory");
}
echo $basePage;
}
}
?>
IMPORTANT: If you take a session error, change text encoding(UTF-8 to ANSI) mpage.php