Welcome Guest, Not a member yet? Register   Sign In
Practical problem in implementing MVC with CodeIgniter
#2

[eluser]tonanbarbarian[/eluser]
Some MVC frameworks have things called Partials or Elements. These are like mini views that can be included in other view.

I did a quick look on the wiki but cannot find the code example for it used perviously.

Basically you create a new plugin or helper

application/helpers/elements.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

function renderElement($view, $params, $display=false) {
  static $CI = null;
  if ($CI===null) $CI =& get_instance();

  $return = $CI->load->view($view, $params, true);
  if ($display)
    echo $return;
  else
    return $return;
} // renderElement()
?>

So this is a very simple element rendering engine. You need to load the helper first of course (in the controller or autoload by preference). Then you simply call renderElement and pass it the name of the view to load, and optionally some params for the view (data variables to create in the view) and whether to display or return the data.

usage in view
Code:
<html>
  <head>
    <?php echo renderElement('headers/head.php'); ?>
  </head>
<body>
<div class="header">
  &lt;?php renderElement('headers/page.php', array('title'=>$title), true); ?&gt;
</div>
...
&lt;/body&gt;
&lt;/html&gt;

So the first call loads a view in application/views/headers/head.php and must use echo because it is returning the data of the element.
The second call is loading application/views/headers/page.php and passing it a variable called title. It is automatically displaying the element because its display parameter is set to true so it does not need the echo

So using something like this you can modularize your views into elements that can be included wherever needed
use this for JS, CSS, anything you want, even individual image tags or anything that is included in the view


Messages In This Thread
Practical problem in implementing MVC with CodeIgniter - by El Forum - 12-13-2007, 11:17 AM
Practical problem in implementing MVC with CodeIgniter - by El Forum - 12-13-2007, 02:28 PM
Practical problem in implementing MVC with CodeIgniter - by El Forum - 12-13-2007, 02:45 PM
Practical problem in implementing MVC with CodeIgniter - by El Forum - 12-13-2007, 04:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB