Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, February 29, 2016

CodeIgniter create a layout/template library

// end of header.php include('template/header.php');

Heading1

Lorem Ipsum...

include('template/footer.php'); //footer.php //maybe some js and here

So I would like to do something similar and in CI. All pages/views will have the same main styles or scripts, but in some cases, some specific pages (like contact.php) may include, and only in these pages, some specific styles or scripts (like the validation.js).

I have found this video that shows how to create a template/layout library using CI, but I'm not quite sure how I can apply this functionality to work properly.

Answer by Shayan Husaini for CodeIgniter create a layout/template library


I have achieved the same thing with the following:

Put this code in your ./application/core/MY_Controller.php file:

load->view($file, $viewData, TRUE);                  $data['layout'] = $layoutData;                  $this->load->view($this->layout, $data);              } else {                  $this->load->view($this->layout, $viewData);              }                $viewData = array();          }     }  

Create a layout folder inside ./application/views directory and then create your files with the entire html in it. Just put the in that file where you want to put the dynamic content in it. Then in your controllers use $this->render(); and pass your file path and data. If you want to use a different layout for specific page just put the $this->layout = 'path_to_your_layout_file'; it will override the layout file to be used.

Answer by Rakan Nimer for CodeIgniter create a layout/template library


I have struggled with a similar problem not long ago. My solution to it was the following : In your controller constructor create 2 arrays one of css files and one of js files and put in them the files common to all views. And in each function in the controller add to them logic specific files. For your example you'll have something like this :

page_data = array();          $this->page_data['css']=array('main.css','bootstrap.css');          $this->page_data['js']=array('main.js','bootstrap.js');      }      public function about()      {         array_push($this->page_data['css'],'secondary.css');         $this->load->view('main_layout',$this->page_data)      }      public function contact()      {}       }  

And in your view file you'll just iterate over the $css and $js array and include them one by one. You can extend that easily to include header and footer templates by pushing them into the page_data array.

I ended up switching to doing all the templating on the client-side with Backbone and using Code Igniter as a REST API only but this technique gave me relatively clean code for what I needed.

Answer by zion ben yacov for CodeIgniter create a layout/template library


Put the bottom class in libraries/Layout.php (your app not the sys). In the autoload add the library:

$autoload['libraries'] = array('layout');  

In your controller just write $this->layout->render();

The class will render the layout views/layouts/default.php and the view views/$controller.views/$method.php

In the default layout just put

load->view($view,$data); ?>  

and thats it.

The code is

obj =& get_instance();      }        function setLayout($layout)      {          $this->layout = $layout;      }      function setLayoutFolder($layoutFolder)      {          $this->layoutsFodler = $layoutFolder;      }        function render()      {            $controller = $this->obj->router->fetch_class();          $method = $this->obj->router->fetch_method();          $viewFolder = !($this->viewFolder) ? $controller.'.views' : $this->viewFolder . '.views';          $view = !($this->view) ? $method : $this->view;            $loadedData = array();          $loadedData['view'] = $viewFolder.'/'.$view;          $loadedData['data'] = $this->data;            $layoutPath = '/'.$this->layoutsFodler.'/'.$this->layout;          $this->obj->load->view($layoutPath, $loadedData);      }  }  ?>  

Answer by cgwCode for CodeIgniter create a layout/template library


I'm doing layout thing is like bellow.

I get content to data['content'] variable.

This is my controller.

    class Article extends MY_Controller {            function __construct() {              parent::__construct();              $this->load->model('article_model');          }            public function index() {                $data['allArticles']    =   $this->article_model->getAll();                 $data['content']        =   $this->load->view('article', $data, true);              $this->load->view('layout', $data);            }  

This is my Layout.I'm using bootstrap layout.

                                                                                                        Starter Template for Bootstrap                                                                                                                                                                                            

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.