Skip to content
Derek Jones edited this page Jul 4, 2012 · 17 revisions

Category:View | Category:View::Community | Category:View::Class

Do you love the simplicity of the CI view loader but just can't stomach loading a whole file just to add a couple of span tags and a whole other file just to close them? I think you might find a happy medium with this library.

Essentially, it’ll let you call a view with a wrapper command and some data like so:

$data[’wrap’] = htmlentities(‘<span class="bigbox"><span class="littlebox></span></span>’); 
$this->render->wrap(’content’,$data); 

That’ll produce the equivalent of ```php

Your content here...


If your 'wrap' data doesn’t have a middle, it'll prefix or post-fix the tags as you might expect.  I was aiming for the jQuery 'wrap' function, if you’re familiar with it.

```php
$this->render->wrap('header',array('wrap'=>htmlentities('&lt;body onload="init()"&gt;')));
$this->load->view('content');
$this->render->wrap('footer',array('wrap'=>htmlentities('&lt;/body&gt;')));

I also threw in my XSLT send() function.

Codeigniter will not let you pass unencoded entities, so your tags will have to be within htmlentities().

[em]You will need to rewrite all of the html brackets in this code before you can use it. This wiki automatically interpret's them into < and >. Make sure that you put them back to &lt; and &gt; as I intended.[/em]

&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter Render Class
 *
 * @package        CodeIgniter
 * @subpackage    Library
 * @category    View
 * @author        Bradford Mar
 * @version        0.1
 * @link        http://codeigniter.com/wiki/render/
 * @license        http://creativecommons.org/licenses/by-sa/3.0/
 */

class Render
{
    /**
     * wrap
     * 
     * Quickly wrap tags around your view.
     */
    
    function wrap($view, $vars = array(), $return = FALSE)
    {
        if (isset($vars['wrap']))
        {
            if (substr($vars['wrap'],0,5) == '&lt;/')
            {
                $this->load->view($view, $vars, $return);
                $this->bypass_view($vars['wrap']);
            } elseif (strstr($vars['wrap'],'&gt;&lt;/') == FALSE)
            {
                $this->bypass_view($vars['wrap']);
                $this->load->view($view, $vars, $return);
            } else
            {
                $bookends = explode('&gt;&lt;/',$vars['wrap'],2);
                $this->bypass_view($bookends[0].'&gt;');
                $this->load->view($view, $vars, $return);
                $this->bypass_view('&lt;/'.$bookends[1]);
            }
        } else
        {
            log_message('debug', "No wrapping for ".$view);
            $this->load->view($view, $vars, $return);
        }
    }

    /**
     * bypass_view
     * 
     * All the benefits of a view sans the file.
     */
    
    function bypass_view($rushtml)
    {
        // PHP 4 requires that we use a global
        global $OUT;
        $OUT->append_output(html_entity_decode($rushtml));
    }

    /**
     * send
     * 
     * Call this after loading all of your views to run XSLT before sending.
     */

    function send($xslview)
    {
        $xml = new DOMDocument();
        $xml->loadXML($this->output->get_output());
        $xsl = new DOMDocument();
        $xsl->loadXML($this->load->view($xslview,null,true));
        $xslt = new XSLTProcessor();
        $xslt->importStylesheet($xsl);
        $html = $xslt->transformToXML($xml);
        $this->output->set_output($html);
    }
}
?&gt;
Clone this wiki locally