# PHP phalcon volt ***volt 的代碼是由 PHP 和 HTML 構成*** ## {{ ... }} Assignments賦值 ``` {% set fruits = ['Apple', 'Banana', 'Orange'], name = robot.name %} {% set price += 100.00 %} {% set age *= 5 %} ``` ## {{ ... }} Variables變數 ``` {{ fruits[2] }} {{ post.title }} {# for $post->title #} {{ post['title'] }} {# for $post['title'] #} ``` ## {{ ...\|... }} Filters過濾器 ``` {{ post.title|e }} {{ post.content|striptags }} {{ name|capitalize|trim }} ``` | Filter | Description | Example | | -------- | -------- | -------- | | abs | 轉成正數 | | | capitalize | 首字母大寫 | | | default | 預設內容 | \{\{ num \| default('1') \}\} | | format | 格式化 | {{ "My real name is %s"\|format(fruits[2]\|capitalize) \}\} | | e or escape | 忽略tag(包含tags的字串) | {{ "\<h1>Hello\<h1>"\|e \}\} → \<h1>Hello\<h1> | | trim | 移除頭尾空白 | {{ " hello "\|trim \}\} | | striptags | 移除HTML之tags(剩內容) | {{ "\<h1>Hello\<h1>"\|striptags \}\} → Hello | | length | 長度 | {{ "robots"\|length \}\}<br/>{{[1,2,3] \|length\}\} | | keys | 回傳keys的array | {% set keys = ['first': 1, 'second': 2, 'third': 3]\|keys %\} | | join | 隔開陣列中每個元素的字串 | {% set joined = "a".."z"\|join(",") %\} | | json_encode | 對變數進行 JSON 編碼 | {% set encoded = robots\|json_encode %\} | | json_decode | 對 JSON 格式的字串進行編碼 | {% set decoded = '{"one":1,"two":2,"three":3}'\|json_decode %\} | ## {% ... %} 循環語句或判斷 ``` {% for index in 0..fruits|length %} {% if fruits[index] is defined %} {{ 'Name: ' ~ fruits[index] }} {% endif %} {% endfor %} ``` ## {# ... #} 注釋 ``` {# {% fruits[2] %} #} ``` ## Loop Context 循環上下文 ``` {% for robot in robots %} {% if loop.first %} <table> <tr> <th>#</th> <th>Id</th> <th>Name</th> </tr> {% endif %} <tr> <td>{{ loop.index }}</td> <td>{{ robot.id }}</td> <td>{{ robot.name }}</td> </tr> {% if loop.last %} </table> {% endif %} {% endfor %} ``` | Variable | Description | | -------- | -------- | | loop.index | 目前迴圈的索引(從1開始) | | loop.index0 | 目前迴圈的索引(從0開始) | | loop.revindex | 目前迴圈剩餘次數(從1開始) | | loop.revindex0 | 目前迴圈剩餘次數(從0開始) | | loop.first | 若是迴圈第一個則回傳 true | | loop.last | 若是迴圈最後一個則回傳 true | | loop.length | 總迴圈長度 | ## Other Operators 其他 | Operator | Description | | -------- | -------- | | ~ | 結合運算符 {{ 'hello ' ~ 'world' }} → hello world | | .. | Creates a range {{ 'a'..'z' }} {{ 1..10 }} | | is | Same as == (equals) | | in | if 'a' in 'abc' | | is not | Same as != (not equals) | | 'a' ? 'b' : 'c' | 三元運算 | ## Tests 測試運算 ``` {% set robots = ['1': 'Voltron', '2': 'Astro Boy', '3': 'Terminator', '4': 'C3PO'] %} {% for position, name in robots %} {% if position is odd %} {{ name }} {% endif %} {% endfor %} ``` | Test | Description | | -------- | -------- | | defined | Checks if a variable is defined (isset()) | | divisibleby | Checks if a value is divisible by other value | | empty | Checks if a variable is empty | | even | Checks if a numeric value is even | | odd | Checks if a numeric value is odd | | iterable | Checks if a value is iterable. Can be traversed by a ‘for’ statement<br/>{% set robots = [1: 'Voltron', 2: 'Astroy Boy'] %}<br/>{% if robots is iterable %}<br/>{% for robot in robots %}<br/>{% endfor %}<br/>{% endif %} | | sameas | Checks if a value is identical to other value <br/> {% set world = 'hello' %} <br/>{% if world is sameas('hello') %} | | numeric | Checks if value is numeric | | scalar | Checks if value is scalar (not an array or object) | | type | Checks if a value is of the specified type <br/> {% if external is type('boolean') %} | --- ### 來源 1. https://docs.phalcon.io/3.4/en/volt#introduction 2. http://www.iphalcon.cn/reference/volt.html