Sag Rising

Notes to Myself

Lessons in Habari Excerpts

Posted by Richard Cockrum on January 29, 2008


Yesterday I posted a bug report for Habari about it's formatting of post excerpts. I followed the directions in the wiki to create an excerpt, then added the code to my home page template.


What I expected was an excerpt that would end after either 60 words or one paragraph, whichever came first. What I got was an excerpt that ended after 60 words and had all paragraph breaks stripped. In writing posts I always use two line breaks to denote paragraph breaks. If I substituted a <--more--> tag, or actual paragraph tags in my posts, the excerpt was created properly, but not when two line breaks were used for creating paragraphs.


According to the wiki page referenced above all you need to do to get an excerpt is put this code in your theme.php file

Format::apply_with_hook_params( 'more', 
                'post_content_excerpt', 'more', 100, 1 );


This function is called when you place this in your template file

$post->content_excerpt;


The first 'more' tells what function in the Habari's Format class to call, 'post_content_excerpt' tells how to call the function in your template, the second 'more' tells what text to use to label the link to the full post in the excerpt, the 100 tells the maximum number of words, and the 1 tells the maximum number of paragraphs. These last four parameters can be changed to whatever you want.


For example

Format::apply_with_hook_params( 'more', 
                    'post_content_summary', 'Read more', 60, 2 );


would be called like this

$post->content_summary;


and would output an excerpt containing either 60 words or 2 paragraphs, whichever came first.


Thanks to the assistance of Michael Bishop, I found out this isn't the correct way to procede. You first need to call Format::apply, then Format::apply_with_hook_params, so you end up with

Format::apply( 'autop', 'post_content_excerpt' ); 
Format::apply_with_hook_params( 'more', 
                'post_content_excerpt', 'more', 100, 1 ); 


The first argument to Format::apply, 'autop', tells Habari to treat all paragraphs created by two line breaks as paragraph tags. After this filtering, the actual excerpt is created. I made this change, and all excerpts were created properly, whether paragraphs were created with two line breaks or actual paragraph tags, or the end of the excerpt was shown by a tag. Life is good. :)


Note: The wiki has also been corrected to make the directions for creating an excerpt more clear.

This entry is filed under and . You can follow any responses to this entry through the feed . New comments are currently closed.

1 Response to Lessons in Habari Excerpts

What do you think?
Comments for this post are disabled.