How to integrate thinkphp-based onethink with smarty Templates

Source: Internet
Author: User
Tags table name smarty template

To integrate the smarty template engine with think templates, follow these steps:

I. Add Application/Common/Conf/config. php

'Tmpl _ ENGINE_TYPE '=> 'smarty ',

II. Download the smarty template engine

Http://www.smarty.net/

Under ThinkPHP \ Library \ Vendor

Because onethink already has a smarty class

ThinkPHP/Library/Think/Template/Driver/Smarty. class. php

Public function fetch ($ templateFile, $ var ){
$ TemplateFile = substr ($ templateFile, strlen (THEME_PATH ));
Vendor ('smarty. Smarty # class ');
$ Tpl = new \ Smarty ();
$ Tpl-> caching = C ('tmpl _ CACHE_ON ');
$ Tpl-> template_dir = THEME_PATH;
$ Tpl-> compile_dir = CACHE_PATH;
$ Tpl-> cache_dir = TEMP_PATH;
If (C ('tmpl _ ENGINE_CONFIG ')){
$ Config = C ('tmpl _ ENGINE_CONFIG ');
Foreach ($ config as $ key => $ val ){
$ Tpl-> {$ key} = $ val;
}
}
$ Tpl-> assign ($ var );
$ Tpl-> display ($ templateFile );
}

The onethink template parsing process is in

HinkPHP \ Library \ Behavior \ ParseTemplateBehavior. class. php
Public function run (& $ _ data ){
$ Engine = strtolower (C ('tmpl _ ENGINE_TYPE '));
$ _ Content = empty ($ _ data ['content'])? $ _ Data ['file']: $ _ data ['content'];
$ _ Data ['prefix'] =! Empty ($ _ data ['prefix'])? $ _ Data ['prefix']: C ('tmpl _ cache_prefix ');
If ('think '= $ engine) {// use the think template engine
If ((! Empty ($ _ data ['content']) & $ this-> checkContentCache ($ _ data ['content'], $ _ data ['prefix'])
| $ This-> checkCache ($ _ data ['file'], $ _ data ['prefix']) {// The cache is valid.
// Load the template cache file
Storage: load (C ('cache _ path '). $ _ data ['prefix']. md5 ($ _ content ). C ('tmpl _ CACHFILE_SUFFIX '), $ _ data ['var']);
} Else {
$ Tpl = Think: instance ('think \ template ');
// Compile and load the template file
$ Tpl-> fetch ($ _ content, $ _ data ['var'], $ _ data ['prefix']);
}
} Else {
// Call the third-party template engine for parsing and output
If (strpos ($ engine ,'\\')){
$ Class = $ engine;
} Else {
$ Class = 'think \ Template \ Driver \ '. ucwords ($ engine );
}
If (class_exists ($ class )){
$ Tpl = new $ class;
$ Tpl-> fetch ($ _ content, $ _ data ['var']);
} Else {// The class is not defined
E (L ('_ NOT_SUPPERT _'). ':'. $ class );
}
}
}

It can be seen that if the template engine is not think, it will be instantiated.

'Think \ Template \ Driver \ '. ucwords ($ engine );

In this way, your system can support the smarty template,

However, some of your other templates do not want to use smarty or retain think.

You can add a keyword in config. php.

If (strstr ($ _ GET ['s '], "NoSmarty ")! = ''){){
 
Return array (
 
'Tmpl _ ENGINE_TYPE '=> 'smarty ',
 
// The current controller uses smarty
 
);
 
} Else {
 
Return array (
 
// The current controller does not use smarty
 
);
 
}




ThinkPHP integrates the Smarty template engine to operate mysql databases

To truly separate the data from the front-end page, you must use the smarty template. In this way, you can only transmit data.

Default settings of the template for configuring Smarty: 'tmpl _ ENGINE_TYPE '=> 'smarty'

Smarty does not parse tags and content in the template: {literal} {/literal}; // but some cannot be used

If the created Smarty template engine is used, Think objects cannot be used to define object data, and Think should be changed to Smarty.

If the actual content is: {$ result array. One-dimensional label. Column name of the table };

However, I don't know what's going on. In version 3.2, I cannot use foreach to traverse the content.

SELECT query condition structure: select field from table name where group field having, order or limit;

For example, create the following obj object:

$ Obj = D ();

$ Obj-> select (); all data table information is queried.

$ Obj-> field (field 1, field 2 ,..); Specify the query field;

$ Obj-> table (data table) query through a specific data table;

$ Obj-> where (parameter) is the condition after where;

$ Obj-> group (field), which is queried by field grouping;

$ Obj-> having (parameter condition), having condition setting;

$ Obj-> order (parameter) for sorting query;

$ Obj-> limit (offset, number of items), limit the number of items to query;

$ Obj-> select (primary key value); only query the information of this primary key. You can also specify multiple primary keys.

Note that there is no assembly order during SQL statement query, but the default order is set internally.

If the query result contains only one piece of information, you can use the find method to save resources.

$ Obj-> find (primary key value); note that this is a one-dimensional array

We also want to talk about the following:

Having: This is similar to where, but they have the following differences:

1: When the two results exist in a general query, they can be used at the same time.

2: if the query results cannot be found, you can only use where

3: If you want to use an alias, you can only use having.


Aggregate functions:

It can be used directly as the method of the last function or directly as follows:

Count (); queries the number of records.

Max (); maximum value query.

Min (); min query.

Avg (); calculates the average value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.