The examples in this article describe the simple configuration and usage of Smarty templates. Share it with everyone for your reference, the details are as follows:
Create templates, templates_c, configs, cache directories in the Smarty directory. So that Smarty can compile and cache it.
Create the smarty_inc.php file to configure smarty as follows:
<?php include_once("./smarty/Smarty.class.php"); //包含smarty类文件 $smarty = new Smarty(); //建立smarty实例对象$smarty $smarty->caching=false; //开发是不建议开启缓存 $smarty->template_dir="./templates"; //设置模板目录 $smarty->compile_dir="./templates_c"; //设置编译目录 $smarty->cache_dir="./cache"; //缓存文件夹 $smarty->cache_lifetime=60; $smarty->left_delimiter = "<{"; //左定界符 $smarty->right_delimiter = "}>"; //右定界符 ?>
Run the test and create the index.php file in the root directory:
<?php include("smarty_inc.php"); $val= array("丁庆","董丹凤","情侣"); $smarty->assign("name",$val); $smarty->display("index.html"); ?>
Create the index.html template under templates:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset="utf-8"> <title>测试页面</title> </head> <body> <{foreach from=$name item=x}> 数组内容:<{$x}> <br/> <{/foreach}> </body> </html>
Okay, you’re done, it’s that simple.
For more content related to Smarty, please check out the special topics on this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Template Technology", "Summary of PHP Database Operation Skills Based on PDO", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "php Common Databases" Summary of operating skills》
I hope this article will be helpful to everyone’s PHP program design based on smarty templates.