## 扩展页面
- 扩展页面是除默认页面,(空扩展、htm、html)的页面后缀
- 扩展文件: method.php
- 共用文件: data.php、_init.php、ini.php、src.php、set.php
- 非共用文件: tpl.blade.php、tpl.js、tpl.css、tpl.scss
设置文件/html/www/demo/test/method/demo1/_init.php内容如下
```
<?php
return function (){
echo "<div>此处运行的是_init.php</div>";
};
```
设置文件/html/www/demo/test/method/demo1/my.blade.php内容如下
```
<div>运行文件名: my.blade.php</div>
```
设置文件/html/www/demo/test/method/demo1/my.scss内容如下
```
body {
background-color: #FEE;
div {
background-color: #FDD;
}
}
```
打开网址[http://demo.tphp.com/test/method/demo1.my](http://demo.tphp.com/test/method/demo1.my)获得如下效果![](/static/plugins/tphp/help/module/method/demo1.png)
生成源代码
![](/static/plugins/tphp/help/module/method/demo1_src.png)
生成的CSS
![](/static/plugins/tphp/help/module/method/demo1_css.png)
## method.php
设置文件/html/www/demo/test/method/demo2/_init.php内容如下
```
<?php
return function (){
echo "<div>此处运行的是_init.php</div>";
};
```
设置文件/html/www/demo/test/method/demo2/method.php内容如下
```
<?php
return new class {
/**
* 初始化
* 运行于_init.php之前
*/
public function _test() {
echo "<div>method.php文件: _test方法</div>";
}
/**
* 数据设置
* 运行于最后,在set.php之后运行
* @param $data: 和set.php用法一致
* @return mixed
*/
public function test($data) {
echo "<div>method.php文件: test方法</div>";
return $data;
}
};
```
设置文件/html/www/demo/test/method/demo2/set.php内容如下
```
<?php
return function (){
echo "<div>此处运行的是set.php</div>";
};
```
打开网址[http://demo.tphp.com/test/method/demo2.test](http://demo.tphp.com/test/method/demo2.test)获得如下效果![](/static/plugins/tphp/help/module/method/demo2.png)
## 内部调用示例
设置文件/html/www/demo/test/method/demo3/_init.php内容如下
```
<?php
class Abc {
public function test($tpl)
{
dump($tpl);
exit("cls 调用");
}
}
return function (){
// 是否 new 类, 默认为false
$isNew = false;
// 调用类方法,同样支持 exit、 auto等方法
// 更多方法文件路径: /vendor/tphp/tphp/src/Basic/Tpl/Method.php
$this->methodForClass('cls', Abc::class, 'test', $isNew);
// 类名也可以以下两种方式
// $this->methodForClass('cls', 'Abc', 'test');
// $this->methodForClass('cls', new Abc(), 'test');
// dump($this->runMethod("cls", "tpl参数变了"));
// 直接使用方法
$this->method("func", function ($tpl) {
exit("func 调用");
});
// return 不会显示
$this->method("no", function ($tpl) {
return "不会显示";
});
// return 会显示
$this->method("exit", function ($tpl) {
return "会显示";
})->exit();
// 关闭自动执行
$this->method("auto", function ($str) {
dump('这里需要调用runMethod才执行');
return "获取数据: {$str}";
})->auto(false);
// dump($this->runMethod("auto", "YES!"));
dump('这里会先运行');
};
```
打开网址[http://demo.tphp.com/test/method/demo3.cls](http://demo.tphp.com/test/method/demo3.cls)获得如下效果![](/static/plugins/tphp/help/module/method/demo3_cls.png)
打开网址[http://demo.tphp.com/test/method/demo3.func](http://demo.tphp.com/test/method/demo3.func)获得如下效果![](/static/plugins/tphp/help/module/method/demo3_func.png)
打开网址[http://demo.tphp.com/test/method/demo3.no](http://demo.tphp.com/test/method/demo3.no)获得如下效果![](/static/plugins/tphp/help/module/method/demo3_no.png)
打开网址[http://demo.tphp.com/test/method/demo3.exit](http://demo.tphp.com/test/method/demo3.exit)获得如下效果![](/static/plugins/tphp/help/module/method/demo3_exit.png)
打开网址[http://demo.tphp.com/test/method/demo3.auto](http://demo.tphp.com/test/method/demo3.auto)获得如下效果![](/static/plugins/tphp/help/module/method/demo3_auto.png)
## 默认扩展页面设置
设置文件/html/www/demo/test/method/demo4/_init.php内容如下
```
<?php
return function (){
$this->method("def", function () {
dump("执行了_init.php");
});
};
```
设置文件/html/www/demo/test/method/demo4/data.php内容如下
```
<?php
return [
"method" => "def"
];
```
设置文件/html/www/demo/test/method/demo4/def.blade.php内容如下
```
<div>执行了def.blade.php</div>
```
打开网址[http://demo.tphp.com/test/method/demo4](http://demo.tphp.com/test/method/demo4)获得如下效果![](/static/plugins/tphp/help/module/method/demo4.png)