Displaying Raw Output in Joomla 1.5 by Setting Format in the Component

The most common way to produce raw output in Joomla is to pass the format= raw parameter in the url.

Joomla has four supported document type objects: html, pdf, feed and raw. When passing no format parameter the default is html. Html format is just the normal html output. This article covers the use of raw. You use raw to generate content that do not fit within the other document types.

The following method will allow you to set the document format within the component itself. This might be useful if you want to keep your url clean of any query string values.

You cannot simply do a setVar(‘format’,'raw’) in the component. The document type is set before the component is even called. So you have to manipulate the document object itself in order to set the format within the component.

Place the following three lines in the display() function of the controller before the view is displayed.

You first get an instance of the current document object.


$document = &JFactory::getDocument();

Then get instance of the raw document object.


$doc = &JDocument::getInstance('raw');

Replace the current document type with the new one.


$document = $doc;

This has the same effect as passing format=raw in the url.

Now set the mime type of the document. This document will be a rss feed.


$document->setMimeEncoding('application/rss+xml');

So This is what the display() function looks like in your controller file


	function display()
	{
		$document = &JFactory::getDocument();
		$doc = &JDocument::getInstance('raw');
		$document = $doc;
		$document->setMimeEncoding('application/rss+xml');

		if ( ! JRequest::getCmd( 'view' ) ) {
			JRequest::setVar('view', 'someview' );
		}

		parent::display();
	}

Once you do this the controller will be looking for the view entry file named view.raw.php in the /components/com_somecomponent/views/someview folder. Just copy the view.html.php file and name it view.raw.php

Leave a comment

8 Comments.

  1. Do you konw if this can be done with Joomla 1.6. I’ve tried your method and did’nt work

  2. Have not tried this in 1.6

  3. Thank you this helped a lot (on 1.5.22)

  4. Joomla 1.6 not support auto create text encoding setting on url, also you can not change document type in your ‘view’. So, you need create an view.raw.php file in your ‘view’ and call it on url format=raw. I have tested.

  5. Thank you for the tutorial.

  6. Thanks Andy Nagai, it was really so helpfull, keep posting ur gr8 works…

  7. Thank you for the JDocument::getInstance, I couldn’t generate a pdf from a task and that made my day :)

  8. Good info. Thanks!