默认定界符"{"与css和js中的"{"冲突,该如何处理?
a. 所有以{ 开头的地方,都空一格。(Smarty只会解析定界符内的内容,且左定界符后不能有空格)
b. 将css和js以外部的方式引入。(Smarty不会解析外部文件)
c. 使用内置函数 literal。
d. 更改定界符。
解决冲突最好的方式:外部引入css和js,对于内部出现的使用literal。
index.php(后端):
<?php
//1.引入smarty类
include 'libs/Smarty.class.php';
//2.实例化smarty对象
$smarty = new Smarty();
//3.设置相关属性
$smarty->template_dir = "templates/"; //模板目录
$smarty->compile_dir = "templates_c"; //编译目录
//修改定界符
$smarty->left_delimiter = '<{'; //自定义定界符,默认是"{"
$smarty->right_delimiter = '}>';
//4.分配数据
$smarty->assign('title','smarty模板引擎');
$smarty->assign('content','smarty模板引擎 是一个强大的模板引擎!');
//5.载入视图
$smarty->display('index.html');
index.html(前端视图):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{$title}</title>
<style>
<{literal}> <{* 通过literal函数解决定界符"{"在CSS和JS中的冲突。Smarty会自动解析定界符内的内容(不会解析引入的外部文件)。也可以通过自定义定界符解决冲突。 *}>
h1{color:tomato; font-size:40px;}
p{color: #00f;}
<{/literal}>
</style>
</head>
<body>
<h1><{$title}> $title</h1> <{* 只有定界符内的内容才会被Smarty解析,且左定界符后不能有空格 *}>
<p><{$content}></p>
<p><?php echo $title;?></p> <{* 不会解析PHP代码 *}>
<{*
这是注释
*}>
</body>
</html>