Welcome Guest, Not a member yet? Register   Sign In
mPage - Master Page Library like Django
#1

[eluser]alpcan[/eluser]
You know, django is a good python framework.And in django, you can master page.
For example:

base.html (Our layout page)
Code:
<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
Code:
{% 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 Big Grin
of course into your controller
Code:
$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:
Code:
<html>
<head>
<title>
<block=title></block>
&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
text text text
lorem ipsom dolar is amet bla bla bla
<block=content></block>
&lt;/body&gt;
&lt;/html&gt;

simple.php
Code:
<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 Tongue
Finally sorry for my bad english
Alpcan AYDIN - [email protected]

And mPage.php:
Code:
&lt;?php
/**
* @author: Alpcan AYDIN <[email protected]>
* @copyright: 2008 Batikon Internet Technologies(c)
* @version: v0.1
*
* For your suggestions send a mail to [email protected]
*/

// 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;
       }
}

?&gt;
#2

[eluser]xwero[/eluser]
someone beat you to it. It's more php oriented in the template.
#3

[eluser]alpcan[/eluser]
[quote author="xwero" date="1218497185"]someone beat you to it. It's more php oriented in the template.[/quote]

Hmm, i think mine, more useful and shorter.
#4

[eluser]xwero[/eluser]
How is it more useful?
#5

[eluser]alpcan[/eluser]
[quote author="xwero" date="1218552964"]How is it more useful?[/quote]

OK, maybe not more useful but absolutely shorter.
Only you write

Code:
<block=name>
Text text text
</block>

in Template_Inheritance_Helper
you should write:
Code:
&lt;? startblock('name') ?&gt;
    &lt;?= get_extended_block() ?&gt;
    Text Text Text
&lt;? endblock() ?&gt;

in fact, two of them are useful
in fact, two of them for CodeIgniter




Theme © iAndrew 2016 - Forum software by © MyBB