Smarty object is created

/*
        //if (!defined('INIT_NO_SMARTY'))
        //{
            header('Cache-control: private');
            header('Content-type: text/html; charset='.$config['charset']);

            /* 创建 Smarty 对象。*/
            require_once  TEST_ROOT. 'core/template.class.php';
            $this->view = new cls_template;

            $this->view->cache_lifetime = $_CFG['cache_time'];
            $this->view->template_dir   = ROOT_PATH . $config['tplPath'] . 'default';
            $this->view->cache_dir      = ROOT_PATH . 'temp/caches';
            $this->view->compile_dir    = ROOT_PATH . 'temp/compiled';

            if ((DEBUG_MODE & 2) == 2)
            {
                $this->view->direct_output = true;
                $this->view->force_compile = true;
            }
            else
            {
                $this->view->direct_output = false;
                $this->view->force_compile = false;
            }

            //$this->view->assign('lang', $_LANG);
            $this->view->assign('sys_charset', $config['charset']);
            if (!empty($_CFG['stylename']))
            {
                $this->view->assign('sys_css_path', 'themes/' . 'default' . '/style_' . $_CFG['stylename'] . '.css');
            }
            else
            {
                $this->view->assign('sys_css_path', 'themes/' . 'default' . '/style.css');
            }
            $GLOBALS['smarty']=$this->view;
        //}
        */

  PHP Web developers to do MVC framework (Smarty instructions)

A, Smarty simple tutorial 
 1. install the demo 
 download the latest version of Smarty -3.1.12 , then unzip the downloaded file. The next example demonstrates demo Smarty comes. 
( : 1) Download: HTTP // www.smarty.net/download 
(2) Create a new directory under your WEB server root directory, here I am in / var after the creation yqting / directory / www, then extract directory demo / and libs / directory to / var / WWW / yqting / directory. 
( 3) Of special note here demo cache / and template_c / directory / two directories, they must be provided as read and write permissions. 
  the chmod 777 Cache / 
  the chmod 777 template_c  
/ ( . 4) to start apache. In the browser, type http: // localhost / yqting / Demo / index.php, such a simple Smarty demo is realized. 
2. Smarty directory structure 
( 1) at / var / WWW /yqting directory start analyzing: 
  yqting /  
    ├── Demo 
    │ ├── Cache cache file storage directory 
    │ ├── configs profile directory 
    │ ├── index . PHP 
    │ ├── plugins from some useful plug-ins defined 
    │ ├── templates template directory 
    │ └── templates_c compiled file storage directory 
    └── libs 
       ├── Debug . tpl Debug template 
       ├── plugins from some useful plug-ins defined 
       ├── SmartyBC . class . PHP support Smarty 2 compatible 
       ├─ Smarty ─ . class . PHP Smarty class definition file 
       └── sysplugins Smarty core functionality plug-in, do not need to be modified 
( 2  ) add their own plug-ins defined
  above directory structure, in fact, is the most central part libs/ Directory, but this part is not allowed to modify. 
  And to add their own plug-ins, one approach is to define your own plug-ins on libs under / plugins / directory, another way is to create your own plugins / directory alone, but also create a cache /, configs /, templates / and templates _c / directory, but also to ensure cache / and templates_c / read-write permissions to the directory. 
  Not difficult to find, in fact, the above example, Demo / directory is a directory that contains a complete own custom plug-ins. We can refer demo / to achieve their own program directory. 
3. implement a simple example 
( 1) in the / var Create / www / yqting / directory weibo /, then create cache /, configs at weibo / directory /, templates / and templates_c / directory, modify cache / and templates_c / directory permissions to read and write.   
( 2) that creates a template file: index.tpl, this file in / var / WWW / yqting / Weibo / the templates directory, as follows: 
   <HTML> 
   <title> Smarty </ title> 
  <head>
  <metahttp-equiv = "the Type-the Content" Content = "text / HTML; charset = GB2312"> 
  </ head> 
  <body> 
  username : { $ the Name }
   </ body> 
  </ HTML> 
  This code is very simple, do not understand what continues to look down, do not worry! Each display do .tpl files are .php files corresponding to a business logic processing, described below this. PHP file. 
