{"id":23644,"date":"2024-11-21T09:53:37","date_gmt":"2024-11-21T01:53:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23644\/"},"modified":"2024-11-21T09:53:37","modified_gmt":"2024-11-21T01:53:37","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8thinkphp6%e7%9a%84%e6%a8%a1%e6%9d%bf%e5%bc%95%e6%93%8e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23644\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u6a21\u677f\u5f15\u64ce"},"content":{"rendered":"<p>6\u662f\u76ee\u524d\u6bd4\u8f83\u6d41\u884c\u7684\u4e00\u4e2aphp\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u7279\u6027\u4ee5\u53ca\u5de5\u5177\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u5185\u7f6e\u7684\u6a21\u677f\u5f15\u64ce\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u3002<\/p>\n<p>\u4e00\u3001\u521b\u5efa\u6a21\u677f\u6587\u4ef6<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\u5939\uff0c\u8def\u5f84\u4e3a\uff1a\/application\/index\/view\/\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5939\u5b58\u653e\u6211\u4eec\u7684\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5728\u6a21\u677f\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaindex.html\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u4f5c\u4e3a\u6211\u4eec\u7684\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<p>\u4e8c\u3001\u6a21\u677f\u8bed\u6cd5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>ThinkPHP6\u4f7f\u7528\u4e86Twig\u6a21\u677f\u5f15\u64ce\uff0c\u5e76\u6dfb\u52a0\u4e86\u81ea\u5df1\u7684\u6269\u5c55\u529f\u80fd\u3002\u6211\u4eec\u6765\u5b66\u4e60\u5b83\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u8f93\u51fa\u53d8\u91cf<\/li>\n<\/ol>\n<p>\u4f7f\u7528{{}}\u8bed\u6cd5\u6765\u8f93\u51fa\u53d8\u91cf\u3002\u4f8b\u5982\uff1a{{title}}\u5c06\u8f93\u51fa\u53d8\u91cf$title\u7684\u503c\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u540d\u4e0d\u9700\u8981\u4f7f\u7528$\u7b26\u53f7\u3002<\/p>\n<ol>\n<li>if\u8bed\u53e5<\/li>\n<\/ol>\n<p>if\u8bed\u53e5\u4f7f\u7528{% if condition %} &#8230; {% endif %}\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>{% if isLogin %}\n  &lt;a href=\"#\"&gt;\u9000\u51fa\u767b\u5f55&lt;\/a&gt;\n{% else %}\n  &lt;a href=\"#\"&gt;\u767b\u5f55&lt;\/a&gt;\n{% endif %}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>foreach\u8bed\u53e5<\/li>\n<\/ol>\n<p>foreach\u8bed\u53e5\u4f7f\u7528{% for key, value in array %} &#8230; {% endfor %}\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>{% for article in articles %}\n  &lt;div class=\"article\"&gt;\n    &lt;h2&gt;{{article.title}}&lt;\/h2&gt;\n    &lt;p&gt;{{article.content}}&lt;\/p&gt;\n  &lt;\/div&gt;\n{% endfor %}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>include\u8bed\u53e5<\/li>\n<\/ol>\n<p>include\u8bed\u53e5\u53ef\u4ee5\u5f15\u5165\u5176\u4ed6\u6a21\u677f\u6587\u4ef6\uff0c\u4f7f\u7528{% include &#8220;file.html&#8221; %}\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>{% include \"header.html\" %}\n\n&lt;div class=\"content\"&gt;\n  ...\n&lt;\/div&gt;\n\n{% include \"footer.html\" %}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u6a21\u677f<\/p>\n<p>\u6211\u4eec\u9700\u8981\u5728\u63a7\u5236\u5668\u4e2d\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u6a21\u677f\u5f15\u64ce\uff0c\u7136\u540e\u518d\u6e32\u67d3\u6a21\u677f\u3002<\/p>\n<p>\u5728\u63a7\u5236\u5668\u4e2d\u52a0\u8f7d\u6a21\u677f\u5f15\u64ce\u5e76\u6e32\u67d3\u6a21\u677f\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appindexcontroller;\n\nuse thinkController;\n\nclass Index extends Controller\n{\n  public function index()\n  {\n    $this-&gt;assign('title', 'Welcome to my blog');\n    $this-&amp;gt;assign('isLogin', true);\n    $this-&amp;gt;assign('articles', [\n      ['title' =&amp;gt; 'article 1', 'content' =&amp;gt; 'something'],\n      ['title' =&amp;gt; 'article 2', 'content' =&amp;gt; 'something else']\n    ]);\n    return $this-&amp;gt;fetch('index');\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cassign\u65b9\u6cd5\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u6a21\u677f\u5f15\u64ce\u3002title\u3001isLogin\u548carticles\u662f\u6211\u4eec\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u540d\u3002<\/p>\n<p>fetch\u65b9\u6cd5\u7528\u4e8e\u6e32\u67d3\u6a21\u677f\u6587\u4ef6\uff0c\u5b83\u7684\u53c2\u6570\u662f\u6a21\u677f\u6587\u4ef6\u540d\uff0c\u5373index.html\u3002<\/p>\n<p>\u56db\u3001\u7ed3\u8bed<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u6a21\u677f\u5f15\u64ce\u8ba9\u6211\u4eec\u66f4\u52a0\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u4ee5\u9875\u9762\u7684\u5f62\u5f0f\u5c55\u73b0\u51fa\u6765\uff0c\u540c\u65f6\u4e5f\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5feb\u6765\u52a8\u624b\u5c1d\u8bd5\u5427\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u6a21\u677f\u5f15\u64ce\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>6\u662f\u76ee\u524d\u6bd4\u8f83\u6d41\u884c\u7684\u4e00\u4e2aphp\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u7279\u6027\u4ee5\u53ca\u5de5\u5177\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u5185\u7f6e\u7684\u6a21\u677f\u5f15\u64ce\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u3002 \u4e00\u3001\u521b\u5efa\u6a21\u677f\u6587\u4ef6 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\u5939\uff0c\u8def\u5f84\u4e3a\uff1a\/application\/index\/view\/\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5939\u5b58\u653e\u6211\u4eec\u7684\u6a21\u677f\u6587\u4ef6\u3002 \u63a5\u4e0b\u6765\u5728\u6a21\u677f\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaindex.html\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u4f5c\u4e3a\u6211\u4eec\u7684\u6a21\u677f\u6587\u4ef6\u3002 \u4e8c\u3001\u6a21\u677f\u8bed\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ThinkPHP6\u4f7f\u7528\u4e86Twig\u6a21\u677f\u5f15\u64ce\uff0c\u5e76\u6dfb\u52a0\u4e86\u81ea\u5df1\u7684\u6269\u5c55\u529f\u80fd\u3002\u6211\u4eec\u6765\u5b66\u4e60\u5b83\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002 \u8f93\u51fa\u53d8\u91cf \u4f7f\u7528{{}}\u8bed\u6cd5\u6765\u8f93\u51fa\u53d8\u91cf\u3002\u4f8b\u5982\uff1a{{title}}\u5c06\u8f93\u51fa\u53d8\u91cf$title\u7684\u503c\u3002\u6ce8\u610f\uff0c\u53d8\u91cf\u540d\u4e0d\u9700\u8981\u4f7f\u7528$\u7b26\u53f7\u3002 if\u8bed\u53e5 if\u8bed\u53e5\u4f7f\u7528{% if condition %} &#8230; {% endif %}\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a {% if isLogin %} &lt;a href=&#8221;#&#8221;&gt;\u9000\u51fa\u767b\u5f55&lt;\/a&gt; {% else %} &lt;a href=&#8221;#&#8221;&gt;\u767b\u5f55&lt;\/a&gt; {% endif %} \u767b\u5f55\u540e\u590d\u5236 foreach\u8bed\u53e5 foreach\u8bed\u53e5\u4f7f\u7528{% for key, value in array %} &#8230; {% endfor %}\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a {% for article in articles %} &lt;div class=&#8221;article&#8221;&gt; &lt;h2&gt;{{article.title}}&lt;\/h2&gt; &lt;p&gt;{{article.content}}&lt;\/p&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23644","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23644"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23644\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}