Creating My Own View&Layout for Joomsport
Now let's continue with the investigation on the topic -- How does Joomla! determine the views/layouts when specifying menu type for a menu item.
We've already explored the theory the the pratice about that, now its time to get hands dirty, let's do a little trial to verify our conclusion. First of all, create a folder named 'test' under:
/components/com_joomsport/views/
And create the files as shown:
Add the files content.
view.html.php:
<?php
// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
class bleagueViewtest extends JView
{
function display($tpl = null)
{
$s = "This is a test string";
$this->assignRef('testStr', $s);
parent::display($tpl);
}
}
metadata.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<view title="Test">
<message><![CDATA[Test]]></message>
</view>
</metadata>
tmpl/default.php:
<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<table border="1px">
<tr>
<th>header0</th><th>header1</th><th>header2</th>
</tr>
<tr>
<td><?php echo $this->testStr ?></td><td>t01</td><td>t02</td>
</tr>
<tr>
<td>t10</td><td>t11</td><td>t12</td>
</tr>
<tr>
<td>t20</td><td>t21</td><td>t22</td>
</tr>
</table>
tmpl/default.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Test Layout">
<message>
<![CDATA[Test Layout]]>
</message>
</layout>
<state>
<name>Test Layout</name>
<description>Test Layout</description>
<url></url>
<params>
</params>
</state>
</metadata>
We are ready to test now. Go to the backend, select one menu item of main menu, change its type, and navigate to the panel:
select the test view we just created, since there is no parameter for the view, just directyly save. Open the browser, go to the front end of Joomla! site, and click that menu item just edited, then we can see the result:
Caution!
When you use assignRef method inview.html.php to regisiter your variables, you must pass a variable as the second argument,rather than passing expression or literal values, such as:
$this->assignRef('test', "this is wrong");
that will not work, and throw errors.
The correct approach is:
$s = "This is goos pratice";
$this->assignRef('test', $s);