用clearsilver宏实现的replace

RT,用clearsilver宏实现的replace,缺点是宏不能嵌套,类似只能调用一次的函数,cs这个东东,需要扩展的东西太多了。

<?cs def:replace(val, f, t) ?>
<?cs set:index=string.find(val, f)?>

<?cs if:index>-1 ?>
<?cs set:subLen=string.length(f) ?>
<?cs set:begin=string.slice(val, 0, index) ?>

<?cs set:end=string.slice(val, 0+subLen+index, string.length(val))?>
<?cs set:val=begin+t+end?>

<?cs if:string.find(val, f)>-1 ?>
<?cs call:replace(val, f, t)?>
<?cs else?>
<?cs var:val?>
<?cs /if?>
<?cs else?>
<?cs var:val?>
<?cs /if?>
<?cs /def?>

<?cs def:strWarp2br(val)?>
<?cs if:string.find(val, "\\r\\n")>-1?>
<?cs call:replace(val, "\r\n", "x<br/>") ?>
<?cs elif:string.find(val, "\\n")>-1?>
<?cs call:replace(val, "\n", "y<br/>") ?>
<?cs elif:string.find(val, "\\r")>-1?>
<?cs call:replace(val, "\r", "z<br/>") ?>
<?cs else?>
<?cs var:val?>
<?cs /if?>
<?cs /def?>

 

posted @ 2011-11-04 10:57  sparks345  阅读(399)  评论(0编辑  收藏  举报