开发者社区> 问答> 正文

请教php smarty 插件调用 错误?报错

在smarty/plugins/下,新建文件:modifier.truncate_cn.php

内容如下:

<?php

/*
*作者:http://www.phpernote.com/
*时间:2013年1月31日06:31:52
*作用:截取中文字符串
*/
function smarty_modifier_truncate_cn($string,$length=0,$ellipsis='…',$start=0){
	$string=strip_tags($string);
	$string=preg_replace('/\n/is','',$string);
	//$string=preg_replace('/ | /is','',$string);//清除字符串中的空格
	$string=preg_replace('/ /is','',$string);
	preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/",$string,$string);
	if(is_array($string)&&!empty($string[0])){
		$string=implode('',$string[0]);
		if(strlen($string)<$start+1){
			return '';
		}
		preg_match_all("/./su",$string,$ar);
		$string2='';
		$tstr='';
		//www.phpernote.com
		for($i=0;isset($ar[0][$i]);$i++){
			if(strlen($tstr)<$start){
				$tstr.=$ar[0][$i];
			}else{
				if(strlen($string2)<$length+strlen($ar[0][$i])){
					$string2.=$ar[0][$i];
				}else{
					break;
				}
			}
		}
		return $string==$string2?$string2:$string2.$ellipsis;
	}else{
		$string='';
	}
	return $string;
}

?>



前台模板调用:
<div id="slider">
{foreach $NewsRotaryView as $value}
    <div>
        <a lazyload="{$value.bpic}"></a>
       <p>{$value.title}</p>
    </div>
{/foreach} 
</div>



上面这个代码,能执行成功。

调用插件功能就报错了,写法如下:

<div id="slider">
{foreach $NewsRotaryView as $value}
    <div>
        <a lazyload="{$value.bpic}"></a>
       <p>{$value.title|truncate_cn:'5'}</p>
    </div>
{/foreach} 
</div>



直接运行报错:如下图:

求指点。

展开
收起
爱吃鱼的程序员 2020-06-22 13:31:52 599 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <divclass="ref">

    引用来自“多萨穆大叔”的答案

    <divclass="ref_body">经过测试,按照你的代码,没有问题。回复<aclass='referer'target='_blank'>@多萨穆大叔:太感谢了,原因:$this->setPluginsDir(array(SMARTY_PLUGINS_DIR,dirname( FILE).'/plugins/'));配置文件里面,这个没写。http://topphp.org/smarty-inherited-settings.html有没有知道,指点下啊,那里出了问题啊。看起来有点像插件文件夹没有读的权限你写的这个插件,smarty不认识是还需要其他地方,配置吗?经过测试,按照你的代码,没有问题。哇咔咔--,您看出来啥了??

    2020-06-22 13:32:09
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载