Php Smarty,包括css/javascript文件
根据页面名称,在页面上包含不同的CSS或JavaScript文件是真的吗。 例如,如果我有页面顺序,将包含Php Smarty,包括css/javascript文件,php,templates,include,smarty,assign,Php,Templates,Include,Smarty,Assign,根据页面名称,在页面上包含不同的CSS或JavaScript文件是真的吗。 例如,如果我有页面顺序,将包含orders.css,如果我有索引页面将包含index.css,或者可能与其他检查一起,如果x=3 includemain.js,如果x=5 includevalidate.js 我知道我可以通过PHP实现这一点,比如: if (x = 3) $smarty->assign('file_css', 'orders'); else $smarty->assign('file_css
orders.css
,如果我有索引页面将包含index.css
,或者可能与其他检查一起,如果x=3 includemain.js
,如果x=5 includevalidate.js
我知道我可以通过PHP实现这一点,比如:
if (x = 3) $smarty->assign('file_css', 'orders');
else $smarty->assign('file_css', 'index');
<link rel='stylesheet' href='css/{$file_css}.css' />
{smarty block='css'}
<link ....... />
<script ..... />
{/smarty block}
在模板中写下如下内容:
if (x = 3) $smarty->assign('file_css', 'orders');
else $smarty->assign('file_css', 'index');
<link rel='stylesheet' href='css/{$file_css}.css' />
{smarty block='css'}
<link ....... />
<script ..... />
{/smarty block}
但也许smarty有办法做到这一点
不久前,我在项目中看到了类似于smarty模板中的块的东西,其中是css/js文件,它们通过一些魔术被包含到页面中,mby由PHP包含,我不知道,但它看起来非常清晰,而且很舒服。有一个文件,其中类似于:
if (x = 3) $smarty->assign('file_css', 'orders');
else $smarty->assign('file_css', 'index');
<link rel='stylesheet' href='css/{$file_css}.css' />
{smarty block='css'}
<link ....... />
<script ..... />
{/smarty block}
{smarty block='css'}
{/smarty block}
然后这些块被包含在模板上必要的地方
这只是个例子,我不记得它到底是怎么叫的,但它是这样的。有几种方法可以让你随心所欲。但是,您可能正在考虑模板继承 您可能可以执行类似于此示例的操作,并在main.tpl中使用不同的样式表附加CSS index.tpl
{block name='head'}{/block}
{block name='body_main'}{block}
main.tpl
{block name='head'append}
{/block}
{block name='body_main'}
你的内容在这里
{/block}
更多信息请查看此处。
有几种方法可以让你随心所欲。但是,您可能正在考虑模板继承 您可能可以执行类似于此示例的操作,并在main.tpl中使用不同的样式表附加CSS index.tpl
{block name='head'}{/block}
{block name='body_main'}{block}
main.tpl
{block name='head'append}
{/block}
{block name='body_main'}
你的内容在这里
{/block}
更多信息请查看此处。
您可以使用{smarty.template},它为当前模板名提供了.tpl扩展名,如“orders.tpl”。 用您的选项替换扩展
<link rel='stylesheet' href='css/{$smarty.template|replace :'.tpl':'.css'}' />
您可以使用{smarty.template},它为当前模板名提供了.tpl扩展名,如“orders.tpl”。 用您的选项替换扩展
<link rel='stylesheet' href='css/{$smarty.template|replace :'.tpl':'.css'}' />
非常感谢,这是一种非常酷的方式。比如果更好,否则:)非常感谢,这是一个非常酷的方法。比“如果”更好,否则:)