成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼
Ecshop中怎样使Smarty支持模板路径替换
smarty目次是smarty顺序,themes是模板目次,themes下面的每一个子目次代表一种模板。默许是default模板。
模板下面的dwt是模板文件,lib里面lbi是模板库文件,images目次寄存图片。和ECShop非常的相似,由于我预备仿它。

每个dwt对应一个php页面,比方index.php对应index.dwt。
好了。
比方说index.dwt里面有句代码:
<div style="WIDTH: 650px; WHITE-SPACE: nowrap; OVERFLOW: auto" class="codecolorer-container html4strict mac-classic">
<div class="html4strict codecolorer"><img src="images/user.gif" alt="user" />
也许你会以为这个图片可以显示出来,然则我们翻开localhost/index.php,发现图片并未显示,图片的途径酿成了localhost/images/user.gif,而真正的途径应该是localhost/themes/default/images/user.gif。
这只是图片的例子,假如js或许css都需求交换,就需求给smarty写个插件或许是扩展smarty的代码。
小弟不才,只能在本来smarty的持续上修正代码,办法是:
翻开Smarty_Compiler.class.php,跳转到_compile_file函数的这句:
$compiled_content = $template_header . $compiled_content;
的下面添加以下代码,都是用正则表达式(preg_replace)来交换字符串,详细寄义有注释:
<div style="WIDTH: 650px; WHITE-SPACE: nowrap; OVERFLOW: auto" class="codecolorer-container php mac-classic">
<div class="php codecolorer">$tmp_dir = 'themes/' . $GLOBALS['_CFG']['template'] . '/'; // 模板地点途径
/* 批改css途径 */
$compiled_content = preg_replace('/(<linkshref=["|'])(?:./|../)?(css/)?([a-z0-9A-Z_]+.css["|']srel=["|']stylesheet["|']stype=["|']text/css["|'])/i','1' . $tmp_dir . '23', $compiled_content);
/* 批改js目次下js的途径 */
$compiled_content = preg_replace('/(<scripts(?:type|language)=["|']text/javascript["|']ssrc=["|'])(?:./|../)?(js/[a-z0-9A-Z_-.]+.(?:js|vbs)["|']></script>)/', '1' . $tmp_dir . '2', $compiled_content);
$pattern = array(
'/|n]*?({.+?})[^<|{|n]*?-->/', // 交换smarty注释
'/<!--[^<|>|{|n]*?-->/', // 交换不换行的html注释
'/(href=["|'])../(.*?)(["|'])/i', // 交换相对链接
'/((?:background|src)s*=s*["|'])(?:./|../)?(images/.*?["|'])/is', // 在images前加上 $tmp_dir
'/((?:background|background-image):s*?url()(?:./|../)?(images/)/is', // 在images前加上 $tmp_dir
'/(['|"])..//is', // 以../开首的途径悉数批改为空
);
$replace = array(
'1',
'',
'123',
'1' . $tmp_dir . '2',
'1' . $tmp_dir . '2',
'1'
);
$compiled_content= preg_replace($pattern, $replace, $compiled_content);
$GLOBALS['_CFG']['template']是一个全局变量,用于寄存当时模板的目次名,你可以放在装备文件中,也可以放入数据库,然后翻开页面的时分读取就行了。
也并纷歧定是一切的资本都需求交换,视你的项目状况而定,其实这几行代码都是从ECShop的cls_template.php copy过来的。在dwt里面刺进js、css的时分需求留意,rel、type、src等属性的在script和link中的挨次固定依照以上代码的挨次。不然无法交换。
新余网站建设

