Codeigniter: Best way to structure partial views
I can't vouch that this is the best approach, but I create a base controller like this:
class MY_Controller extends CI_Controller {
public $title = '';
// The template will use this to include default.css by default
public $styles = array('default');
function _output($content)
{
// Load the base template with output content available as $content
$data['content'] = &$content;
$this->load->view('base', $data);
}
}
The view called 'base' is a template (a view that includes other views):
<?php echo doctype(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php $this->load->view('meta'); ?>
</head>
<body>
<div id="wrapper">
<?php $this->load->view('header'); ?>
<div id="content">
<?php echo $content; ?>
</div>
<?php $this->load->view('footer'); ?>
</div>
</body>
</html>
What this achieves is that every controller wraps its output in the base template, and that views have valid HTML instead of opening tags in one view and closing in another. If I'd like a specific controller to use a different or no template, I could just override the magic _output()
method.
An actual controller would look like this:
class Home extends MY_Controller {
// Override the title
public $title = 'Home';
function __construct()
{
// Append a stylesheet (home.css) to the defaults
$this->styles[] = 'home';
}
function index()
{
// The output of this view will be wrapped in the base template
$this->load->view('home');
}
}
Then I could use its properties in my views like this (this is the 'meta' view that populates the <head>
element):
echo "<title>{$this->title}</title>";
foreach ($this->styles as $url)
echo link_tag("styles/$url.css");
I like my approach because it respects the DRY principle and the header, footer and other elements get included just once in the code.
Make simple in-depth partial views with Code Igniter
Finally, I made my own library to handle what I needed:
https://github.com/ldiqual/codeigniter-pview
Thanks for your answers, anyway :)
Dynamic views with Codeigniter or templates or partial views
why don't you use AngularJS ? you can make a single page application using ui-routes or ng-routes. you can make ajax call to your controller method that will render your view, and you can show that view in .
Best method of including views within views in CodeIgniter
Views within other views are called Nested views.
There are two ways of including nested views in CodeIgniter:
1. Load a nested view inside the controller
Load the view in advance and pass to the other view. First put this in the controller:
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
?>
Then put <?=$menu?>
in your view at the point you want the menu to appear.
2. Load a view "from within" a view
First put this in the controller:
<?php
$this->load->view('home');
?>
Then put this in the /application/views/home.php
view:
<?php $this->view('menu'); ?>
<p>Other home content...</p>
About best method, I prefer the 1st method over 2nd one, because by using 1st method I don't have to mix up code, it is not like include php. Although indirectly both are same, the 1st method is clearer & cleaner than 2nd one!
Codeigniter best way to handle controller calling views
The best way I find is create a default view.
views > default_view.php
views > includes > header_view.php
views > includes > footer_view.php
views > information > contact_view.php
On that view
<?php
$this->load->view('includes/header_view');
$this->load->view($content_page);
$this->load->view('includes/footer_view');
?>
Then on the controller to load view this way you do not have to load the header and footer views all the time.
Following the Codeigniter StyleGuide
Filename: Example.php
<?php
class Example extends CI_Controller {
public function index() {
// Add any other variables
$data['content_page'] = 'information/contact_view'; // This will be your content page example
$this->load->view('default_view', $data);
}
}
Best structure Codeigniter, constant top menu
One of the ideas is -
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
//load your model
$inbox_new_messages = $this->Call_the_model->get_new_messages();
//save in session
$this->session->set_userdata('sess_inbox_new_messages', $inbox_new_messages);
}
}
then just extend your controllers with new MY_Controllers and
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
}
in your view files - check if that session variable is set and display the number.
Hope that helps.
What are Partial Views?
A partial view is just a sub-view that you can include in a parent view. Let's take a look at a common example:
// Controller:
$data['myvar'] = array('element1', 'element2', 'element3');
$this->load->view('myview', $data);
// Myview:
<ul>
foreach ($myvar as $var) {
$this->load->view('partialview', array('var', $var));
}
</ul>
// Partialview:
<li><?= $var ?></li>
This is useful to repeat content according to a list.
Note that nothing differs between a view and a partialview, it's just the way you include it that defines the term.
CodeIgniter MVC - Is it a good practice to have Models serve views?
Better you create a custom library and put getNoticeboardHTML()
in that class. After that call this wherever you need. Do not dirty your model with HTML.
Create Custom Library
what is the best structure for a project in codeigniter and how to use it?
Fair warning, this type of question will get shot down by many SO moderators, but I'll give you some tips regardless:
Controller actions are single-use. If you find yourself with duplicate code in multiple controllers (or, needing to call a controller function from another controller), that's a sure sign you should move that code to a model or library.
Models are object-specific, not action-specific. I wouldn't have a model dedicated to logins, unless you have multiple types of logins (most apps/sites just have member logins, but you might have administrators, etc. that are stored in a different table from the rest). Instead, have a
User_model
class, and makefunction login($email, $password)
a method of that class.Controller-to-model interaction should be very concise. If you find yourself with 30 lines of code passing data back and forth between the same controller and model, you might be trying to do too much with that one controller action.
Keep your models fat, controllers skinny, and views dumb.
Related Topics
Woocommerce - Get Category for Product Page
Refresh a Table with Jquery/Ajax Every 5 Seconds
Backslash in PHP -- What Does It Mean
How to Have Codeigniter Load Specific Pages Using Ssl
How to Manage a Single PHP5 Session on Multiple Apache Servers
Session Unset, or Session_Destroy
Combine PHP Prepared Statments with Like
Download a File from Ftp Using Curl and PHP
MySQL Get a Random Value Between Two Values
Fatal Error: Call to Undefined Function SQLsrv_Connect() in C:\Xampp\Htdocs
How to Render Zf2 View Within JSON Response
Laravel 4 Removing Public from Url
Select Random File from Directory
PHP Crop Image to Fix Width and Height Without Losing Dimension Ratio
How to Access JSON Decoded Array in PHP