Skip to content
yudayyy edited this page Jul 17, 2012 · 19 revisions

http://ariok.github.com/codeigniter-boilerplate

#How to

##Create a simple page ###Controller Create a new controller which extends BP_Controller and add index function with build_content and render_page calls.

class Example extends BP_Controller {
    
    public function index()
    {
        $this->build_content();
        $this->render_page();
    }
}

Rename the file "example.php" and move it to folder "application/controllers".

###View Create a view file in application/views/pages/ folder and call it "example.php" This is the content of the page, here you can add whatever you prefer... :

<h1>Example Page</h1>
<p>This's the example page</p>

You can check results at http://yourhost/example

Note: By default C-B uses a simple .htaccess to remove "index.php", depending on your server configuration you would change it to make it works properly.

###Menu Add your page to menu view is a simple task :) open file "application/views/template/nav.php" and add this code to current menu (this's a standard implementation with nav -> ul -> li tags but you can change it... this's your template :P

<nav>
    <ul>
        ----SOME OTHER PAGES----
         <li class="<?php echo isActive($page_id,"example")?>"><a href="<?php echo base_url()?>example">Example</a></li>
    </ul>
</nav>

The code you must write in your template to make it works are essentially:

  • isActive($page_id,"example") : this code produces the string "active" when the current page id is equal to "example". This way you can skin your menu having different standard active menu button.

  • href="<?php echo base_url()?>example" creates link for example page

Note: you can disable C-B Menu behavior by adding $this->hasNav = false in index functions and removing this code from the main template:

<?php 
/* codeigniter-boilerplate: main navigation *******************************/
echo $nav
?> 

###Content from controller Since now you have created a simple "static" page, but you could add content from DB or whatever you want simply by calling build_content function with associative array as param :

$toContent["date"] = date("Y-m-d");
$toContent["news"] = $this->db->get("news")->result_array();
$this->build_content($toContent);

build_content($toContent) is simply a shortcut to :

$this->content = load->view("pages/page_view_name",$content,true);

You can otherwise directly add content to $this->content, this way you generate content for your page without using a view.

#Template -> Page hierarchy The default template for C-B is defined by the file application/views/template/main.php This template use HTML5-Boilerplate to define a standard HTML5 page and add some little integration to permit C-B output.

##Meta Title and Description are defined at template level, check the main.php file and search for <title>__YOUR SITE TITLE HERE__</title> and <meta name="description" content="__SITE DESCRIPTION HERE__">. Define this data at template level means that every pages you are going to create inherit these information. You can modify theme for a specific page by adding these rows in your controller code:

$this->title = "Example Page";
$this->description = " Description for example page"; 

##Javascript, CSS and Google Fonts As for meta data, you can add your js/css/gfont includes directly into the main.php file, but if you need specify custom import specifically for a page you can do that by adding some of these rows:

// Define custom CSS
$this->css = array("example.css");

// Import Google Font Lobster and Puritan
$this->GFont = array("Lobster","Puritan"); 

// Custom JS
$this->javascript("example.js","someother.js");

Note: You must put js file in /js folder and styles in /css one.

Note: JS, Style and GFonts are not going to replace what you define in main.php.

Note: Google Font include system uses the Web Fonts API parameter name

Clone this wiki locally