( 3) Create the index.php, this file in / var / WWW / yqting / Weibo / at, as follows: 
   <? PHP 
  / * Sample Example * / 
  the require '../libs/Smarty.class.php' ; 
  Smarty $ = new new Smarty (); 
  $ username = "Smarty" ; 
  $ Smarty -> $ username ); 
  $ Smarty -> the display ( 'the index.tpl' ); 
   ?>  
  which require the use of the path must be correct, you can refer to the above directory structure look! 
( 4 ) in Smarty3, the constructor Smarty class has been designated the template_dir, compile_dir, config_dir and cache_dir, need not be specified again.     
( 5) browser to http: // localhost / yqting / weibo / index.php, you can see the information output username:. Smarty the 
two, smarty interpretation program 
  we can see that the program is in line with the actual part of the smarty php code is a set of language norms, we turn to explain:   
( 1) / ** / statement: 
  contains part of the programming head of the main comments. content should be the role of the program, with the author and copyright write time to do a brief introduction, which smarty is not required, but the style of the program, this is a good style.   
( 2 ) include_once statement: 
  it will be installed to the site of smarty file that contains the current file, path included attention must write correctly.   
( 3) $ smarty = new new Smarty ():  
  This is a new one Smarty object $ Smarty , instantiating a simple object.   
( 4) $ smarty -> templates = "":  
  This is a clause which specifies the path when using tpl template $ smarty objects , it is a directory, in the absence of this sentence, Smarty default template path for the templates directory of the current directory, actually when writing the program, we want this sentence states that this is a good program style.   
( 5) $ smarty -> templates_c = "":  
  This indicates a directory of $ smarty object is compiled . In the template design articles we already know is a compiled Smarty template language, and this directory is that it compiled templates directory, be aware that if the site is located on a linux server, make sure teamplates_c defined in this directory has writable readable permissions, by default it is templates_c compile directory in the current directory, for the same reason we expressly written out.   
( 6) delimiter $ Smarty -> left_delimiter and $ Smarty -> right_delimiter:  
  indicates about when looking template variable delimiter. The default is"{" And "}", but in practice because we want to use the <script> in the template function definition Script is inevitably use {}, although it has its own solution, but the habits we redefine it is "# {" and "} #" or "<! - {" and "} ->", or other identifier, note that if the delimiter is defined here about, in the template file corresponding to so that each variable defined using the same reference numerals, for example, herein designated "<{" and "}>", html template will have the corresponding { $ name } becomes <{ $ name }>, so that the program was you can find the correct template variables.   
( 7) $ Smarty -> Cache = "./ Cache":  
  Tell output Smarty template file cache. Previous Smarty We know that the biggest advantage is that it can be cached, this is to set the cache directory. By default, the current directory under the cache directory, and directory templates_c equivalent in linux system, we want to make sure it's readable and writable properties.   
( . 8) $ Smarty -> cache_lifetime = 60 * 60 * 24:    
  Here it will be calculated second cache valid time units. Cache will be rebuilt when Smarty's the first time the cache time expires caching variable is set to true. When its value is - 1 indicates that established the cache never expires, is 0 indicates that the cache is always re-established each time the program execution. The above said it would set cache_lifetime set to one day.   
( 9) $ Smarty-> caching = 1:    
  This attribute tells Smarty whether or not to cache and cache mode. 
  It can take three values, 0: Smarty default value that represents the right template caching; 1: that Smarty will use the currently defined cache_lifetime to decide whether to end cache; 2 : that Smarty will use use cache_lifetime this value when the cache is built . Use true and false is customary to indicate whether the cache.   
( 10) $ Smarty -> ASSIGN ( "name", $ username ): 
  a prototype of the number of ASSIGN ( String varname, Mixed  var ), varname template variables used in the template, var variable name you want to point out a template variable substitution ; its second prototype for the ASSIGN ( Mixed  var ), we want to explain in detail later in this example, use member function, assign one of the core functions of Smarty, replace all of the template variables have to use it.   
( 11) $ Smarty -> Run the display ( "index.tpl"):    
  The function prototype for the Run the display ( Stringvarname), acts as a template to display. Simply put, it will analyze the treated template displayed here do not add the path of the template file, just use a file name on it, it is the path we have $ Smarty -> Templates ( String path) defined before.   

After the implementation of the program we can open templates_c the cache directory under the current directory, you will find some more in the bottom %% directory, these directories is Smarty compile and cache directory, it is automatically generated by the program, these do not directly generate the file is modified.   
I simply put Smarty above program some common basic elements introduced a bit, in the example you can see behind them will be multiple use.
View Code

 

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11464930.html