Home >Backend Development >PHP Tutorial >How to deal with delimiter conflicts in PHP Smarty

How to deal with delimiter conflicts in PHP Smarty

墨辰丷
墨辰丷Original
2018-05-16 13:47:571831browse

This article mainly introduces how to deal with delimiter conflicts in PHP Smarty. Interested friends can refer to it. I hope it will be helpful to everyone.

The default delimiter "{" conflicts with "{" in css and js. How to deal with it?
1. All spaces starting with { should be left blank. (Smarty will only parse the content within the delimiter, and there must be no spaces after the left delimiter)
2. Import css and js externally. (Smarty will not parse external files)
3. Use the built-in function literal.
4. Change the delimiter.

The best way to resolve conflicts: introduce css and js externally, and use literal for internal ones.

index.php (backend):

<?php  
//1.引入smarty类  
include &#39;libs/Smarty.class.php&#39;;  
//2.实例化smarty对象  
$smarty = new Smarty();   
//3.设置相关属性  
$smarty->template_dir = "templates/"; //模板目录  
$smarty->compile_dir = "templates_c"; //编译目录  
//修改定界符  
$smarty->left_delimiter = &#39;<{&#39;;    //自定义定界符,默认是"{"  
$smarty->right_delimiter = &#39;}>&#39;;  
//4.分配数据  
$smarty->assign(&#39;title&#39;,&#39;smarty模板引擎&#39;);  
$smarty->assign(&#39;content&#39;,&#39;smarty模板引擎 是一个强大的模板引擎!&#39;);  
//5.载入视图  
$smarty->display(&#39;index.html&#39;);

index.html (front-end view):

<!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>

Related recommendations:

php delimiter EOF explains

PHP delimiter eof how to use the delimiter of

php heredoc Technical details

The above is the detailed content of How to deal with delimiter conflicts in PHP Smarty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn