Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 17 revisions

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: [code] $data[’wrap’] = htmlentities(‘<span class="littlebox>’); $this->render->wrap(’content’,$data); [/code] That’ll produce the equivalent of [code]

Your content here...

[/code]

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.

[code]$this->render->wrap('header',array('wrap'=>htmlentities('<body onload="init()">'))); $this->load->view('content'); $this->render->wrap('footer',array('wrap'=>htmlentities('</body>')));[/code]

I also threw in my XSLT send() function.

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

Codeigniter will not let you pass unencoded entities, so your tags will have to be within htmlentities(). [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**

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->view($view, $vars, $return);
            $this->bypass_view($vars['wrap']);
        } elseif (strstr($vars['wrap'],'&gt;&lt;/') == FALSE)
        {
            $this->bypass_view($vars['wrap']);
            $this->view($view, $vars, $return);
        } else
        {
            $bookends = explode('&gt;&lt;/',$vars['wrap'],2);
            $this->bypass_view($bookends[0].'&gt;');
            $this->view($view, $vars, $return);
            $this->bypass_view('&lt;/'.$bookends[1]);
        }
    } else
    {
        log_message('debug', "No wrapping for ".$view);
        $this->view($view, $vars, $return);
    }
}

/**
 * bypass_view
 * 
 * All the benefits of a view sans the file.
 */

function bypass_view($rushtml)
{
    ob_start();
    echo html_entity_decode($rushtml);
    // PHP 4 requires that we use a global
    global $OUT;
    $OUT->append_output(ob_get_contents());
    @ob_end_clean();
}

/**
 * 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('base',null,true));
    $xslt = new XSLTProcessor();
    $xslt->importStylesheet($xsl);
    $html = $xslt->transformToXML($xml);
    $this->output->set_output($html);
}

} ?> [/code]

Clone this wiki locally