{"id":878,"date":"2012-09-07T22:30:31","date_gmt":"2012-09-07T14:30:31","guid":{"rendered":"http:\/\/www.poloo.org\/?p=878"},"modified":"2012-09-07T22:30:31","modified_gmt":"2012-09-07T14:30:31","slug":"codeigniter-%e4%b8%ad%e9%9b%86%e6%88%90smarty","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=878","title":{"rendered":"Codeigniter \u4e2d\u96c6\u6210smarty"},"content":{"rendered":"<p>Ci\u662f\u500b\u4e0d\u932f\u7684\u6846\u67b6\uff0cSmarty\u662f\u4e00\u500b\u4e0d\u932f\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u7f51\u4e0a\u770b\u4e86\u5f88\u591a\uff0c\u4e5f\u4e0d\u77e5\u9053\u662f\u7248\u672c\u95ee\u9898\u8fd8\u662f\u4ec0\u4e48\uff0c \u603b\u662f\u914d\u7f6e\u4e0d\u6210\u529f\uff0c\u591a\u6b21\u8bd5\u9a8c\u7ec8\u4e8eOK\u3002\u5c0d\u65bc\u7ed3\u5408Smarty\u7684\u914d\u7f6e\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1. \u7b2c\u4e00\u6b65\u914d\u7f6eci\u548c\u4e0b\u8f7dsmarty\u7684\u6a21\u677f\u3002<br \/>\nCodeigniter\u7248\u672c\u4e3a2.1.2\uff0cSmarty\u7248\u672c\u4e3a3.1.11\u3002<br \/>\n2. \u7b2c\u4e8c\u90e8\u628a\u4e0b\u8f7d\u5230\u7684smarty\u7248\u672c\u89e3\u538b\u7136\u540e\u628a\u91cc\u9762\u7684libs\u6587\u4ef6\u6539\u540d\u4e3asmarty\u7136\u540e\u628a\u8fd9\u4e2a\u6587\u4ef6\u62f7\u5230ci\u7684\\application\\libraries\u76ee\u5f55\u4e0b\u9762<br \/>\n\u76ee\u9304\u7d50\u69cb\u662f\uff1a<span style=\"color: #0000ff;\">application\\libraries\\Smarty\\Smarty.class.php<\/span><br \/>\n3. \u5728ci\\application\\libraries\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u9762\u5efa\u7acb\u4e00\u4e2a\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u4f8b\u5982\u89c1\u4e00\u4e2aCismarty.php\u7684\u6587\u6863\u3002<br \/>\n4. \u7528\u7f16\u8bd1\u5668\u6253\u5f00Cismarty.php\u7136\u540e\u5199\u5165\u4ee5\u4e0b\u4ee3\u7801:<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:php\">&lt;?php \r\n  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\n require_once( APPPATH . 'libraries\/Smarty\/Smarty.class.php' );\r\n\r\n class Cismarty extends Smarty {\r\n\r\n     var $CI;\r\n\r\n     public function __construct()\r\n     {\r\n         parent::__construct();\r\n\r\n         $this-&gt;CI =&amp; get_instance();\r\n       $this-&gt;template_dir = APPPATH.'views';\r\n       $this-&gt;compile_dir  = APPPATH.'templates_c\/';\r\n       $this-&gt;cache_dir    = APPPATH.\"cache\/\";\r\n       $this-&gt;left_delimiter = '{';\r\n       $this-&gt;right_delimiter = '}';\r\n     }\r\n\r\n }<\/pre>\n<p>5. \u5728\u5efa\u7acb\u4e00\u4e2aci\\application\\templates_c\u6587\u4ef6\u5939<br \/>\n6. \u6253\u5f00ci\\application\\config\\autoload.php\u6587\u4ef6<br \/>\n\u628a$autoload[&#8216;libraries&#8217;] = array();\u6539\u6210$autoload[&#8216;libraries&#8217;] = array(&#8216;database&#8217;,&#8217;cismarty&#8217;);<br \/>\n\u6216\u8005\u5728\u4f7f\u7528\u7684\u6642\u5019load\u4e5f\u53ef:<span style=\"color: #0000ff;\">$this-&gt;load-&gt;library(&#8216;cismarty&#8217;);<\/span><\/p>\n<p>\u5efa\u7acb\u63a7\u5236\u5668text.php<\/p>\n<pre class=\"brush:php\">&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\nclass Text extends CI_Controller {\r\n\r\n\/**\r\n* Text construtor\r\n*\/\r\npublic function __construct()\r\n{\r\nparent::__construct();\r\n$this-&gt;load-&gt;library('cismarty');\r\n\r\n$this -&gt; load-&gt;helper('url');\r\n\/\/\u5b9a\u7fa9CSS\u3001JS\u8def\u5f91\r\n$this -&gt; cismarty -&gt; assign('base_url',base_url());\r\n\r\n}\r\n\/**\r\n* Index Page for this controller.\r\n*\/\r\npublic function index()\r\n{\r\n$this -&gt; cismarty -&gt; assign('title','\u6e2c\u8a66\u9801\u9762');\r\n$this -&gt; cismarty -&gt; display(\"text.html\");\r\n}\r\n}<\/pre>\n<p>\u5efa\u7acb\u6a21\u677f\u6587\u4ef6text.html<\/p>\n<pre class=\"brush:xml\"> &lt;html&gt;\r\n &lt;head&gt;\r\n   &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\r\n\r\n     &lt;title&gt;\u6e2c\u8a66\u9801\u9762&lt;\/title&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n {$base_url}\r\n &lt;p&gt;\u6a21\u677f\u5167\u5bb9&lt;\/p&gt;\r\n {$title}\r\n &lt;\/body&gt;\r\n &lt;\/html&gt;<\/pre>\n<p>\u6253\u958b\u700f\u89bd\u5668\uff0c \u901a\u904e\u700f\u89bd index.php\/text \u5373\u53ef\u67e5\u770b\u6548\u679c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ci\u662f\u500b\u4e0d\u932f\u7684\u6846\u67b6\uff0cSmarty\u662f\u4e00\u500b\u4e0d\u932f\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u7f51\u4e0a\u770b\u4e86\u5f88\u591a\uff0c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,15],"tags":[131,36,132,63,133,39,134],"class_list":["post-878","post","type-post","status-publish","format-standard","hentry","category-Note","category-Php","category-Program","tag-codeigniter","tag-php-2","tag-smarty","tag-63","tag-133","tag-39","tag-134"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=878"}],"version-history":[{"count":1,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":879,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/878\/revisions\/879"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}