Yii framework extension to replace context of text with whatever you like.
Description and use cases ¶
You can use this extension to replaces 'standar' parts for a string to whatever you like. I will describe my personal use case. In my blog I used to add posts with title like:
- {yii}{ext} EReadmore
- {yii}{wiki} A simple...
- {php} New to ...
This was to categorize the Posts. So one day i wanted to replace all this with some icons. The code below does this work for me. Other use cases can be to display emoticons in text, tags, etc.
Code example ¶
Notice that there is not constrain to use a key=>value array as data.
What $data will be depends entirely to you and its related to who you will access that data in the replace
eval phrase.
$this->beginWidget('ext.EReplacer', array(
'bag'=>'{}',
'data'=>array(
'yii'=>'http://image src...',
'wiki'=>'http:///image src...',
'php'=>'http://image src...',
),
'replace'=>'"<img src=\'".$data[$el]."\' />";',
));
echo $post->title;
$this->endWidget();
You can as well use the text property like:
$this->widget('ext.EReplacer', array(
'bag'=>'{}',
'text'=>$post->title,
'data'=>array(
'yii'=>'http://image src...',
'wiki'=>'http:///image src...',
'php'=>'http://image src...',
),
'replace'=>'"<img src=\'".$data[$el]."\' />";',
));
Thanks ¶
I would like to thank rawtaz, SJFriedl, ciss & robert___ for the regexp help.
documentation
Demo site and documentation will be created soon. Thanks in advanced.
Can't use [] as bag
I just stumble upon the problem using '[]' as bag. But of course '{}' is working. Overall it is a great extension :)
Check documentation
@junxiong If you noticed at the $bag comment in the file it says " ...if you want to use regexp special chars be sure you escape them." What this means is that such a problem was expected. Please try
... 'bag'=>'\[\]', ...
Thanks for the comment
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.