本篇文章主要介绍php smarty 定界符冲突该如何处理,感兴趣的朋友参考下,希望对大家有所帮助。
默认定界符{与css和js中的{冲突,该如何处理?
1. 所有以{ 开头的地方,都空一格。(smarty只会解析定界符内的内容,且左定界符后不能有空格)
2. 将css和js以外部的方式引入。(smarty不会解析外部文件)
3. 使用内置函数 literal。
4. 更改定界符。
解决冲突最好的方式:外部引入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>
相关推荐:
php定界符eof讲解
php定界符eof如何使用
php的定界符heredoc技术详解
以上就是php smarty 定界符冲突该如何处理的详细内容。