{"id":1674,"date":"2020-10-05T04:31:39","date_gmt":"2020-10-04T20:31:39","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1674"},"modified":"2022-02-16T21:53:32","modified_gmt":"2022-02-16T13:53:32","slug":"iris-mvc-view-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/05\/iris-mvc-view-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris mvc\u7684view"},"content":{"rendered":"<h1>view<\/h1>\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u5927\u5bb6\u5982\u4f55\u5728 iris \u6846\u67b6\u4e0b\u4f7f\u7528 MVC \u88fd\u4f5c\u4e00\u500b\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u4f46\u662f\u5c0d\u65bc<code>view<\/code>\u9084\u6c92\u6709\u4efb\u4f55\u8aaa\u660e\uff0c\u6240\u4ee5\u672c\u7bc7\u5c07\u70ba<code>view<\/code>\u505a\u9032\u4e00\u6b65\u7684\u8aaa\u660e\u3002<\/p>\n<p><!--more--><\/p>\n<h2>iris MVC \u7684 view<\/h2>\n<p>\u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e86\u5982\u4f55\u64b0\u5beb\u3001\u5be6\u4f5c\u4ee5\u53ca\u7d81\u5b9a<code>controller<\/code>\u4e26\u4e14\u4ecb\u7d39<code>action<\/code>\u8207url\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u5982\u679c\u662f\u64b0\u5bebapi\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u61c9\u8a72\u662f\u582a\u7528\uff0c\u4f46\u662f\u5982\u679c\u8981\u958b\u767cserver side rendering\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u5c31\u7a0d\u5acc\u4e0d\u8db3\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528iris MVC\u7684view\u3002<\/p>\n<h3>view \u7684\u7d81\u5b9a<\/h3>\n<p>\u7531\u65bc\u5728<code>controller<\/code>\u8655\u7406response\u6642\u5728<code>controller<\/code>\u7d44html\u8a9e\u6cd5\u4e00\u4f86\u4e0d\u76f4\u89ba\uff0c\u4e8c\u4f86\u6b0a\u8cac\u6c92\u6709\u5206\u96e2\u958b\u4f86\u6703\u5c0e\u81f4\u958b\u767c\u4e0a\u9047\u5230\u554f\u984c\uff0c\u56e0\u6b64\u9019\u88e1\u4ecb\u7d39\u5982\u4f55\u7d81\u5b9a<code>template<\/code>\u4ee5\u53ca<code>template<\/code>\u7684\u64b0\u5beb\u3002<br \/>\n\u5728 iris MVC\u7684<code>template<\/code>\u652f\u63f4\u4ee5\u4e0b\u5e7e\u7a2e<\/p>\n<ul>\n<li>HTML  html\/template<\/li>\n<li>Blocks    kataras\/blocks<\/li>\n<li>Django    flosch\/pongo2<\/li>\n<li>Pug   Joker\/jade<\/li>\n<li>Handlebars    aymerick\/raymond<\/li>\n<li>Amber eknkc\/amber<\/li>\n<li>Jet   CloudyKit\/jet<\/li>\n<li>Ace   yosssi\/ace<\/li>\n<\/ul>\n<p>\u9019\u88e1\u4ee5<code>html\/template<\/code>\u70ba\u4f8b\u5b50\u4f86\u8aaa\u660e\uff0c\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">app.RegisterView(iris.HTML(&quot;.\/views&quot;, &quot;.html&quot;))<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u4ecb\u7d39\u5982\u4f55\u8f09\u5165\u5148\u5beb\u597d\u7684<code>template<\/code>\uff0c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39\u5982\u4f55\u7d81\u5b9a\u5c0d\u6620\u7684<code>view<\/code>\u3002<\/p>\n<h3>view \u7684\u7bc4\u672c\u7684\u8a9e\u6cd5<\/h3>\n<p>\u5728\u4ecb\u7d39\u5b8c\u8f09\u5165<code>template<\/code>\u7684\u65b9\u6cd5\u5f8c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39\u5982\u4f55\u64b0\u5beb<code>template<\/code>\u4ee5\u53ca\u5982\u4f55\u5728<code>controller<\/code>\u7d81\u5b9a\u5c0d\u61c9\u7684<code>view<\/code><br \/>\n\u9996\u5148\u770b\u770b\u4ee5\u4e0b\u4f8b\u5b50<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;head&gt;\n    &lt;title&gt;{{.Title}}&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n{{ yield }}\n&lt;footer&gt;\n{{ render &quot;partials\/footer.html&quot; }}\n&lt;\/footer&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u9019\u662f\u64f7\u53d6\u65bc<\/p>\n<pre><code class=\"language-bash\">\/view\/layout\/html\/views\/layouts\/main.html<\/code><\/pre>\n<p>\u7684\u7a0b\u5f0f\uff0c\u64b0\u5beb\u8981\u6e32\u67d3\u7684\u5167\u5bb9\uff0c\u5176\u4e2d\u6709\u500b\u95dc\u9375\u5b57<code>render<\/code>\u9019\u6bb5\u662f\u547c\u53eb<code>partial view<\/code>\u5176\u5167\u5bb9\u5982\u4e0b\u6240\u5217<\/p>\n<pre><code class=\"language-html\">&lt;h3&gt;Footer Partial&lt;\/h3&gt;\n&lt;h4&gt;{{.FooterText}}&lt;\/h4&gt;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u5167\u5bb9\u653e\u7f6e\u65bc<\/p>\n<pre><code class=\"language-bash\">\/view\/layout\/html\/views\/partials\/footer.html<\/code><\/pre>\n<p>\u5728\u6e96\u5099\u597d<code>template<\/code>\u4e4b\u5f8c\u5c31\u9700\u8981\u5728<code>controller<\/code>\u547c\u53eb\u5c31\u53ef\u4ee5\u4e86\uff0c\u8a73\u60c5\u8acb\u770b\u4e0b\u5217\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-go\">func (c *BookController) getIndex(ctx iris.Context) {\n    data := iris.Map{\n    &quot;Title&quot;:      &quot;Page Title&quot;,\n    &quot;FooterText&quot;: &quot;Footer contents&quot;,\n    &quot;Message&quot;:    &quot;Main contents&quot;,\n    }\n\n    ctx.ViewLayout(&quot;layouts\/main&quot;)\n    ctx.View(&quot;index&quot;, data)\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u547c\u53eb\u6211\u5011\u6700\u5148\u64b0\u5beb\u7684main.html\u4e26\u4e14\u5c07\u5c0d\u61c9\u7684\u8cc7\u6599\u7d50\u69cbdata\u50b3\u5165\uff0c\u800c<code>template<\/code>\u4e2d\u7684<code class=\"kb-btn\">{}<\/code>\u5247\u6703\u6e32\u67d3\u6210data\u5167\u5c0d\u61c9\u7684\u8b8a\u6578\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u9019\u62fc\u6587\u7ae0\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528MVC\u7684view\uff0c\u4e26\u4ecb\u7d39\u5982\u4f55\u7d81\u5b9a\u4ee5\u53ca\u64b0\u5beb<code>template<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>view \u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u5927\u5bb6\u5982\u4f55\u5728 iris \u6846\u67b6\u4e0b\u4f7f\u7528 MVC \u88fd\u4f5c\u4e00\u500b\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u4f46\u662f\u5c0d\u65bcview\u9084\u6c92\u6709 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/05\/iris-mvc-view-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris mvc\u7684view\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84,86],"class_list":["post-1674","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris","tag-mvc"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1674"}],"version-history":[{"count":15,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1674\/revisions"}],"predecessor-version":[{"id":2959,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1674\/revisions\/2959"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}