Basic settings about smarty

WBOY
Release: 2016-07-25 09:10:26
Original
940 people have browsed it
  1. include_once("smarty/Smarty.class.php");
  2. $smarty=new Smarty();
  3. $smarty->config_dir="smarty/Config_File.class.php";
  4. $smarty->caching=false;
  5. $smarty->template_dir="./templates";
  6. $smarty->compile_dir="./templates_c";
  7. $smarty->cache_dir="./smarty_cache ";
  8. $smarty->left_delimiter="{";
  9. $smarty->right_delimiter="}";
  10. ?>
Copy the code

3. Write the php file index.php

  1. include("smarty_inc.php");
  2. $name[]=array("name"=>"News First","date"=>"2008- 1-1");
  3. $name[]=array("name"=>"News Article 2","date"=>"2008-2-1");
  4. $name[]=array( "name"=>"News Article 3","date"=>"2008-3-1");
  5. $row=array("title","author");
  6. $smarty->assign ("title",$name);
  7. $smarty->assign("row",$row);
  8. $smarty->display("index.html")
  9. ?>
Copy code

4. Write index.html in the templates folder index.html

  1. {$row[0]}|{$row[1]}
  2. {section name=list loop=$title}
  3. {$title[list].name}--{$title[list].date}

  4. {/section}
Copy code

5. Use variable operators index.php

  1. include("smarty_inc.php");
  2. $value="it is Work and, it Is video";
  3. $smarty->assign("name",$value) ;
  4. $smarty->display("index.html")
  5. ?>
Copy code

index.html

  1. Original content: {$name}
  2. {$name|capitalize}
  3. {$name|cat:"Demo"}
  4. { $smarty.now|date_format:'%Y-%M-%D'};
  5. {$name|repalce:"is":"***"};
  6. {$name|truncate}
Copy code

6. foreach index.php

  1. include("smarty_inc.php");
  2. $value=array(4,5,6,7);
  3. $value_key=array('a'=>"PHP" ,'b'=>"JAVA",'c'=>"C++");
  4. $smarty->assign("name",$value);
  5. $smarty->assign("name_key", $value_key);
  6. $smarty->display("index.html")
  7. ?>
Copy code

index.html

  1. {include file="header.html"}
  2. {foreach from=$name item=id}
  3. Array content: {$id}
  4. {/foreach}
  5. {foreach from=$name item=id key=k}
  6. Array content: {$k}--{$id}
  7. {/foreach}
Copy code

7.literal When curly brackets appear, if you use javascript, you can use literal for text processing

8.strip Optimize the page to remove the spaces in the tags. Make it difficult for people to steal easily 9. Cache basic configuration:

  1. include_once("smarty/Smarty.class.php");
  2. $smarty=new Smarty();
  3. $smarty->config_dir="smarty/Config_File.class.php" ;
  4. $smarty->caching=true;
  5. $smarty->template_dir="./templates";
  6. $smarty->compile_dir="./templates_c";
  7. $smarty->cache_dir="./ smarty_cache";
  8. $smarty->cache_lifetime=60;
  9. $smarty->left_delimiter="{";
  10. $smarty->right_delimiter="}";
  11. ?>
Copy code

With ID cache

  1. include("smarty_inc.php");
  2. $id=$_GET[id];
  3. $value=array(4,5,6,7);
  4. $smarty-> ;assign("name",$value);
  5. $smarty->assign("id",$id);
  6. $smarty->display("index.html",$id);
  7. ?>
Copy code

Clear cache and partial cache

  1. include("smarty_inc.php");
  2. $id=$_GET[id];
  3. $value=array(4,5,6,7);
  4. //Use insert Local cache:
  5. function insert_shijian(){
  6. return date("Y-m-d H:m:s");
  7. }
  8. $smarty->assign("name",$value);
  9. $smarty->assign( "id",$id);
  10. $smarty->display("index.html",$id);
  11. //Delete the cache with ID$smarty->clear_cache('index.html',$id) ;
  12. //Delete all cache $smarty->clear_all_cache();
  13. ?>
Copy code
  1. {insert name='shijian' }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!