PHP page
<?php //open Output Control buffer Ob_start ();//new template file $file = "./cache/newindex{$_get[' page ']}.html"; $cachetime = 10;const DSN = ' Mysql:host=localhost;dbname=test ';const dbuser = ' root ';const dbpwd = ' root ';// Template does not exist or exceeds cache time to generate a cache template if (!file_exists ($file) | | filemtime ($file) + $cachetime <time ()) {//Create Smarty Object require_once './libs/smarty.class.php '; require_ once ' page.class.php ';//define root directory define (' root ', str_replace ("\ \", "/", DirName (__file__)). " /");//Instantiate Smarty Class $smarty=new smarty ();//Set delimiter $smarty->left_delimiter=" <{"; $smarty->right_ Delimiter= "}>";//set to false the bounding symbol can have spaces $smarty->auto_literal = false;//add a plugin directory//$smarty- >setpluginsdir (ROOT. " /libs/myplugins/");//Note Add a plug-in, to the system default path to add otherwise cannot use the default system plug-in $smarty->setpluginsdir (Array ( root. " /libs/plugins/",//system default set path root." /libs/myplugins/",//Custom));//Connect Database try{ $pdo = new pdo (dsn, dbuser,dbpwd);} catch (pdoexception $e) { echo "database connection failed:". $e->getmessage (); exit;} $query = "Select id, username, password,email from users limit 3" , $stmt = $pdo->prepare ($query), $stmt ->execute (), $users = $stmt->fetchall ( PDO::FETCH_ASSOC); $smarty->assign (' users ', $users);//var_dump ($users); $query = "Desc users"; $ stmt = $pdo->prepare ($query); $stmt ->execute (); $tdname = $stmt->fetchall (PDO :: Fetch_column);//var_dump ($tdname); $page = new page (20, 2); $smarty->assign (' Tdname ', $ Tdname); $smarty->assign (' fpage ', $page->fpage ());//variable Output $smarty->display (' hello.tpl ');//Returns the contents of the output buffer $ Content = ob_get_contents ();//Writes a string to the file File_put_contents ("./cache/newindex{$_get[' page ']}.html", $content);//Flushes out (sends out) the contents of the output buffer Ob_flush ();echo "cache file does not exist, query the database, generate the cache file, output to the browser"; else{ include $file; echo "load cache files directly";}? >
Template TPL page
Browser page
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/86/9F/wKioL1fFfpOjAriAAAA6bv1SNIo927.png-wh_500x0-wm_3 -wmp_4-s_867193092.png "title=" Firefox _2016-08-30t12-38-34.786z.png "alt=" wkiol1fffpojariaaaa6bv1snio927.png-wh_50 "/ >
This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1844499
PHP Smarty page static plus paging and cache control