Skip to content

Instantly share code, notes, and snippets.

@ralphsaunders
Created August 29, 2011 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphsaunders/1178019 to your computer and use it in GitHub Desktop.
Save ralphsaunders/1178019 to your computer and use it in GitHub Desktop.
Loading a view with templates in Codeigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/site
* - or -
* http://example.com/index.php/site/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/site/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$todo_list = array( 'milk', 'bread', 'eggs', 'sugar' );
$data['title'] = 'Page Title';
$data['main_content'] = 'welcome'; // "welcome" is the filename of your view
$data['todo'] = $todo_list;
$this->load->view('template', $data);
}
}
/* End of file site.php */
/* Location: ./application/controllers/site.php */
<!doctype html>
<html lang="en">
<head>
<title><?php if (isset($title)) { echo $title; } else { echo "Untitled"; } ?></title>
<!-- Character Encoding -->
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Favicon -->
<link rel="shortcut icon" href="<?php echo base_url(); ?>resources/imgs/favicon.ico" />
<!-- Stylesheets -->
<link rel="stylesheet" href="<?php echo base_url(); ?>resources/css/style.css" type="text/css" media="screen">
<!-- jQuery; other scripts are located at the bottom of the page -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var baseUrl = "<?php echo base_url(); ?>/";
var siteUrl = "<?php echo site_url(); ?>/";
</script>
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Humans -->
<link rel="author" href="<?php echo base_url(); ?>humans.txt" />
</head>
<body id="index">
<?php $this->load->view('header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('footer'); ?>
<h1>Ohai</h1>
<ul>
<?php foreach( $todo_list as $todo ) : ?>
<li><?php echo $todo; ?></li>
<?php endforeach; ?>
</ul>
<?php echo date(); ?>
@AysadKozanoglu
Copy link

thank you, works great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment