{"id":261,"date":"2011-08-12T16:08:41","date_gmt":"2011-08-12T08:08:41","guid":{"rendered":"http:\/\/www.cnmiss.cn\/?p=261"},"modified":"2011-08-12T16:15:47","modified_gmt":"2011-08-12T08:15:47","slug":"codeigniter-2-02-%e4%b8%ad%e9%85%8d%e7%bd%ae-smarty","status":"publish","type":"post","link":"https:\/\/www.cnmiss.cn\/?p=261","title":{"rendered":"CodeIgniter 2.02 \u4e2d\u914d\u7f6e smarty"},"content":{"rendered":"<p>1\u3001\u5230\u76f8\u5e94\u7ad9\u70b9\u4e0b\u8f7dSmarty\u7684\u6e90\u7801\u5305\uff1b<br \/>\n2\u3001\u5c06\u6e90\u7801\u5305\u91cc\u9762\u7684libs\u6587\u4ef6\u5939copy\u5230CI\u7684\u9879\u76ee\u76ee\u5f55\u4e0b\u9762\u7684libraries\u6587\u4ef6\u5939\u4e0b\uff0c\u5e76\u91cd\u547d\u540d\u4e3aSmarty\uff1b<br \/>\n3\u3001\u5728\u9879\u76ee\u76ee\u5f55\u7684libraries\u6587\u4ef6\u5939\u5185\u65b0\u5efa\u6587\u4ef6Cismarty.php\uff0c\u91cc\u9762\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n&lt;?php<br \/>\nif(!defined(&#8216;BASEPATH&#8217;)) EXIT(&#8216;No direct script asscess allowed&#8217;);<\/p>\n<p>require_once( APPPATH . &#8216;libraries\/Smarty\/Smarty.class.php&#8217; );<\/p>\n<p>class Cismarty extends Smarty {<\/p>\n<p>protected $ci;<\/p>\n<p>public function\u00a0 __construct(){<\/p>\n<p>$this-&gt;ci = &amp; get_instance();<\/p>\n<p>$this-&gt;ci-&gt;load-&gt;config(&#8216;smarty&#8217;);\/\/\u52a0\u8f7dsmarty\u7684\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>\/\/\u83b7\u53d6\u76f8\u5173\u7684\u914d\u7f6e\u9879<br \/>\n$this-&gt;template_dir\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;template_dir&#8217;);<br \/>\n$this-&gt;complie_dir\u00a0\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;compile_dir&#8217;);<br \/>\n$this-&gt;cache_dir\u00a0\u00a0\u00a0\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;cache_dir&#8217;);<br \/>\n$this-&gt;config_dir\u00a0\u00a0\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;config_dir&#8217;);<br \/>\n$this-&gt;template_ext\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;template_ext&#8217;);<br \/>\n$this-&gt;caching\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = $this-&gt;ci-&gt;config-&gt;item(&#8216;caching&#8217;);<br \/>\n$this-&gt;cache_lifetime = $this-&gt;ci-&gt;config-&gt;item(&#8216;lefttime&#8217;);<br \/>\n}<\/p>\n<p>}\n<\/p><\/blockquote>\n<p>4\u3001\u5728\u9879\u76ee\u76ee\u5f55\u7684config\u6587\u4ef6\u5939\u5185\u65b0\u5efa\u6587\u4ef6smarty.php\u6587\u4ef6,\u91cc\u9762\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n&lt;?php\u00a0 if ( ! defined(&#8216;BASEPATH&#8217;)) exit(&#8216;No direct script access allowed&#8217;);<br \/>\n$config[&#8216;theme&#8217;]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8216;default&#8217;;<br \/>\n$config[&#8216;template_dir&#8217;] = APPPATH . &#8216;views&#8217;;<br \/>\n$config[&#8216;compile_dir&#8217;]\u00a0 = FCPATH . &#8216;templates_c&#8217;;<br \/>\n$config[&#8216;cache_dir&#8217;]\u00a0\u00a0\u00a0 = FCPATH . &#8216;cache&#8217;;<br \/>\n$config[&#8216;config_dir&#8217;]\u00a0\u00a0 = FCPATH . &#8216;configs&#8217;;<br \/>\n$config[&#8216;template_ext&#8217;] = &#8216;.html&#8217;;<br \/>\n$config[&#8216;caching&#8217;]\u00a0\u00a0\u00a0\u00a0\u00a0 = false;<br \/>\n$config[&#8216;lefttime&#8217;]\u00a0\u00a0\u00a0\u00a0 = 60;\n<\/p><\/blockquote>\n<p>5\u3001\u5728\u5165\u53e3\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u65b0\u5efa\u6587\u4ef6\u5939templates_c\u3001cache\u3001configs\uff1b<\/p>\n<p>6\u3001\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u9762\u7684config\u76ee\u5f55\u4e2d\u627e\u5230autoload.php\u6587\u4ef6<br \/>\n\u4fee\u6539\u8fd9\u9879<\/p>\n<blockquote><p>\n$autoload[&#8216;libraries&#8217;] = array(&#8216;Cismarty&#8217;);\/\/\u76ee\u7684\u662f\uff1a\u8ba9\u7cfb\u7edf\u8fd0\u884c\u65f6\uff0c\u81ea\u52a8\u52a0\u8f7d\uff0c\u4e0d\u7528\u8ba4\u4e3a\u7684\u5728\u63a7\u5236\u5668\u4e2d\u624b\u52a8\u52a0\u8f7d\n<\/p><\/blockquote>\n<p>7\u3001\u5728\u9879\u76ee\u76ee\u5f55\u7684core\u6587\u4ef6\u5939\u4e2d\u65b0\u5efa\u6587\u4ef6MY_Controller.php \u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\n&lt;?php if (!defined(&#8216;BASEPATH&#8217;)) exit(&#8216;No direct access allowed.&#8217;);<\/p>\n<p>class Controller extends CI_Controller {<\/p>\n<p>public function __construct() {<\/p>\n<p>parent::__construct();<\/p>\n<p>}<\/p>\n<p>public function assign($key,$val) {<br \/>\n$this-&gt;cismarty-&gt;assign($key,$val);<br \/>\n}<\/p>\n<p>public function display($html) {<br \/>\n$this-&gt;cismarty-&gt;display($html);<br \/>\n}<br \/>\n}\n<\/p><\/blockquote>\n<p>\/\/================================================================<br \/>\n\u914d\u7f6e\u5b8c\u6bd5<br \/>\n\/\/================================================================<br \/>\n\u4f7f\u7528\u65b9\u6cd5\uff1a<br \/>\n\u5728\u63a7\u5236\u5668\u4e2d\u5982\uff1a<\/p>\n<blockquote><p>\n&lt;?php if ( ! defined(&#8216;BASEPATH&#8217;)) exit(&#8216;No direct script access allowed&#8217;);<\/p>\n<p>class Welcome extends Controller {<\/p>\n<p>public function __construct(){<br \/>\nparent::__construct();<br \/>\n}<\/p>\n<p>public function index()<br \/>\n{<br \/>\n$data[&#8216;title&#8217;] = &#8216;\u6d4b\u8bd5&#8217;;<br \/>\n$data[&#8216;test&#8217;] = &#8216;123456789&#8217;;<br \/>\n$this-&gt;assign(&#8216;test&#8217;,$data);<br \/>\n$this-&gt;assign(&#8216;tmp&#8217;,&#8217;hello&#8217;);<br \/>\n$this-&gt;display(&#8216;test.html&#8217;);<br \/>\n}<br \/>\n}\n<\/p><\/blockquote>\n<p>\u7136\u540e\u518d\u89c6\u56fe\u4e2d\uff1a\u8bd5\u56fe\u6587\u4ef6\u5939\u4f4d\u4e8e\u9879\u76ee\u76ee\u5f55\u7684views\u4e4b\u4e0b\uff1a<br \/>\n\u65b0\u5efa\u6587\u4ef6test.html<\/p>\n<blockquote><p>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta charset=&#8221;utf-8&#8243;&gt;<br \/>\n&lt;title&gt;{$test[&#8216;title&#8217;]}&lt;\/title&gt;<\/p>\n<p>&lt;style type=&#8221;text\/css&#8221;&gt;<br \/>\n&lt;\/style&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<\/p>\n<p>{$test[&#8216;test&#8217;]|md5}<br \/>\n&lt;br&gt;<br \/>\n{$tmp}<br \/>\n123<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5230\u76f8\u5e94\u7ad9\u70b9\u4e0b\u8f7dSmarty\u7684\u6e90\u7801\u5305\uff1b 2\u3001\u5c06\u6e90\u7801\u5305\u91cc\u9762\u7684libs\u6587\u4ef6\u5939cop &hellip; <a href=\"https:\/\/www.cnmiss.cn\/?p=261\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-xxbj","tag-codeigniter"],"_links":{"self":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":0,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}