1

I'm trying to display an XML feed in a custom Joomla 2.5 component's view/layout, but the XML is rendered as a regular layout inside the site's HTML template. How can I display the XML without any template HTML code?

(The trick to include tmpl=component in the URL from this related question doesn't help, there's still some HTML output from the template that ruins the XML.)

I would prefer a solution that only involves code changes in my custom component, like in Symfony when you call the method setLayout(false).

Community
  • 1
  • 1
Christian Davén
  • 16,713
  • 12
  • 64
  • 77

3 Answers3

2

The only solution I have found is to create a file in the current template folder, e.g. "xml.php", and put only this in the file:

<?php
$document = JFactory::getDocument();
$document->setMimeEncoding('text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<jdoc:include type="component" />

Then I can append tmpl=xml to the URL.

Christian Davén
  • 16,713
  • 12
  • 64
  • 77
  • I added defined( '_JEXEC' ) or die( 'Restricted access' ); to the top line. Works like a charm! – Jim Jun 06 '12 at 15:10
2

[edit] My bad, I made an assumption and you know what that gets you.

Joomla! 1.6->2.5 you can create an alternate output format for an existing view by:

  1. calling the view with a format parameter attached e.g. &format=json
  2. creating a matching view class file e.g. view.json.php that can sit alongside the standard view.html.php file for you view.

The view.yourformat.php file can use your existing controllers and template files in the normal fashion.

Don't forget to add either &tmpl=component or &tmpl=raw to your query string so modules etc don't load as well.

tmpl=raw won't load the html body surrounds or template, only the main component.

[/edit]

From Joomla! 1.6 onward (including 2.5) there is built in support for controller formats ie. you create a controller for the output format you want.

Normally a controller would be named for each view:

/components/mycomp/controllers/myview.php

A XML version of the controller would be name:

/components/mycomp/controllers/myview.xml.php

A JSON version would be:

/components/mycomp/controllers/myview.json.php

To call a particular format version of a controller you simply add &format=theformatyouwant to the URL parameters, so in your case &format=xml

This is discussed in this document from 1.6 days - I used it as a basis for several of our components that have JSON and ics requirements.

Craig
  • 9,335
  • 2
  • 34
  • 38
  • This solution requires practically identical copies of the controller and view classes, which violates DRY. I only want another layout, not controller or view. But nonetheless, I learned something valuable from you, thanks! (Another solution could be to check for the XML type in the controller, change the layout and reset the type to HTML to reuse the same view class.) – Christian Davén Apr 03 '12 at 08:13
  • I'm missing something - if your content views are already XML, how does it violate DRY when all you have to do is load just your existing `/tmpl/` files or sub-template files into the new controller. Given your solution above that is. – Craig Apr 03 '12 at 10:19
  • Well, the new controller is a copy of the old controller, which contains some logic. And the new cannot inherit the old, since it seems it has to have the same class name. – Christian Davén Apr 04 '12 at 08:00
0

This issue drove me crazy a couple of times.

After much frustration, the simplest solution is the one suggested by cppl. In your query string put the following variables:

format=yourcustomformat
view=viewname

Let say you want json output from a view called json.

Create a veiw folder with the name of your view

json

And a file inside that folder called

view.json.php

Then in your url string you include the following url parameters seperated by the & symbol:

index.php?option=com_mycomponent&format=json&view=json

cppl is correct that this loads a non-html view. However you don't have to put the tmpl parameter in at least in 2.5. If the view name is not view.html.php then 2.5 seems to not include the assigned site template in the response. I think because the view is not veiw.html.php it assumes raw output and does not include the template. I tested this with both an ajax call and a direct url call to the view and in both cases all I got back was the component output. Yeah!

If someone knows where this issue is well documented by the Joomla folks please post!

Robert
  • 5,278
  • 43
  • 65
  • 115