How to add a New Format to your Joomla 3 Component

This tutorial series, Joomla 3 component development, is currently in progress…

In our last few tutorials about creating components within Joomla 3, we’ve been creating new views. The default format for views is HTML format. However, you can setup your views to return data in other formats too, such as json or xml.

Creating a new format

  1. Create your new “view format” file: /components/com_helloworld/views/helloworld/view.xml.php

    file formats example

    In our testing, we are creating an xml format, but you can create other formats too. For example, if you wanted to create a json format, you would create:
    /components/helloworld/views/helloworld/view.json.php

  2. In the file you just created, write the php code that you would like to ultimately create your XML file. Here’s our example code:
    <?php // No direct access to this file defined('_JEXEC') or die('Restricted access');   // import Joomla view library jimport('joomla.application.component.view');   /**  * XML View class for the HelloWorld Component  */ class HelloWorldViewHelloWorld extends JViewLegacy {         // Overwriting JView display method         function display($tpl = null)          {             echo "<?xml version='1.0' encoding='UTF-8'?> <article>   <title>How to create a Joomla Component</title>   <alias>create-component</alias> </article>";         } }

Testing our new format

To test our new format, call it via the URL as in this example:

https://example.com/index.php?option=com_helloworld&format=xml

If you followed along with this tutorial, your Joomla component should output the following:

xml output

0 thoughts on “How to add a New Format to your Joomla 3 Component

  1. When i call the view via the URL as in this example https://example.com/index.php?option=com_helloworld&format=xml i get the follow error:

    This page contains the following errors:

    error on line 1 at column 1: Document is empty

    Below is a rendering of the page up to the first error.

     

    Any ideas?

     

    1. That error indicates that a piece of code, likely a php function, is not closed properly. You may need to add a closing tag.

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+