CodeIgniter Forums

Full Version: xsl templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum


I'm trying get some sort of xsl templating in CI.
First idea was to put all the xsl files in the view folder as views but the thing is when you do that you can't get the source from it.

$contentXsl = $this->load->view('templates/content.xsl');

$contentXml = $this->get->content() // returns xml for content

echo $this->xlsparser->parse($contentXml, $contentXsl); //outputs xhtml

(simple example of the idea)

I now got a model that parses the xml and xsl to a nice xhtml, but it now needs the template and source both as string, which isn't acceptable because then i have to define all the templates myself, and the idea is that one of the designers can make xls templates, my models deliver the xml for that template and the parser makes the xhtml.

// Load the XML source
$xml = new DOMDocument;
$xml->loadxml($source);  // or load() if it's a file

// Load the XSL source
$xsl = new DOMDocument;
$xsl->loadxml($template);  // or load() if it's a file

// Configure the transformer
$proc = new XSLTProcessor;

// Enable ability to use php functions

// attach the xsl rules

// output result
return $proc->transformToXML($xml);

What is the best place for storing those xsl files?
Or better yet, can someone point me in a direction to make something like this work?

El Forum

Got it working now, but not sure if it´s the CI way.

added in config.php:
$templatePath = str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']);
$config['template_path'] = $templatePath.'templates/';

added in url_helper:
if ( ! function_exists('template_path'))
    function template_path()
        $CI =& get_instance();
        return $CI->config->slash_item('template_path');

and the controller:
// returns xml for content
$contentXml = $this->get->content();

// get path to template
$template = template_path().'index.xsl';

// parse template from xsl with the xml
$data->content = $this->xslparser->init($contentXml, $template);

// output the template
$this->load->view('templates/index.tpl.php', $data);

I'm open to suggestions if this isn't the way to go.