Home >Backend Development >PHP Tutorial >Chuanzhi Podcast Smarty video tutorial materials (courseware, source code) sharing
"Smarty Video Tutorial" will show you how to implement code separation. smarty is one of the most famous PHP template engines in the industry. It separates logical code and external content, providing an easy-to-manage and use method to separate PHP code logic that is originally mixed with HTML code. Simply put, the purpose is to separate PHP programmers from front-end personnel so that the work of the two will not affect each other!
Course playback address: http://www.php.cn/course/353.html
The teacher’s teaching style:
The lectures are friendly and natural, unpretentious, not pretentious, nor deliberately exaggerated, but talk eloquently and carefully, and the relationship between teachers and students is In an atmosphere of equality, collaboration, and harmony, silent emotional exchanges are carried out, and the desire and exploration of knowledge are integrated into simple and real teaching situations. Students gain knowledge through quiet thinking and silent approval
The more difficult point in this video is Smarty-caching:
(1) Page caching: Global caching of the entire page
requires 4 steps:
①Enable caching $smarty->caching = true;
②Set cache life cycle $smarty->cache_lifetime = 3600;
③Set cache directory $ smarty->cache_dir = './cache';
④Set to fetch data from the database only when there is no cache
if(!$smarty->isCached(‘模板名’)){//从数据库取数据并assign赋值}
(2) Do not cache locally
On the basis of caching the entire page, there will be some parts of the page that cannot be cached and need to be updated in time. Such as time, stock information, etc. So this requires controlling local non-caching. There are 4 methods in total: Take passing a timestamp to the template in the php file as an example:
<?php //省略其他代码 $smarty->assign(‘time’,time()); ?>
①Single tag control
Use nocache in the tag of the template to control the tag No caching, as follows:
{$time nocache}
②nocache tag pair
In the area within the {nocache}{/nocache} tag pair, no caching can be achieved
For example
{nocache}{$time1}{$time2}{/nocache}
In this way, the two tags $time1 and $time2 are not cached
③Control when assigning a value
In php, when using the assign method to assign a value to the template tag, the first One parameter is the tag name, the second parameter is the value, and the third parameter is optional. If the third parameter is given as true, it means that the tag is not cached
$smarty->assign(‘time’,$time,true);
In this way, the {$time} tag in the template is not cached
④Get the corresponding value in PHP through the insert method The return value of the function
The specific syntax is: the {insert name='xxx'} tag in the template can get the return value of the insert_xxx() function in the php file.
For example: the code in the template file:
{insert name=’time’}
Here we also recommend downloading source code resources: http://www.php.cn/xiazai/ learn/2113
The resources share with you video courseware, ppt and source code
The above is the detailed content of Chuanzhi Podcast Smarty video tutorial materials (courseware, source code) sharing. For more information, please follow other related articles on the PHP Chinese website!