Welcome Guest, Not a member yet? Register   Sign In
Object Programming with CodeIgniter
#3

(04-28-2015, 12:57 AM)Vimal Wrote: Greate tell me more.. And what is the library you used in $this->layout ???? I am searching template library

Concerning the Layout it's a library which is auto-loaded and working with an asset helper (also auto-loaded) and layout views.

Layout Class (in application/libraries folder)

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

    class 
Layout {
        private 
$CI;
        private 
$data = array();
        private 
$layout "default";

        public function 
__construct() {
            
$this->CI =& get_instance();
            
$this->data["output"] = "";
            
$this->data["title"] = "Your title here";
            
$this->data["charset"] = $this->CI->config->item("charset");
            
$this->data["css"] = array("font-awesome.css""foundation.css""toast.css");
            
$this->data["js"] = array("modernizr.js""jquery-1.11.2.js""foundation.min.js""toast.js""launchFoundation.js");
        }

        public function 
view($name$data = array()) {
            
$this->data["output"] .= $this->CI->load->view($name$datatrue);
            
$this->CI->load->view("layouts/".$this->layout$this->data);
        }

        public function 
setTitle($title) {
            if(!empty(
$title) && is_string($title)) {
                
$this->data["title"] = $title." - OGMA";

                return 
true;
            }

            return 
false;
        }

        public function 
setLayout($layout) {
            if(!empty(
$layout) && file_exists(APPPATH.'views/layouts/'.$layout.'.php')) {
                
$this->layout $layout;

                return 
true;
            }

            return 
false;
        }

        public function 
setMenu($menu) {
            if(!empty(
$menu) && is_string($menu)) {
                
$this->data["menu"] = $menu;

                return 
true;
            }

            return 
false;
        }

        public function 
addCSS($name) {
            if(!empty(
$name) && file_exists(css_path().$name.".css")) {
                
$this->data["css"][] = $name.".css";

                return 
true;
            }

            return 
false;
        }

        public function 
addJS($name) {
            if(!empty(
$name) && is_string($name) && file_exists(js_path().$name.".js")) {
                
$this->data["js"][] = $name.".js";

                return 
true;
            }

            return 
false;
        }
    } 

Asset Helper

Default Layout View (in application/views/layouts folder, you have to create the layouts one)

PHP Code:
<!doctype html>
<
html class="no-js" lang="en">
    <
head>
        <
meta charset="<?= $charset; ?>" />
        <
meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <
title><?= $title?></title>
        <?php
            
foreach($css as $stylesheet) {
                echo 
css($stylesheet);
            }
        
?>
    </head>
    <body>
        <nav>
            <menu>
                <?= img("logo-small.png", array("alt" => "Logo""class" => "logo")); ?>
                <span class="brand">Brand <small>v1</small></span>
                <?= $menu?>
            </menu>
        </nav>
        <main>
            <?= $output ?>
        </main>
        <?php
            
foreach($js as $script) {
                echo 
js($script);
            }
        
?>
    </body>
</html> 

Then you are free to add for example a menuMaker like I did (it simply use a config array and build all links with the makeMenu() function) or whatever you think would be useful !

I took the Layout Class from the internet but can't manage to find it again so sorry for no credits...
Reply


Messages In This Thread
Object Programming with CodeIgniter - by _this - 04-27-2015, 11:48 PM
RE: Object Programming with CodeIgniter - by _this - 04-28-2015, 05:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB