smarty模板使用
- 安装配置
下载smarty模板核心类库,把libs文件夹复制到项目文件夹下,修改为smarty
在php文件中调用
require '../smarty/Smarty.class.php';
$tpl = new Smarty();
$tpl->left_delimiter="{{";
$tpl->right_delimiter="}}";
$tpl->setTemplateDir('../templates');
$tpl->setCompileDir('./templates_c');
//$tpl->setConfigDir('./templates_c');
$tpl->setCacheDir('../secretCache');
$mytitle = 'smart官方模板';
#需要注意的是模板文件中,变量标签名通常是{{$mytitle}}但在smarty默认的assign方法中要去掉$,例如 {{$mytitle}},assign语句为$tpl->assign('mytitle','我的标题')
$tpl->assign('mytitle',$mytitle);
$tpl->assign('mywords','搏击选手居马别克和朋友们');
$tpl->assign('mypic','../static/pic/5vkj20dc0hsmyy.jpg');
$tpl->display('../templates/selfdefine.html')
- 模板文件
模板文件 <!DOCTYPE html> <html lang="en"> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="../../../../favicon.ico"> <title>{{$mytitle}}</title> #第一个模板变量 <!-- Bootstrap core CSS --> <link href="../static/booststrap4/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="../static/mycss/navbar-top.css" rel="stylesheet"> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4"> <a class="navbar-brand" href="#">Top navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarCollapse"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> <form class="form-inline mt-2 mt-md-0"> <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </div> </nav> <main role="main" class="container"> <div class="jumbotron"> <h1>Navbar example</h1> <p class="lead">{{$mywords}}</p> #第二个模板变量 <a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs »</a> <p> <br> <img src="{{$mypic}}" height="200"> #第三个模板变量 </p> </div> </main> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="../static/booststrap4/js/jquery-3.3.1.min.js"></script> </body> </html>
- 效果