Home  >  Article  >  Backend Development  >  避免Smarty与CSS语法冲突的方法_php实例

避免Smarty与CSS语法冲突的方法_php实例

WBOY
WBOYOriginal
2016-05-16 20:22:32967browse

本文实例讲述了避免Smarty与CSS语法冲突的方法。分享给大家供大家参考。具体分析如下:

熟悉CSS的人很快就会发现Smarty和CSS的语法存在冲突,因为二者都需要使用大括号{}。如果简单地将CSS标记嵌入到HTML文档首部,将导致"不可识别标记"错误:

<html> 
<head> 
<title>{$title}</title> 
<style type="text/css"> 
p{ 
margin::2px 
} 
</style> 
</head> 
... 

不要担心,因为我们有3种解决方案。

一、使用link标记从另一个文件中提取样式信息:

<html> 
<head> 
<title>{$title}</title> 
<link rel="stylesheet" type="text/css" href="css/default.css"/> 
</head> 
...

二、使用Smarty的literal标记将样式表信息包围起来

这些标记告诉Smarty不要解析该标记内的任何内容:

<html> 
<head> 
<title>{$title}</title> 
{literal} 
<style type="text/css"> 
p{ 
margin::2px 
} 
</style> 
{/literal} 
</head> 
...

三、修改Smarty的默认定界符

可以通过设置center_delimiter和center_delimiter属性来做到这一点:

<&#63;php 
require("Smarty.class.php"); 
$smarty=newSmarty; 
$smarty->left_delimiter=''; 
$smarty->right_delimiter=''; 
... 
&#63;>

虽然3种解决方案都能解决问题,但其中第一种可能是最方便的,因为将CSS放在单独的文件中是一种常见的实践做法。此外,这种解决方案不需要修改Smarty的重要默认配置(定界符)。

希望本文所述对大家的php程序设计有所帮助。

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