Book Image

CodeIgniter 1.7 Professional Development

By : Adam Griffiths
Book Image

CodeIgniter 1.7 Professional Development

By: Adam Griffiths

Overview of this book

<p>CodeIgniter is an open source PHP framework with a small footprint and exceptional performance. It gives you a rich set of libraries for common tasks, with a simple interface to access them. There are several unexplored aspects of CodeIgniter that can help developers build applications more easily and quickly. In this book, you will learn the intricacies of the framework and explore some of its hidden gems.<br /><br />If you want to get the most out of CodeIgniter, this book is for you. It teaches you what you need to know to use CodeIgniter on a daily basis. You will create mini-applications that teach a specific technique and let you build on top of the base. <br /><br />This book will take you through developing applications with CodeIgniter. You will learn how to make your CodeIgniter application more secure than a default installation, how to build large-scale applications and web services, how to release code to the community, and much more. You will be able to authenticate users, validate forms, and also build libraries to complete different tasks and functions.<br /><br />The book starts off introducing the framework and how to install it on your web server or a local machine. You are introduced to the Model-View-Controller design pattern and how it will affect your development. Some important parts of the CodeIgniter Style Guide are included to keep CodeIgniter development as standardized as possible; this helps greatly when working as part of a team or taking on an old CodeIgniter project. You will quickly move on to how CodeIgniter URLs work and learn about CodeIgniter-specific files such as helpers and plugins. By the time you finish this book, you will be able to create a CodeIgniter application of any size with confidence, ease, and speed.</p>
Table of Contents (16 chapters)
CodeIgniter 1.7 Professional Development
Credits
About the Author
About the Reviewers
Preface
Index

Views: Your template files


Views are your template files. They are what is output to the browser, and should be mostly HTML code, with some PHP code. Put simply, views are HTML files of your template; they can also be page segments, such as a header or footer. View files can also call other views, if you need such flexibility.

Views are located in the system/application/views/ folder, and can be stored in subfolders inside this main views folder.

Loading a view

Loading a view is done in the same way as loading a model; it takes just one line of code.

$this->load->view('view_name');

Here, view_name would be the name of the view file that you wish to load. You can also pass an array of data to a view file by passing an array as the second parameter of the load->view function.

$data['title'] = "My Web Page";
$this->load->view('view_name', $data);

You would then be able to use the variable $title in your view file to echo out the page title.

Loading multiple views

You load multiple views in the same way as you load just one view. CodeIgniter intelligently handles multiple calls to $this->load->view, and will append these calls together. Here is an example:

function index()
{
  $data['title'] = "My Web Page";
  $this->load->view('header', $data);
  $this->load->view('navigation');
  $this->load->view('content');
  $this->load->view('footer');
}

Adding dynamic data

Adding data to a view file is a very simple process, which we have touched upon already. You pass data to a view file by using the second parameter of the load->view function. All view data must be passed as an array or an object. This is because CI runs this array or object through PHP's extract function, which simply takes an object or array key and creates a variable for it with the same name. Let's look at an example:

$data = array(
      'title' => "My Web Page",
      'heading' => "Welcome to my web page",
      'content' => "This is my first web page using Codeigniter!"
      );
$this->load->view('main', $data);

You will then be able to use this data in your view, as illustrated below:

<html>
<head>
  <title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
<?php echo $content; ?>
</body>
</html>

Creating loops

Creating loops in view files has been a stumbling block for a few developers. By passing a multidimensional array to a view file, you can easily establish a loop in any of your view files. Let's take a look at an example.

<?php
  class Todo extends Controller
  {
    function index()
    {
      $data['todo_list'] = array("buy food", "clean up", "mow lawn");
      $this->load->view('todo', $data);
    }
  }
?>

This is a very simple Controller. Your view file for this would be as follows:

<html>
<head>
  <title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
<?php echo $content;?>
<h2>My Todo List</h2>
<?php
foreach($todo_list as $item)
{
  echo $item;
}
?>
</body>
</html>

Returning views as data

You are also able to return view files as data; this can be useful if you wish to process this data in some way. Simply set the third parameter to boolean TRUE—and it will return the view data.

$this->load->view('welcome', NULL, TRUE);

CodeIgniter uses an output buffer to take all calls to the load view function, and processes them all at once, sending the whole page to the browser at the same time. So when you return views as data, you will be able to save the contents of the view inside a variable for whatever use you need.