Yii 1.1: xreadmore

A widget to shorten html content to a certain length, and give a link to read for the full text.


A widget to shorten html content to a certain length, and give a link to read for the full text.


Simple case

In the view add:

    $this->widget('ext.XReadMore.XReadMore', array(
    $this->widget('ext.XReadMore.XReadMore', array(

Other Uses

There are some other parameters that could be set. The following are the list of all parameters that could be set with the widget.

public $showLink = true; // Show link to go to full text page
public $model = null; // the data model, if set use model->attribute for long text, defaults to null
public $attribute = null; // the attribute
public $text = null; // the long text, model proprty should be null to use this
public $stripTags = true; // determines whether to return plain text or html text
public $maxChar = 400; // the maximal count of character before it is truncated
public $separator = " "; // the separator to use after maxChar is satisfied
public $linkHtmlOptions = array(); // link html options.
public $allowedTags = '';
public $charset = 'UTF-8';
public $linkText = 'Read more...'; // link label, default 'Read more...'.
public $linkUrl = null; // link url.
public $return = false; //if short version will be returned or echoed.

Thanks to Usui eitalian@mail.ru for adding selective tag and encoding support.

Total 6 comments

#12069 report it
Blue Spy at 2013/02/26 04:10am

@letop That's a nice idea. I did plan to make this not just to be a widget but also a filter, but since currently this works okay in my case, i might extend it more later. That's a good idea. Maybe if you have the resources and interested to extend this extension, I could set up a github account for this.

#12062 report it
le_top at 2013/02/25 05:11pm
Alternative method/suggestion

Hi Your extension reminded me of a technique I put in place so I described it here. Maybe it'll spark an idea to propose a formatter with your extension.

#11998 report it
Blue Spy at 2013/02/19 09:32pm
short html

@pmaselkowski I am not sure what you mean by cut html. In this case, I'll assume you meant by shortening html text. This extension just counts a certain length of characters and extracts the html. As for closing the tags, the result is then purified with html purify to fix broken tags. So far, it worked okay in my case.

#11995 report it
pmaselkowski at 2013/02/19 06:04pm

How does it cut HTML? Does it automatically close tags?

#11987 report it
Blue Spy at 2013/02/18 05:55pm

I am glad to know that this is helping someones else in their project :)

This extension actually uses a character too to do the split. In this case it uses a space character by default, but if you prefer a different character, you may set it with the separator property.

So, actually this extension searches for a character after a certain number of characters is met. Originally this is done so the split wouldn't happen in the middle of a word or something.

#11981 report it
bonnie at 2013/02/18 08:42am

Thanks for this. I had come up with same approach but mine looks for the period not number of characters. I will use it soon in my project.

Leave a comment

Please to leave your comment.

Create extension