PHPでSmartyのテンプレートエンジンを使っている場合に、テンプレートに直接CSSやJavascriptを書く時は {literal} タグを前後に書いておきます。
{ や } がテンプレート言語の開始を表すデリミタとして認識されるためです。
{literal}と{/literal}の中はそのまま表示されます。
< スポンサーリンク >
例
テンプレートのheader内にCSSを書いてみます。
PHP:test.php
<?php
require_once “../libs/smarty-3.1.38/libs/Smarty.class.php”;
$smarty = new Smarty();
$smarty->template_dir = “./templates/”;
$smarty->compile_dir = “./templates_c/”;
$txt = array(“男”,”女”);
$smarty->assign(‘txt’,$txt);
$smarty->display(‘test.tpl’);
テンプレート:test.tpl
<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”utf-8″>
<title>Smarty Test</title>
{literal}
<style>
.male{color:blue;}
.female{color:red;}
</style>
{/literal}
</head>
<body>
<form name=”in_form” id=”in_form”>
<p class=”male”>{$txt.0}</p>
<p class=”female”>{$txt.1}</p>
</form>
</body>
</html>
ブラウザで見てみると、ちゃんとスタイルが反映されています。
literalタグをいれていない場合は、下図のようなエラーが出ます。
Fatal error: Uncaught –> Smarty Compiler: Syntax error in template “file:C:\inetpub\wwwroot\smarty\templates\test.tpl” on line 7 “.male{color:blue;}” – Unexpected “:”, expected one of: “}” <– thrown in C:\inetpub\wwwroot\libs\smarty-3.1.38\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 7
こんな記事も書いています