線上書籍

Home

PHP SMARTY 樣板引擎

一、函數

############################################################################### # 轉向函數 ############################################################################### function redirect_header($url = "", $time = 3000, $message = '已轉向!!',$error='') { $_SESSION['redirect'] = "\$.jGrowl('{$message}', { life:{$time} , position: 'center', speed: 'slow' });"; $_SESSION['error'] = $error ? $message :""; $url = $error ? WEB_URL . "/message.php":$url; header("location:{$url}"); exit; }

二、使用方法

  1. $url:轉向網址
  2. $time:停留時間
  3. $message:訊息
  4. $error:true => 轉向至 message.php
  5. redirect_header("index.php", 3000, $msg);
  6. $db->query($sql) or redirect_header("", 3000,  $db->error."\n".$sql,true);

三、message.php

  1. 程式碼 <?php /* 引入檔頭,每支程都會引入 */ require_once 'head.php'; /* 過濾變數,設定預設值 */ $op = system_CleanVars($_REQUEST, 'op', 'opList', 'string'); $sn = system_CleanVars($_REQUEST, 'sn', '', 'int'); /* 程式流程 */ switch ($op){ case "xxx" : //$msg = XXX(); redirect_header(WEB_URL, 3000, $msg); exit; default: $op = "opList"; opList(); break; } /*---- 將變數送至樣版----*/ $smarty->assign("WEB", $WEB); $smarty->assign("op", $op); /*---- 程式結尾-----*/ $smarty->display('message.tpl'); /*---- 函數區-----*/ ############################### # 預設 ############################### function opList(){ global $smarty; }

     

  2. message.tpl <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 可选的 Bootstrap 主题文件(一般不用引入) --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" href="<{$xoImgUrl}>css/style.css"> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <!-- bootstrap 驗證 --> <link rel="stylesheet" href="<{$xoAppUrl}>class/bootstrapValidator/css/bootstrapValidator.css"/> <script type="text/javascript" src="<{$xoAppUrl}>class/bootstrapValidator/js/bootstrapValidator.js"></script> <title>訊息 - 程式產生器</title> </head> <body> <{* 頁面轉向 *}> <{if $redirect}> <{include file="file:$redirectFile"}> <{/if}> <div class="container" style="margin-top:20px;"> <div class="alert alert-danger" role="alert"> <{$error}> </div> </div> </body> </html>

     

  3.