# 在模板中使用函數
在視圖模板中使用函數包含3種情況,一種是PHP函數比如time()、datae()等。
另外就是調用 控制器中的函數。
最后就是框架的function.php中的用戶自定義的函數,框架用戶自定義函數請到 <span style="color:red;">**自定義函數**</span> 章節進行詳細了解。
<br/><br/>
#### 使用PHP函數的例子
~~~
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MAGPHP框架</title>
</head>
<body>
當前時間:<?=date('Y-d-m h:i:s', time())?>
<br/>
<?=str_replace('張三','李四','歡迎用戶張三登錄!')?>
<br/>
<?=md5('這是密碼')?>
</body>
</html>
~~~
<br/><br/>
#### 使用控制器中函數
~~~
class IndexController extends Controller{
public function getuser(){
$name = '張三';
return $name;
}
}
~~~
~~~
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MAGPHP框架</title>
</head>
<body>
用戶:<?=$this->getuser()?>
</body>
</html>
~~~
>[warning] 在/app/Views/Index/下模板文件中,但是不能是對應模板的方法,比如這里的getuser()方法下,不能在模板中調用$this->getuser()方法。
<br/><br/>
#### 使用框架自定義函數的例子
公共函數文件中/Lib/function.php
~~~
class PF{
public static function geTime(){
$isTime = date('Y-d-m', time());
return $isTime;
}
}
~~~
公共函數文件中/app/Lib/function.php
~~~
class AF{
public static function geTime(){
$isTime = date('h:i:s', time());
return $isTime;
}
public static function getUser(){
$user = '張三';
return $user;
}
}
~~~
~~~
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MAGPHP框架</title>
</head>
<body>
公共自定義函數:<?=PF::geTime()?>
<!-- 輸出:2016-12-22 -->
<br/>
app應用中自定義函數:<?=AF::geTime()?>
<!-- 輸出:18:56:57 -->
<br/>
app應用中自定義函數:<?=AF::getUser()?>
<!-- 輸出:張三 -->
</body>
</html>
~~~
>[warning] 框架用戶自定義函數請到 <span style="color:red;">**自定義函數**</span> 章節進行詳細了解
<br/><br/>