I\'m fairly new to MVC, and I\'ve found CodeIgniter recently. I\'m still learning everyday, but one problem is its template engine. What is the best way to create templates
Well codeignier does not have such library by default. But if you want different themes, views, and assets to be managed try using this:
https://github.com/mahadazad/php-layout-manager
A Codeigniter template is generally just a PHP file. You can use all the usual PHP syntax to output variables, do loops, and call other PHP code.
Sample controller:
<?php
class Blog extends Controller {
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
Sample view:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
Read more in the docs here: CodeIgniter User Guide: Views
I have two primary templates; one for the site and one for our admin panel. Here is my setup for our main site (mostly static)... I decided on one controller called site
It calls the template file and each page and gets its view file.
Why doesn't anyone mention template engine use? Are -just- views better/faster?
In config/template.php
I defined the template(s). Note *site_template* is in the views
folder:
$template['site']['template'] = 'site_template';
$template['site']['regions'] = array('title','section','col2','content',);
$template['site']['parser'] = 'parser';
$template['site']['parser_method'] = 'parse';
$template['site']['parse_template'] = FALSE;
In config/routers.php
I setup rules to handle the requests for the
site
controller which are single segments urls mostly but we do
have one section that is structured as such; /who-we-are and then
for selected people /who-we-are/robert-wayne and so:
$route['what-we-do'] = 'site/what_we_do';
$route['who-we-are'] = 'site/who_we_are';
$route['who-we-are/(:any)'] = "site/who_we_are/$1"
And controllers/site.php
Again with a function for each page/section:
class Site extends CI_Controller
{
function __construct() {
parent::__construct();
$this->template->set_template('site'); // ask for the site template
$this->load->library('mobile');
}
public function index()
{
$data = array('section' => 'home');
$this->template->write_view('col2', 'site/menu.php', $data);
$this->template->write('title', "COOL PAGE TITLE", TRUE);
$this->template->write('section', $data['section'], TRUE);
$this->template->write_view('content', 'site/welcome', $data);
$this->template->render();
}
public function who_we_are()
{
// this bit readies the second segment.
$slug = str_replace('-', '_', $this->uri->segment(2, 0));
if($slug) // IF there is a second segment we load the person.
{
$data['bio'] = $this->load->view('site/people/'.$slug, '', true)
} else {
// where it loads the general view who_we_are
}
// and so on for each page...
and as fine point notice the router lets us leave out `/site/' in the url, http://the site.com/who-we-are
thoughts? anyone? bueller?
There is a library which allows you to use templates in CodeIgniter in a native style. To load a template/theme just do:
$this->load->theme(‘theme_name’);
To load CSS and javascript files from your views you can do:
$this->load->css(‘path/file.css’);
$this->load->js(‘path/file.js’);
You can optionally control the way browsers cache CSS & JS files.
I use CodeIgniter with Smarty and it's great (if you like Smarty, I do).
Say you have an article controller, you could do somehting like this in it:
class Article extends Controller {
function show_all() {
$articles = $this->article_model->get_all();
$this->smarty->assign('entities', $articles);
$this->smarty->view('list');
}
}
And then in your template:
{include file="header.tpl"}
<ul>
{foreach from=$entities item=entity}
<li>{$entity.title}</li>
{/foreach}
</ul>
{include file="footer.tpl"}
The nice part about this is that the controller doesn't really need to know about headers and footers. It just knows that a group of articles should be shown as a list. From there, it's just the templates that are responsible for defining how a list of things are displayed, in this case, in a ul between a header and footer.
Another cool thing you can do is use this list template for things that aren't articles. You could have a list of users or pages or whatever. In some cases reusing a template like this can be useful. Not always, but sometimes.
Setting up CodeIgniter for smarty is pretty straightforward. It's a matter of copying the Smarty files to your library folder and creating a simple wrapper for it. You can find instructions here:
http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html
Once you get is set up it's great.
Unlike other frameworks CodeIgniter does not have a global template system. Each Controller controls it's own output independent of the system and views are FIFO unless otherwise specified.
For instance if we have a global header:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title><?=$title?></title>
<!-- Javascript -->
<?=$javascript ?>
<!-- Stylesheets -->
<?=$css ?>
</head>
<body>
<div id="header">
<!-- Logos, menus, etc... -->
</div>
<div id="content">
and a global footer:
</div>
<div id="footer">
<!-- Copyright, sitemap, links, etc... -->
</div>
</body>
</html>
then our controller would have to look like
<?php
class Welcome extends Controller {
function index() {
$data['title'] = 'My title';
// Javascript, CSS, etc...
$this->load->view('header', $data);
$data = array();
// Content view data
$this->load->view('my_content_view', $data);
$data = array();
// Copyright, sitemap, links, etc...
$this->load->view('footer', $data);
}
}
There are other combinations, but better solutions exist through user libraries like:
See Comments Below