{"id":615,"date":"2023-02-26T10:56:53","date_gmt":"2023-02-26T02:56:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=615"},"modified":"2023-04-29T20:19:04","modified_gmt":"2023-04-29T12:19:04","slug":"introduction-to-twig-template-language","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/introduction-to-twig-template-language\/","title":{"rendered":"Twig\u6a21\u7248\u8bed\u8a00\u5165\u95e8"},"content":{"rendered":"<p>Twig\u4e2d\u6709\u4e24\u79cd\u5b9a\u754c\u7b26 <code>{% raw %} {% ... %}{% endraw %}<\/code> \u548c <code>{% raw %} {{ ... }}{% endraw %}<\/code> \uff0c \u524d\u4e00\u79cd\u7528\u6765\u6267\u884c\u8bed\u53e5\uff0c\u6bd4\u5982for\u5faa\u73af\u3001IF\u5224\u65ad\u3001\u8fc7\u6ee4\u7b49\u64cd\u4f5c\uff0c\u540e\u4e00\u79cd\u6a21\u7248\u4e2d\u7528\u6765\u663e\u793a\u53d8\u91cf\u3002Twig\u5e38\u7528\u7528\u6cd5\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n<h2>\u53ef\u7528\u7b26\u53f7<\/h2>\n<pre><code>== != &lt; &gt; &gt;= &lt;= + - ~ * \/ \/\/ % ** | [] . .. and or not in is b-and b-or b-xor<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u90e8\u5206\u7b26\u53f7\u7684\u542b\u4e49\u5982\u4e0b<\/p>\n<ul>\n<li><code>~<\/code>\uff1a\u8fde\u63a5\u4e24\u5b57\u7b26\u4e32\uff0c\u76f8\u5f53\u4e8e PHP \u4e2d\u7684\u70b9\u53f7<\/li>\n<li><code>\/\/<\/code>\uff1a\u6574\u9664<\/li>\n<li><code>**<\/code>\uff1a\u4e58\u65b9\uff0c\u76f8\u5f53\u4e8e PHP \u4e2d\u7684 ^<\/li>\n<li><code>b-and\u3001b-or\u3001b-xor<\/code>\uff1a\u6309\u4f4d\u4e0e\u3001\u6309\u4f4d\u6216\u3001\u6309\u4f4d\u5f02\u6216<\/li>\n<li><code>-<\/code>\uff1a\u51cf\u6cd5\u4ee5\u53ca\u53bb\u9664\u7a7a\u767d\u7684\u7b80\u5199\u7528\u6cd5\uff0c\u5982 <code>{% raw %} {{- \u6570\u636e }}{% endraw %}<\/code> =&gt; \u53bb\u9664\u5de6\u8fb9\u7684\u7a7a\u767d\u3001<code>{% raw %} {{ \u6570\u636e -}}{% endraw %}<\/code> =&gt; \u53bb\u9664\u53f3\u8fb9\u7684\u7a7a\u767d\u3001<code>{% raw %} {{- \u6570\u636e -}}{% endraw %}<\/code> =&gt; \u53bb\u9664\u4e24\u8fb9\u7684\u7a7a\u767d<\/li>\n<\/ul>\n<h2>\u6ce8\u91ca<\/h2>\n<pre><code>{# This is Twig Comment #}<\/code><\/pre>\n<h2>\u53d8\u91cf<\/h2>\n<p>foo \u4e3aPHP\u8d4b\u503c\u53d8\u91cf\uff0c \u5219\u6a21\u7248\u4e2d\u53ef\u4ee5\u4f7f\u7528 <code>{% raw %} {{ foo }}{% endraw %}<\/code>\u3002\u5982\u679c\u8be5\u53d8\u91cf\u4e3a\u6570\u7ec4\u6216\u8005\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 <code>{% raw %} {{ foo.bar }}{% endraw %}<\/code>\u6765\u8868\u793a\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u5982 <code>{% raw %} {{ foo.getName }}{% endraw %}<\/code>\u6216\u5f53\u6709\u53c2\u6570\u4f20\u9012\u65f6\u4f7f\u7528 <code>{% raw %} {{ foo.getName(p1, p2, p3) }}{% endraw %}<\/code><\/p>\n<p>\u5f53\u6a21\u7248\u4e2d\u4f7f\u7528 <code>{% raw %} {{ foo.bar }}{% endraw %}<\/code>\u65f6\uff0cPHP\u4f1a\u505a\u5982\u4e0b\u5224\u65ad\u6765\u68c0\u6d4b\u5bf9\u5e94\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<ol>\n<li>\u68c0\u6d4bfoo\u662f\u5426\u662f\u6570\u7ec4\uff0c\u5e76\u4e14bar\u662f\u4e00\u4e2a\u6709\u6548\u7684KEY<\/li>\n<li>\u5982\u679cfoo\u4e3a\u5bf9\u8c61\uff0c\u5219\u68c0\u6d4bbar\u662f\u5426\u4e3a\u6709\u6548\u7684\u5c5e\u6027<\/li>\n<li>\u5982\u679cfoo\u4e3a\u5bf9\u8c61\uff0c\u4f46bar\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u5c5e\u6027\uff0c\u5219\u68c0\u6d4bbar\u662f\u5426\u4e3a\u6709\u6548\u65b9\u6cd5<\/li>\n<li>\u5982\u679cfoo\u4e3a\u5bf9\u8c61\uff0c\u4f46bar\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u5219\u68c0\u6d4bgetBar\u662f\u5426\u4e3a\u6709\u6548\u65b9\u6cd5<\/li>\n<li>\u5982\u679cfoo\u4e3a\u5bf9\u8c61\uff0c\u4f46getBar\u4e0d\u662f\u6709\u6548\u65b9\u6cd5\uff0c\u5219\u68c0\u6d4bisBar\u662f\u5426\u4e3a\u6709\u6548\u65b9\u6cd5<\/li>\n<li>\u5982\u679c\u6ca1\u6709\uff0c\u5219\u8fd4\u56denull<\/li>\n<\/ol>\n<p>\u5f53foo\u4e3a\u6570\u7ec4\u65f6\uff0c\u6a21\u7248\u4e5f\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<code>{% raw %} {{ foo[&#039;bar&#039;] }}{% endraw %}<\/code><\/p>\n<p>\u53d8\u91cf\u672a\u5b9a\u4e49\u9ed8\u8ba4\u503c\u8bbe\u7f6e\uff1a<code>{% raw %} {{ var|default(&#039;var is not defined&#039;) }}{% endraw %}<\/code><\/p>\n<p>\u53ef\u4f7f\u7528 <code>{% raw %} {% set \u53d8\u91cf\u540d=\u53d8\u91cf\u503c %}{% endraw %}<\/code> \u58f0\u660e\u53d8\u91cf\uff0c\u4e5f\u53ef\u5199\u6210 <code>{% raw %} {% set \u53d8\u91cf\u540d %} \u53d8\u91cf\u503c {% endset %}{% endraw %}<\/code><\/p>\n<p>PHP \u4e2d\u975e\u5173\u8054\u6570\u7ec4\u88ab\u6620\u5c04\u6210 <code>[\u5143\u7d201, \u5143\u7d202, ...]<\/code>\uff0c\u5173\u8054\u6570\u7ec4\u5219\u88ab\u6620\u5c04\u6210 <code>{\u952e1: \u503c1, \u952e2: \u503c2, ...}<\/code><\/p>\n<h2>\u6570\u7ec4\u904d\u5386<\/h2>\n<h3>\u6309\u6570\u7ec4value\u904d\u5386\uff1a<\/h3>\n<pre><code>{% for value in foo %}\n    {{ value }}\n{% endfor %}<\/code><\/pre>\n<h3>\u6309\u6570\u7ec4key\u904d\u5386\uff1a<\/h3>\n<pre><code>{% for key in foo|keys %}\n    {{ key }}\n{% endfor %}<\/code><\/pre>\n<h3>\u6309key\uff0cvalue\u904d\u5386<\/h3>\n<pre><code>{% for key, value in foo %}\n    {{ key}}:{{value }}\n{% endfor %}<\/code><\/pre>\n<h3>\u5982\u679c foo \u975e\u6570\u7ec4\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528else\u8bed\u53e5\uff0c\u5982\uff1a<\/h3>\n<pre><code>{% for key, value in foo %}\n    {{ key}}:{{value }}\n{% else %}  \n    foo is Not a Array\n{% endfor %}<\/code><\/pre>\n<h3>\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5e26\u6761\u4ef6\uff0c\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u65f6\u6bd4\u8f83\u6709\u7528\uff0c\u53ef\u7528value.field\u6765\u5224\u65ad\uff1a<\/h3>\n<pre><code>{% for key, value in foo if value == 1%}\n    {{ key}}:{{value }}\n{% endfor %}<\/code><\/pre>\n<h3>\u5faa\u73af\u4f53\u5185\u90e8\u53d8\u91cf<\/h3>\n<ul>\n<li>loop.index \u5faa\u73af\u7684\u6b21\u6570\uff08\u4ece1\u5f00\u59cb\uff09<\/li>\n<li>loop.index0 \u5faa\u73af\u7684\u6b21\u6570\uff08\u4ece0\u5f00\u59cb\uff09<\/li>\n<li>loop.revindex \u5faa\u73af\u5269\u4f59\u6b21\u6570\uff08\u6700\u5c0f\u503c\u4e3a1\uff09<\/li>\n<li>loop.revindex0 \u5faa\u73af\u5269\u4f59\u6b21\u6570\uff08\u6700\u5c0f\u503c\u4e3a0\uff09<\/li>\n<li>loop.first \u5f53\u7b2c\u4e00\u6b21\u5faa\u73af\u7684\u65f6\u5019\u8fd4\u56detrue<\/li>\n<li>loop.last \u5f53\u6700\u540e\u4e00\u6b21\u5faa\u73af\u7684\u65f6\u5019\u8fd4\u56detrue<\/li>\n<li>loop.length \u5faa\u73af\u7684\u603b\u6570<\/li>\n<li>loop.parent \u88ab\u5faa\u73af\u7684\u6570\u7ec4<\/li>\n<\/ul>\n<h2>\u6761\u4ef6\u8bed\u53e5<\/h2>\n<h3>\u9700\u8981\u4f7f\u7528 or \u548c and \u4ee3\u66ff ||\u3001&amp;&amp;<\/h3>\n<pre><code>{% if a == &#039;1&#039; or b == &#039;2&#039; %}\n    {# do something #}\n{% endif %}<\/code><\/pre>\n<h3>\u5224\u65ad\u53d8\u91cf\u662f\u5426\u5b9a\u4e49<\/h3>\n<pre><code>{% if var is not defined %}\n    {# do something #}\n{% endif %}<\/code><\/pre>\n<h3>\u662f\u5426\u4e3aNULL<\/h3>\n<pre><code>{% if var is null %}\n    {# do something #}\n{% endif %}<\/code><\/pre>\n<h3>\u662f\u5426\u4e3afalse<\/h3>\n<pre><code>{% if var is sameas(false) %}\n    {# do something %}\n{% endif %}<\/code><\/pre>\n<h2>\u89e3\u6790\u5b9a\u754c\u7b26<\/h2>\n<pre><code>{{ &#039;{{&#039; }}<\/code><\/pre>\n<pre><code>{% raw %}\n    &lt;ul&gt;\n    {% for item in seq %}\n        &lt;li&gt;{{ item }}&lt;\/li&gt;\n    {% endfor %}\n    &lt;\/ul&gt;\n{% endraw %}<\/code><\/pre>\n<h2>\u63a7\u5236\u7ed3\u6784<\/h2>\n<ul>\n<li><code>{% raw %} {% if aaa %} xxx {% elseif bbb %} yyy {% else %} zzz{% endraw %}<\/code>\uff1a\u5224\u65ad\u8bed\u53e5<\/li>\n<li><code>{% raw %} {% for %} xxx {% endfor %}{% endraw %}<\/code>\uff1a\u8fed\u4ee3\u53d8\u91cf<\/li>\n<li><code>{% raw %} {% do %}{% endraw %}<\/code>\uff1a\u6ca1\u4ec0\u4e48\u5176\u4ed6\u542b\u4e49\uff0c<code>{% raw %}{% do 1+2 %}{% endraw %}<\/code> \u7b49\u540c\u4e8e <code>{% raw %}{{ 1+2 }}{% endraw %}<\/code><\/li>\n<li><code>{% raw %} {% flush %}{% endraw %}<\/code>\uff1a\u5237\u65b0\u8f93\u51fa\u7f13\u51b2\uff0c\u7b49\u540c\u4e8e flush<\/li>\n<li><code>{% raw %} {% include %}{% endraw %}<\/code>\uff1a\u5305\u542b\u6a21\u677f<\/li>\n<li><code>{% raw %} {% extends %}{% endraw %}<\/code>\uff1a\u6269\u5c55\u6a21\u677f<\/li>\n<li><code>{% raw %} {% embed %} xxx {% endembed %}{% endraw %}<\/code>\uff1a\u5305\u542b\u6a21\u677f\u5e76\u6269\u5c55\u8be5\u6a21\u677f\u7684\u5185\u5bb9\uff0c\u76f8\u5f53\u4e8e include \u548c extends \u7684\u7ed3\u5408\u4f53<\/li>\n<li><code>{% raw %} {% use %}{% endraw %}<\/code>\uff1a\u5305\u542b\u6a21\u677f\uff0c\u8fd1\u4f3c\u4e8e\u591a\u91cd\u7ee7\u627f<\/li>\n<li><code>{% raw %} {% from aaa import bbb as ccc %}{% endraw %}<\/code>\uff1a\u4ece\u6307\u5b9a\u6a21\u677f\u5bfc\u5165\u5b8f\u5e76\u8bbe\u7f6e\u522b\u540d<\/li>\n<li><code>{% raw %} {% macro %} xxx {% endmacro %}{% endraw %}<\/code>\uff1a\u5b9a\u4e49\u5b8f\u4ee5\u4fbf\u591a\u6b21\u8c03\u7528\uff0c\u4e0e\u5b9a\u4e49 PHP \u51fd\u6570\u65e0\u5f02<\/li>\n<li><code>{% raw %} {% sandbox %} {% include xxx %} {% endsandbox %}{% endraw %}<\/code>\uff1a\u5bf9\u5bfc\u5165\u7684\u6a21\u677f\u6307\u5b9a\u6c99\u7bb1\u6a21\u5f0f\uff0c\u53ea\u5bf9 include \u8bed\u53e5\u6709\u6548\uff0c\u53ea\u5728\u6c99\u7bb1\u6a21\u5f0f\u5df2\u5f00\u542f\u7684\u60c5\u51b5\u4e0b\u751f\u6548<\/li>\n<li><code>{% raw %} {% block xxx %} \u6216 {% block %} xxx {% endblock %}{% endraw %}<\/code>\uff1a\u5b9a\u4e49\u4ee3\u7801\u5757\u6216\u8986\u76d6\u4ee3\u7801\u5757<\/li>\n<li><code>{% raw %} {% set xxx %} \u6216 {% set %} xxx {% endset %}{% endraw %}<\/code>\uff1a\u5728\u6a21\u677f\u5185\u5b9a\u4e49\u53d8\u91cf<\/li>\n<li><code>{% raw %} {% filter %} xxx {% endfilter %}{% endraw %}<\/code>\uff1a\u591a\u884c\u8fc7\u6ee4\u5668<\/li>\n<li><code>{% raw %} {% spaceless %} xxx {% endspaceless %}{% endraw %}<\/code>\uff1a\u53bb\u9664 HTML \u7247\u6bb5\u4e2d\u7684\u7a7a\u683c<\/li>\n<li><code>{% raw %} {% autoescape %} xxx {% endautoescape %}{% endraw %}<\/code>\uff1a\u5c06\u5b57\u7b26\u4e32\u5b89\u5168\u5730\u5904\u7406\u6210\u5408\u6cd5\u7684\u6307\u5b9a\u6570\u636e<\/li>\n<li><code>{% raw %} {% verbatim %} xxx {% endverbatim %}{% endraw %}<\/code>\uff1a\u963b\u6b62\u6a21\u677f\u5f15\u64ce\u7684\u7f16\u8bd1\uff0c\u662f raw \u7684\u65b0\u540d\u5b57<\/li>\n<\/ul>\n<h2>\u5185\u5efa\u8fc7\u6ee4\u5668<\/h2>\n<p>\u8fc7\u6ee4\u5668\u7528\u6765\u4fee\u9970\u6570\u636e\uff0c\u5404\u8fc7\u6ee4\u5668\u53ef\u4ee5\u7528\u7ad6\u7ebf\u5206\u9694\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff0c\u7528\u62ec\u53f7\u4f20\u9012\u53c2\u6570\u3002\u4e5f\u53ef\u4ee5\u5c06\u8fc7\u6ee4\u5668\u5f53\u6210\u5355\u72ec\u7684\u51fd\u6570\u6765\u7528\uff0c\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>{% filter \u8fc7\u6ee4\u5668\u540d %}\n\u5f85\u5904\u7406\u7684\u6570\u636e\n{% endfilter %}<\/code><\/pre>\n<ul>\n<li>batch\uff1a\u5c06\u6570\u7ec4\u6309\u6307\u5b9a\u7684\u4e2a\u6570\u5206\u5272\u6210\u66f4\u5c0f\u7684\u6570\u7ec4\uff0c\u53ef\u9009\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7528\u6765\u5728\u5143\u7d20\u4e0d\u591f\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u586b\u5145\u3002\u5982 <code>{% raw %} {{ [1, 2, 3, 4, 5]|batch(2, &#039;NoItem&#039;) }}{% endraw %}<\/code> =&gt; [[1, 2], [3, 4], [5, &#8216;NoItem&#8217;]]<\/li>\n<li>date_modify\uff1a\u4fee\u6539\u65f6\u95f4\uff0c\u5e38\u4e0e date \u8054\u7528\u3002\u5982 <code>{% raw %} {{ birthday|date_modify(&#039;+3 days&#039;)|date(&#039;Y-m-d&#039;) }}{% endraw %}<\/code> =&gt; \u5c06\u5f53\u524d\u65f6\u95f4\u52a03\u5929\u540e\u663e\u793a<\/li>\n<li>default\uff1a\u5f53\u6240\u4fee\u9970\u7684\u6570\u636e\u4e0d\u5b58\u5728\u6216\u4e3a\u7a7a\u65f6\uff0c\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5982 {{ name|default(&#8216;Ruchee&#8217;) }} =&gt; &#8216;Ruchee&#8217;<\/li>\n<li>escape\uff1a\u5c06\u5b57\u7b26\u4e32\u5b89\u5168\u5730\u5904\u7406\u6210\u5408\u6cd5\u7684\u6307\u5b9a\u6570\u636e\uff0c\u53ef\u7b80\u5199\u4e3a e\uff0c\u652f\u6301\u591a\u79cd\u8f6c\u6362\u6a21\u5f0f\uff0c\u9ed8\u8ba4\u6a21\u5f0f\u4e3a html\uff0c\u5176\u4ed6\u53ef\u9009\u6a21\u5f0f\u6709 html_attr\u3001js\u3001css\u3001url<\/li>\n<li>first\uff1a\u8fd4\u56de\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u3002\u5982 <code>{% raw %} {{ {a: 1, b: 2, c: 3}|first }}{% endraw %}<\/code> =&gt; 1<\/li>\n<li>last\uff1a\u8fd4\u56de\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6216\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u5982 <code>{% raw %} {{ {a: 1, b: 2, c: 3}|last }}{% endraw %}<\/code> =&gt; 3<\/li>\n<li>replace\uff1a\u66ff\u6362\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9\u3002\u5982 <code>{% raw %} {{ &#039;%s1 love %s2&#039;|replace({&#039;%s1&#039;: &#039;Ruchee&#039;, &#039;%s2&#039;: &#039;Vim&#039;}) }}{% endraw %}<\/code> =&gt; &#8216;Ruchee love Vim&#8217;<\/li>\n<li>raw\uff1a\u8ba9\u6570\u636e\u5728 autoescape \u8fc7\u6ee4\u5668\u91cc\u5931\u6548<\/li>\n<\/ul>\n<h2>\u501f\u7528\u81eaPHP\u81ea\u5e26\u51fd\u6570\u7684\u8fc7\u6ee4\u5668<\/h2>\n<ul>\n<li>abs\uff1a\u53d6\u7edd\u5bf9\u503c<\/li>\n<li>nl2br\uff1a\u5c06\u5b57\u7b26\u4e32\u91cc\u7684 <code>\\n<\/code> \u66ff\u6362\u6210 <code>&lt;br\/&gt;<\/code><\/li>\n<li>join\uff1a\u5c06\u6570\u7ec4\u7684\u5404\u4e2a\u5143\u7d20\u6309\u6307\u5b9a\u5206\u9694\u7b26\u7ec4\u6210\u5b57\u7b26\u4e32<\/li>\n<li>sort\uff1a\u5bf9\u6570\u7ec4\u6392\u5e8f<\/li>\n<li>trim\uff1a\u53bb\u9664\u5b57\u7b26\u4e32\u9996\u5c3e\u7684\u6307\u5b9a\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c<\/li>\n<li>date\uff1a\u683c\u5f0f\u5316\u65f6\u95f4\uff0c\u53ef\u5904\u7406\u4e0e strtotime \u517c\u5bb9\u7684\u5b57\u7b26\u4e32\uff0c\u6216 DateTime\/DateInterval \u7684\u5b9e\u4f8b\uff0c\u53ef\u9009\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u65f6\u533a\uff0c\u5982\u679c\u6240\u4fee\u9970\u7684\u6570\u636e\u4e3a\u7a7a\u5219\u9ed8\u8ba4\u4e3a\u5f53\u524d\u65f6\u95f4<\/li>\n<li>reverse\uff1a\u53cd\u8f6c\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff0c\u5728 array_reverse \u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u5904\u7406<\/li>\n<li>slice\uff1a\u622a\u53d6\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff0c\u5728 array_slice \u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u5904\u7406<\/li>\n<li>keys\uff1a\u5c06\u6570\u7ec4\u7684\u5168\u90e8\u952e\u540d\u63d0\u53d6\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u7b49\u540c\u4e8e array_keys<\/li>\n<li>merge\uff1a\u5408\u5e76\u4e24\u6570\u7ec4\uff0c\u8fd1\u4f3c\u4e8e array_merge \u3002\u5982 <code>{% raw %} {{ \u6570\u7ec41|merge(\u6570\u7ec42) }}{% endraw %}<\/code><\/li>\n<li>length\uff1a\u8fd4\u56de\u6570\u7ec4\u5143\u7d20\u7684\u4e2a\u6570\u6216\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u7b49\u540c\u4e8e count \u548c strlen \u7684\u7ed3\u5408\u4f53<\/li>\n<li>capitalize\uff1a\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u7b49\u540c\u4e8e ucfirst<\/li>\n<li>title\uff1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u7b49\u540c\u4e8e ucwords<\/li>\n<li>lower\uff1a\u5c06\u5b57\u7b26\u4e32\u6240\u6709\u5b57\u6bcd\u5168\u90e8\u53d8\u6210\u5c0f\u5199\uff0c\u7b49\u540c\u4e8e strtolower<\/li>\n<li>upper\uff1a\u5c06\u5b57\u7b26\u4e32\u6240\u6709\u5b57\u6bcd\u5168\u90e8\u53d8\u6210\u5927\u5199\uff0c\u7b49\u540c\u4e8e strtoupper<\/li>\n<li>split\uff1a\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u6570\u7ec4\uff0c\u7b49\u540c\u4e8e str_split<\/li>\n<li>striptags\uff1a\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684 HTML\/PHP \u6807\u8bb0\uff0c\u7b49\u540c\u4e8e strip_tags<\/li>\n<li>url_encode\uff1a\u7f16\u7801\u94fe\u63a5\u5b57\u7b26\u4e32\uff0c\u7b49\u540c\u4e8e urlencode<\/li>\n<li>json_encode\uff1a\u7f16\u7801 JSON \u683c\u5f0f\uff0c\u7b49\u540c\u4e8e json_encode<\/li>\n<li>format\uff1a\u683c\u5f0f\u5316\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd1\u4f3c\u4e8e printf \u3002\u5982 <code>{% raw %} {{ &#039;My name is %s, and I love %s&#039;|format(&#039;Ruchee&#039;, &#039;Vim&#039;) }}{% endraw %}<\/code> =&gt; &#8216;My name is Ruchee, and I love Vim&#8217;<\/li>\n<li>number_format\uff1a\u683c\u5f0f\u5316\u6570\u503c\uff0c\u7b49\u540c\u4e8e number_format<\/li>\n<li>convert_encoding\uff1a\u7f16\u7801\u8f6c\u6362\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u8f6c\u6362\u540e\u7684\u7f16\u7801\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u8f6c\u6362\u524d\u7684\u7f16\u7801\uff0c\u8fd1\u4f3c\u4e8e iconv<\/li>\n<\/ul>\n<h2>\u5185\u5efa\u51fd\u6570<\/h2>\n<ul>\n<li>even\uff1a\u662f\u5426\u4e3a\u5076\u6570<\/li>\n<li>odd\uff1a\u662f\u5426\u4e3a\u5947\u6570<\/li>\n<li>empty\uff1a\u662f\u5426\u4e3a\u7a7a<\/li>\n<li>null\uff1a\u662f\u5426\u4e3a null<\/li>\n<li>defined\uff1a\u662f\u5426\u5df2\u5b9a\u4e49<\/li>\n<li>sameas\uff1a\u76ee\u6807\u53d8\u91cf\u4e0e\u6307\u5b9a\u503c\u662f\u5426\u6307\u5411\u7684\u662f\u5185\u5b58\u4e2d\u7684\u540c\u4e00\u4e2a\u5730\u5740\uff0c\u4f7f\u7528\u5f62\u5f0f if \u53d8\u91cf\u503c is sameas(\u6307\u5b9a\u503c)<\/li>\n<li>divisibleby\uff1a\u76ee\u6807\u6570\u503c\u662f\u5426\u80fd\u591f\u88ab\u6307\u5b9a\u503c\u6574\u9664\uff0c\u4f7f\u7528\u5f62\u5f0f if \u76ee\u6807\u6570\u503c divisibleby(\u6307\u5b9a\u503c)\uff0c\u5176\u4e2d\u6307\u5b9a\u503c\u4e0d\u80fd\u4e3a 0<\/li>\n<li>iterable\uff1a\u76ee\u6807\u53d8\u91cf\u662f\u5426\u662f\u6570\u7ec4\u6216\u8005\u662f\u5426\u53ef\u8fed\u4ee3\uff0c\u4f7f\u7528\u5f62\u5f0f if \u53d8\u91cf\u503c is iterable<\/li>\n<li>attribute\uff1a\u52a8\u6001\u83b7\u53d6\u53d8\u91cf\u5c5e\u6027\u503c\uff0c\u4e24\u79cd\u4f7f\u7528\u5f62\u5f0f\u4e3a <code>attribute(\u6570\u7ec4, &#039;\u5143\u7d20\u540d&#039;)<\/code> \u548c <code>attribute(\u5bf9\u8c61, &#039;\u65b9\u6cd5\u540d&#039;, \u53ef\u9009\u53c2\u6570)<\/code><\/li>\n<li>block\uff1a\u91cd\u590d\u5f15\u7528\u6307\u5b9a\u4ee3\u7801\u5757\uff0c\u5982 <code>{% raw %} {{ block(&#039;title&#039;) }}{% endraw %}<\/code><\/li>\n<li>constant\uff1a\u4ece\u5b57\u7b26\u4e32\u6216\u5bf9\u8c61\u53d6\u5f97\u5e38\u91cf\u503c<\/li>\n<li>cycle\uff1a\u5faa\u73af\u663e\u793a\u4e00\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u8c03\u7528\u5f62\u5f0f\u4e3a <code>cycle(\u6570\u7ec4, \u4e00\u4e2a\u5faa\u73af\u53d8\u91cf)<\/code><\/li>\n<li>date\uff1a\u683c\u5f0f\u5316\u65f6\u95f4<\/li>\n<li>dump\uff1a\u5728\u5f00\u542f\u8c03\u8bd5\u6a21\u5f0f\u7684\u60c5\u51b5\u4e0b\u663e\u793a\u8be6\u7ec6\u7684\u53d8\u91cf\u4fe1\u606f\uff0c\u7b49\u540c\u4e8e var_dump<\/li>\n<li>include\uff1a\u5305\u542b\u5176\u4ed6\u6a21\u677f\u6587\u4ef6<\/li>\n<li>parent\uff1a\u5728\u8986\u76d6\u4ee3\u7801\u7247\u6bb5\u65f6\u7528\u4e8e\u5f15\u7528\u7236\u7247\u6bb5\u7684\u5185\u5bb9<\/li>\n<li>random\uff1a\u5236\u9020\u4e00\u4e2a\u968f\u673a\u6570<\/li>\n<li>range\uff1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u533a\u95f4\u7684\u6570\u7ec4\uff0c\u53ef\u6307\u5b9a\u6b65\u957f\uff0cTwig \u4f7f\u7528 .. \u4f5c\u4e3a\u5176\u7b80\u7528\u6cd5\uff0c\u7b49\u540c\u4e8e range<\/li>\n<li>template_from_string\uff1a\u6839\u636e\u5b57\u7b26\u4e32\u52a0\u8f7d\u6a21\u677f<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Twig\u4e2d\u6709\u4e24\u79cd\u5b9a\u754c\u7b26 {% raw %} {% &#8230; %}{% endraw %} \u548c {% raw %} [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[174],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-opencart","tag-twig"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}