Home arrow PHP arrow Page 3 - Returning Strings from Views with Code Igniter

Generating partial sections of a web page - PHP

When it comes to generating both static and dynamic web pages, CodeIgniter gives PHP developers the liberty of working with different approaches that can be easily adapted to suit the requirements of a huge variety of web applications. Building web pages using CI demands that you always deal with one or multiple views, generally represented in the form of HTML files that contain embedded PHP variables. Therefore, if you’re a CI user who want to learn to manipulate views in some clever ways, keep reading. In this series of articles, you’ll find a comprehensive guide to several methods you can implement within your own CI-based applications to handle view files in a truly efficient manner.

TABLE OF CONTENTS:
  1. Returning Strings from Views with Code Igniter
  2. Review: using nested views along with the load-vars() method
  3. Generating partial sections of a web page
  4. Putting all of the views together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
April 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the previous segment, it’s possible to pass a third Boolean argument to the $this->load->view()” method to return its output as a string after it has been parsed. By using this approach, it is also very simple to build sections of a web page independently, in a way similar to the method shown in the preceding article.

To demonstrate how this approach can be successfully implemented, first I’m going to create some new view files, which are listed below:


(definition of ‘main_page.php’ file - located at /application/views/ folder)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Sample Web Page</title>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #999;

}

#container{

width: 600px;

margin: 0 auto;

}

#header{

padding: 10px;

background: #eee;

}

#content{

padding: 10px;

background: #9cf;

}

#footer{

padding: 10px;

background: #eee;

}

h1{

font: bold 2em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;

color: #039;

}

h2{

font: bold 1.5em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;

}

p{

font: normal .8em Arial, Helvetica, sans-serif;

margin: 0 0 18px 0;

}

</style>

</head>

<body>

<div id="container">

<?php

echo $header.$content.$footer;

?>

</div>

</body>

</html>



(definition of ‘header_view.php’ file - located at /application/views/ folder)

<div id="header">

<h1><?php echo $header;?></h1>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p>

</div>



(definition of ‘content_view.php’ file - located at /application/views/ folder)


<div id="content">

<?php if($users->num_rows > 0):?>

<?php foreach($users->result() as $user):?>

<p><strong>First Name: </strong><?php echo $user->firstname;?></p>

<p><strong>Last Name: </strong><?php echo $user->lastname;?></p>

<p><strong>Email: </strong><?php echo $user->email;?></p>

<hr />

<?php endforeach;?>

<?php endif;?>

</div>



(definition of ‘footer_view.php’ file - located at /application/views/ folder)


<div id="footer">

<h2><?php echo $footer;?></h2>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p>

</div>


Hopefully, at this point the group of view files coded above should be fairly familiar to you, right? Naturally, these are responsible for generating different sections of a web page, plus there’s one that acts like a general layout, called “main_page.php,” which loads the values returned from the other views.

With this handy view structure at our disposal, now it’s time to see how it can be used in conjunction with the strings returned by the “$this->load->view()” method to build an entire web document.

This task will be performed by a controller, and its definition will be shown in the next few lines. So, jump ahead and read the following section, please.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: