{~W('Menu/title',array(5,'thinkphp'))}

Widget实例
Widget类的实现可以作为分层控制器的另外一个典型实例。
举个例子,我们在页面中实现一个分类菜单的Widget,首先我们要定义一个Widget控制器层
MenuWidget,如下:
namespace Home\Widget;
class MenuWidget extends Think\Controller {
public function index(){
echo 'menuWidget';
}
}
类文件位于 Home/Widget/MenuWidget.class.php 。
然后,我们在需要显示分类菜单的模版中通过W方法调用这个Widget。
{~W('Menu/index')}
执行后的输出结果是: menuWidget
如果需要在调用Widget的时候传入参数,可以这样定义:
namespace Home\Widget;
class MenuWidget extends Think\Controller {
public function index($id,$name){
echo $id.':'.$name;
}
}
在需要显示分类菜单的模版中添加如下的Widget调用代码如下:
{~W('Menu/index',array(5,'thinkphp'))}
ThinkPHP3.2.3快速入门
本文档使用 看云 构建 - 104 -
则会输出 5:thinkphp

 

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\View\Index\index2.html

<layout name="Layout/newlayout" />
<body>
{~W('Menu/index')}
<hr>
{~W('Menu/title',array(5,'thinkphp'))}
<hr>
<form method="post" action="">
    <input type="submit">
</form>
</body>
<?php
//D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\Widget\MenuWidget.class.php
namespace OuterMok2\Widget;
class MenuWidget extends \Think\Controller {
    
public function index(){
    echo 'menuWidget';
}

public function title($id,$name){
    echo $id.':'.$name;
}

}

 

posted @ 2017-12-18 14:31  sky20080101  阅读(451)  评论(0)    收藏  举报