{"id":82,"date":"2021-07-30T15:35:50","date_gmt":"2021-07-30T07:35:50","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=82"},"modified":"2024-07-08T12:54:20","modified_gmt":"2024-07-08T04:54:20","slug":"smarty%e6%a8%a1%e6%9d%bf%e4%b8%ad%e6%a0%87%e7%ad%be%e7%9a%84%e8%be%93%e5%87%ba%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/07\/30\/smarty%e6%a8%a1%e6%9d%bf%e4%b8%ad%e6%a0%87%e7%ad%be%e7%9a%84%e8%be%93%e5%87%ba%e6%96%b9%e5%bc%8f\/","title":{"rendered":"\u3010php\u3011smarty\u6a21\u677f\u4e2d\u6807\u7b7e\u7684\u8f93\u51fa\u65b9\u5f0f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">smarty\u6a21\u677f\u4e2d\u7684\u6807\u7b7e\u6709\u4ee5\u4e0b\u8f93\u51fa\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2460 \u539f\u6837\u8f93\u51fa\uff0c\u5373&amp;\u6807\u7b7e\u540d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2461 \u6807\u7b7e\u53c2\u4e0e\u6570\u5b66\u8fd0\u7b97\uff08\u4e0d\u63a8\u8350\uff0c\u6a21\u677f\u53ea\u4fdd\u6301\u8f93\u51fa\u5c31\u884c\u4e86\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>{$age+30}&lt;\/p>\n&lt;p>\u8001\u5934\u7684\u771f\u5b9e\u5e74\u9f84\u662f{$age},\u4ed6\u627e\u8001\u5a46\u7684\u6700\u9002\u5408\u5e74\u9f84\u662f{$age-$sub}\u5c81&lt;\/p>\n&lt;p>{30+$age} \u8fd9\u79cd\u5199\u6cd5\u5728smarty2.6\u91cc\uff0c\u662f\u4e0d\u652f\u6301\u7684\uff0c\u5373\u8fd0\u7b97\u7684\u65f6\u5019\uff0c$\u6807\u7b7e\u8981\u653e\u5728\u6700\u524d\u9762&lt;br \/>\n            \u5efa\u8bae\u4e0d\u8981\u8fd9\u6837\u5199\uff0c\u4e3a\u4e86\u517c\u5bb9\u5c31\u7248\u672c\u7684smarty\n&lt;\/p><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> \u2462 \u6807\u7b7e\u53ef\u4ee5\u8fdb\u884c if else&nbsp; \u5224\u65ad\uff08\u4ece\u5206\u5de5\u89d2\u5ea6\u770b\uff0c\u6a21\u677f\u4e0d\u5e94\u7528\u8d1f\u8d23\u903b\u8f91\u5224\u65ad\uff0c\u6709\u65f6\uff0c\u5728\u6a21\u677f\u4e0a\uff0c\u8fdb\u884c\u903b\u8f91\u5224\u65ad\uff0c\u53ef\u4ee5\u6781\u5927\u7684\u7b80\u5316\u5de5\u4f5c\uff0c\u6bd4\u5982\uff1a\u4f1a\u5458\u666e\u901a\u4f1a\u5458\uff0cVIP\u4f1a\u5458\uff0c\u5728\u9875\u9762\u53f3\u4e0a\u89d2\uff0c\u663e\u793a\u4e0d\u540c\u7684\u63d0\uff09 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>\n            \u4ece\u5730\u5740\u680f\u4e0a\u4f20\u503c\uff0c\u5224\u65ad\u662f\u661f\u671f\u51e0?\n            \u5982\u679c\u662f0,7\uff0c\u5219\u4e3a\u5468\u65e5\uff0c\u5982\u679c\u662f6\uff0c\u5219\u4e3a\u5468\u516d\uff0c\u5176\u4f59\u5219\u4e3a\u5de5\u4f5c\u65e5\n        &lt;\/p>\n        &lt;p>\n            {if $smarty.get.today == 0 || $smarty.get.today == 7}\n             \u4eca\u5929\u662f\u5468\u65e5\n            {else if $smarty.get.today == 6} \n             \u4eca\u5929\u662f\u5468\u516d\n            {else}\n             \u4eca\u5929\u662f\u5de5\u4f5c\u65e5\uff0c\u4e0a\u73ed\u53bb\u5427\n            {\/if}\n        &lt;\/p><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\n\n\u2463 \u6807\u7b7e\u53ef\u4ee5\u7528\u4e8e\u63a7\u5236\u5faa\u73af\u8bed\u53e5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp; for&nbsp; \uff0c&nbsp; while(smarty3\u65b0\u589e)&nbsp; \uff0c&nbsp; foreach&nbsp; \uff0c&nbsp; section<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$smarty-&gt;assign(&#8216;start&#8217;,1);<br> $smarty-&gt;assign(&#8216;end&#8217;,10);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;pre>\u63a5\u4e0b\u6765\u6253\u5370\uff0c1\u5230100\u4e4b\u95f4\u7684\u6240\u6709\u5947\u6570:\n            \u5e38\u89c4\u60f3\u6cd5\uff1a\u662f\u7528%2 == 1 \uff0c\u6765\u5224\u65ad\u5e76\u6253\u5370\n            \u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u5229\u7528for\u5faa\u73af\u7684'\u6b65\u957f'\u5c5e\u6027\u6765\u63a7\u5236\n            \u6b65\u957f\u5373\uff0c\u7531\u5c0f\u5230\u5927\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u6b65\u7684\u589e\u957f\"\u5e45\u5ea6\",\n            1,3,5,7\uff0c\u8fd9\u5c31\u6bcf\u4e00\u6b65\u9012\u589e2\n        &lt;\/pre>\n \n        &lt;p>\n            {for $i=$start to 100 step 2}\n                {$i}  {if $i@iteration % 3 == 0}&lt;br \/> {\/if}\n            {\/for}\n            &lt;br \/>\n            &lt;h2>$i@iteration\u8868\u793a\u5f53\u524d\u5faa\u73af\u5355\u5143\uff0c\u662f\u6574\u4f53\u5faa\u73af\u4f53\u4e2d\u7684\u7b2c\u51e0\u6b21\u5faa\u73af&lt;\/h2>\n        &lt;\/p>\n\n&lt;pre>\n    \u7528@first\u63a7\u5236\u9996\u884c\n    @first\u662f1\u4e2a\u5e03\u5c14\u503c\uff0c\u5f53\u7b2c\u4e00\u6b21\u5faa\u73af\u65f6\uff0c\u5176\u503c\u4e3a\u771f\uff0c\u5426\u5219\u5176\u503c\u4e3a\u5047\n    @last\u4e5f\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5f53\u6b64\u6b21\u5faa\u73af\u7684\u6b21\u6570\u7b49\u4e8e\u603b\u5faa\u73af\u6b21\u6570\u7684\u65f6\u5019\uff0c\u5176\u503c\u4e3a\u771f\uff0c\u5426\u5219\u5176\u503c\u4e3a\u5047\n    &lt;\/pre>\n    &lt;table border=\"1px\">\n        &lt;tr>&lt;td>\u5e8f\u53f7&lt;\/td>&lt;td>\u5546\u54c1\u540d&lt;\/td>&lt;td>\u5546\u54c1\u4ef7\u683c&lt;\/td>&lt;\/tr>\n        {foreach $goods as $k=>$g}\n        &lt;tr {if $g@first} bgcolor=\"blue\" {else if $g@last} bgcolor=\"green\" {\/if}>\n        &lt;td>{$g@iteration}&lt;\/td>&lt;td>{$g.goods_name}&lt;\/td>&lt;td>{$g.shop_price}&lt;\/td>&lt;\/tr>\n        {\/foreach}\n    &lt;\/table>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> foreach\u5faa\u73af\u8fd8\u6709smarty2\u7684\u7ecf\u5178\u5199\u6cd5\uff0c\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{literal}\n    foreach\u5faa\u73af\u6570\u7ec4\u7684\u5178\u578b\u5199\u6cd5\n    {foreach from=$source key=$key $item=$item}\n    {\/foreach}\n \n    \u5982\u4f55\u6765\u7406\u89e3foreach\u5462?\n    \u7b54\uff1afrom\u662f\u6765\u81ea\u7684\u610f\u601d\uff0c\u5373\uff0c\u5faa\u73af$source\u8fd9\u4e2a\u53d8\u91cf\n \n    \u4ece\u5934\u5230\u5c3e\uff0c\u6328\u4e2a\u7684\u628a$source\u7684\u6bcf\u4e2a\u5355\u5143\u7684\"\u952e\"\u548c\"\u503c\"\u62ff\u51fa\u6765\n    \u5206\u522b\u8d4b\u7ed9 $key,$item\n \n    \u56e0\u6b64\uff0c\u5faa\u73af\u8fc7\u7a0b\u4e2d\uff0c$key,$item\u7684\u503c\uff0c\u4e0d\u65ad\u53d8\u5316\uff0c\u76f4\u5230\u5faa\u73af\u7ed3\u679c\n{\/literal}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> section\u5199\u6cd5\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{literal}\n \n    &lt;h2>section&lt;\/h2>\n \n    &lt;pre>\n    \u4e00\u822csection\u8fd9\u4e48\u5199{section loop=$source name=g}\n    loop\u662f\u5f85\u5faa\u73af\u7684\u6570\u7ec4\n \n    name=g,\u4ee3\u8868\u6bcf\u4e00\u6b21\u5faa\u73af\uff0cg=0 1 2 3  ....\n    \n    \u5982\u679c\u6570\u7ec4\u4e5f\u662f\u6807\u7b7e\u7684\u7d22\u5f15\u6570\u7ec4\uff0c\u6070\u597d\u5c31\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u952e\n \n    \u56e0\u6b64\u624b\u518c\u4e0a\u8bf4\uff0csection\u7528\u4e8e\u5faa\u73af\u4ece0\u5f00\u59cb\u7684\u7d22\u5f15\u6570\u7ec4\n \n    \u663e\u793a\u65f6\uff0c$smarty[g].\u952e\u540d\n    &lt;\/pre>\n    \n    {\/literal}\n    &lt;h2>\u5e03\u5c14\u5546\u573a\u5546\u54c1\u5217\u8868&lt;\/h2>\n    &lt;table border=\"1px\">\n        &lt;tr>&lt;td>\u5e8f\u53f7&lt;\/td>&lt;td>\u5546\u54c1\u540d&lt;\/td>&lt;td>\u5546\u54c1\u4ef7\u683c&lt;\/td>&lt;\/tr>\n        {section loop=$goods name=g}\n        &lt;tr>&lt;td>{$goods[g].goods_id}&lt;\/td>&lt;td>{$goods[g].goods_name}&lt;\/td>&lt;td>{$goods[g].shop_price}&lt;\/td>&lt;\/tr>\n        {\/section}\n    &lt;\/table><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> \u201c\u6ce8\u201d\uff1a{foreach}\u53ef\u4ee5\u505a\u5230{section}\u505a\u5230\u7684\u529f\u80fd\uff0c\u800c\u4e14\u66f4\u7b80\u5355\u66f4\u6e05\u6670\u7684\u8bed\u6cd5\u3002\u4e00\u822c\u66f4\u63a8\u8350\u4f7f\u7528{foreach}\u8bed\u6cd5 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2464 \u53d8\u91cf\u8c03\u8282\u5668\uff0c\u901a\u8fc7\u53d8\u91cf\u8c03\u8282\u5668\uff0c\u6765\u5bf9\u53d8\u91cf\u7684\u503c\u8fdb\u884c\u5fae\u8c03\uff0c\u6bd4\u5982:\u628a\u65f6\u95f4\u6233\u5fae\u8c03\u6210 \u65e5\u671f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \u53d8\u91cf\u8c03\u8282\u5668\u7684\u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570\n \u8fd9\u4e2a\u51fd\u6570\uff0c\u4ee5\u6807\u7b7e\u5bf9\u5e94\u7684\u53d8\u91cf\u503c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8fd0\u7b97\uff0c\u628a\u8fd4\u56de\u503c\uff0c\u663e\u793a\u5728\u6807\u7b7e\u5904\n\n \u5982\uff1a{$g.add_time}\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65f6\u95f4\u6233\n \u5fae\u8c03\u6210\u65e5\u671f\n {$g.add_time|date_format:\"%Y-%m-$d %H:%M:%S\"}\n \u53d8\u6210\u5e74\u6708\u65e5 \u65f6:\u5206:\u79d2\u8fd9\u79cd\u683c\u5f0f<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h2>{$poem|upper}&lt;\/h2>\n    &lt;h2>{$poem|lower}&lt;\/h2>\n    &lt;h2>{$poem|upper|lower}&lt;\/h2><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>smarty\u6a21\u677f\u4e2d\u7684\u6807\u7b7e\u6709\u4ee5\u4e0b\u8f93\u51fa\u65b9\u5f0f\uff1a \u2460 \u539f\u6837\u8f93\u51fa\uff0c\u5373&#038;&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":1221,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/82\/revisions\/1221"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}