Boris Garkoun
Boris Garkoun

Reputation: 127

Is it better to add new views or layouts of one view for the Joomla component?

I'm developing a component for Joomla! 3. It has a "contact" view displaying three links which leads to the:

  1. list of all available contact books.
  2. form for adding a contact to some available book.
  3. page where you can import your contacts.

This page is displayed by means of a layout with the name default.php of a "contact" view. How should I implement this 3 pages? Is it better to display them with the same view by means of its separate layouts or it more correct to create three separate views for each from the mentioned pages? All these three pages are all about the work with contacts.

Upvotes: 0

Views: 310

Answers (1)

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6418

You can do it using the same view using different layouts. Say you have three layouts-

  1. default layout, where you show all available contact books.
  2. form layout, where you show the form for adding a contact.
  3. import layout, where you show the page of import contacts.

Then make three layouts at

component/com_yourcomponent/views/contact/tmpl/default.php
component/com_yourcomponent/views/contact/tmpl/default_form.php
component/com_yourcomponent/views/contact/tmpl/default_import.php

Now you can access the layouts by using the layout query at the URL. Say for showing the form layout you have to hit the URL-

http://yoursite.com/index.php?option=com_yourcomponent&view=contact&layout=form

If I want to access the layouts from the menu.

Yes, If you want to access them from the menu then you have to do some more job. You have to create three more files inside the same view's tmpl directory along with layout .php files. The files path would be-

component/com_yourcomponent/views/contact/tmpl/default.xml
component/com_yourcomponent/views/contact/tmpl/form.xml
component/com_yourcomponent/views/contact/tmpl/import.xml

And a sample .xml file (say form.xml) looks like-

    <?xml version="1.0" encoding="utf-8"?>
    <metadata>
        <fields name="params">
            <fieldset name="basic" label="Basic">
                <!-- Your menu settings params are here-->
            </fieldset>
        </fields>
        <layout title="COM_YOURCOMPONENT_FORM_VIEW_DEFAULT_TITLE" option="COM_YOURCOMPONENT_FORM_VIEW_DEFAULT_OPTION">
            <help
                key = "JHELP_MENUS_MENU_ITEM_CONTACT_FORM"
            />
            <message>
                <![CDATA[COM_YOURCOMPONENT_FORM_VIEW_DEFAULT_DESC]]>
            </message>
        </layout>
    </metadata>

And you have to do some more extra job at component/com_yourcomponent/views/contact/view.html.php and that is, set the layout comes from the menu at the display() function.

$this->layout_type = str_replace(':_', '', $layout);
$this->setLayout($this->layout_type);

Remember

There is only one place for data manipulation for all the view layouts. So you can catch the layout name by using $this->layout_type and apply your business logic conditionally.

Hope, this will help you.

Upvotes: 2

Related Questions