Insight Joomla! (DAY 5) - Examing Joomsport (DAY 3): My First View

62 篇文章 0 订阅

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:


joomsportViewtest


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:

change-nemu-view-test


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:


view-layout-test-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);





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值