避免smarty與css語法沖突的方法
本文實例講述了避免smarty與css語法沖突的方法。分享給大家供大家參考。具體分析如下:
熟悉css的人很快就會發(fā)現(xiàn)smarty和css的語法存在沖突,因為二者都需要使用大括號{}。如果簡單地將css標記嵌入到html文檔首部,將導致不可識別標記錯誤:
- <html>
- <head>
- <title>{$title}</title>
- <style type=text/css>
- p{
- margin::2px
- }
- </style>
- </head>
- ...
不要擔心,因為我們有3種解決方案。
一、使用link標記從另一個文件中提取樣式信息:
- <html>
- <head>
- <link rel=stylesheet type=text/css href=css/default.css/>
- </head>
- ...
二、使用smarty的literal標記將樣式表信息包圍起來
這些標記告訴smarty不要解析該標記內(nèi)的任何內(nèi)容:
- <html>
- <head>
- {literal}
- p{
- margin::2px
- }
- </style>
- {/literal}
- </head>
- ...
三、修改smarty的默認定界符
可以通過設置center_delimiter和center_delimiter屬性來做到這一點:
- require(smarty.class.php);
- $smarty=newsmarty;
- $smarty->left_delimiter='';
- $smarty->right_delimiter='';
- ...
- ?>
雖然3種解決方案都能解決問題,但其中***種可能是最方便的,因為將css放在單獨的文件中是一種常見的實踐做法。此外,這種解決方案不需要修改smarty的重要默認配置(定界符)。
希望本文所述對大家的php程序設計有所幫助。