ThinkPHP 模板引擎提供豐富的檔案引入與流程控制標籤。本文整理 CSS/JS 引入三種方式、if/elseif/else 條件判斷、switch 多條件分支的完整語法與運算子對照表。
<link rel="stylesheet" type="text/css" href="__PUBLIC__/Css/style.css" />
<script src="__PUBLIC__/Js/main.js"></script>
<import type="js" file="Js.common" /> <!-- Public/Js/common.js -->
<import type="css" file="Css.layout" /> <!-- Public/Css/layout.css -->
<import type="js" file="Js.custom" basepath="./Other" /> <!-- Other/Js/custom.js -->
優點:路徑簡潔、自動補全副檔名、支援自訂基礎路徑。
<load href="__PUBLIC__/Js/main.js" />
<load href="__PUBLIC__/Css/style.css" />
自動根據副檔名輸出對應標籤,無需指定 type。
<if condition="$sex eq '男'">
男人是泥巴做的
<else />
女人是水做的
</if>
<if condition="$age lt 18">
未成年
<elseif condition="$age eq 18" />
青春年少
<else />
成年
</if>
| 模板 | PHP |
|---|---|
| eq | == |
| neq | != |
| gt | > |
| lt | < |
| egt | >= |
| elt | <= |
| heq | === |
| nheq | !== |
<switch name="number">
<case value="1">一個和尚挑水吃</case>
<case value="2">兩個和尚抬水吃</case>
<case value="3">三個和尚沒水吃</case>
<default />這裡是預設值
</switch>
注意:name 屬性填變量名稱(不含 $ 符號),value 可為字串或數字。
<if condition="$showPanel"><include> 提升可讀性