Core:Format Class

From Habari Project

Jump to: navigation, search



This class is primarly used to format output of core and plugins. Theme developers can use it in the "theme.php" file.

How to use?

This class can be used in two ways:

<?php Format::apply($action,$target[,$params]); ?>


<?php Format::apply_with_hook_params($action,$target[,$params]); ?>

The parameter are the following:

  • $action (mandatory): the name of the formatter to apply.
  • $target (mandatory): the value on which the parameter has to be applied. It's written in the following form: for the variable "$post->content_out", write "post_content_out".
  • $params (optional): extra parameters to be passed to the formatter.


These formatters are extracted from default k2 theme:

// Apply Format::autop() to post content...
Format::apply( 'autop', 'post_content_out' );
// Apply Format::autop() to comment content...
Format::apply( 'autop', 'comment_content_out' );
// Apply Format::tag_and_list() to post tags...
Format::apply( 'tag_and_list', 'post_tags_out' );
// Apply Format::nice_date() to post date...
Format::apply( 'nice_date', 'post_pubdate_out', 'F j, Y @ g:ia' );
// Apply Format::nice_date() to comment date...
Format::apply( 'nice_date', 'comment_date', 'F j, Y @ g:ia' );

Default formatters

All the formatters described below are available in plugins and themes. They are parts of the Habari core.

Formatted content (autop)

This formatter converts non-HTML paragraphs separated with 2 line breaks into HTML paragraphs while preserving any internal HTML.


Formatted tags list (tag_and_list)

Turns an array of tag names into an HTML-linked list with command and an "and".

Optional parameters:

  • between: Text to put between each element (default ",").
  • paragraphs: Text to put between the next to last element and the last element (default "and").
Format::apply( 'tag_and_list', 'post_tags_out', ',', 'and');

Formatted date (nice_date)

Formats a date using a date format string. As dates are returned as a timestamp by Habari core, you can use the parameters from the PHP Date function to format it.

Format::apply('nice_date', 'post_pubdate_out', 'F j, Y @ g:ia' );

Formatted time (nice_time)

Formats a time using a date format string. As times are returned as a timestamp by Habari core, you can use the parameters from the PHP Date function to format it.

Format::apply('nice_time', 'post_pubdate_out', 'F j, Y @ g:ia' );

Summary of text (summarize)

Returns a shortened version of whatever is passed in.

Optional parameters:

  • count: maximum number of words to display (default 100)
  • paragraphs: maximum number of paragraphs to display (default 1)
Format::apply('summarize ', 'post_content_out', 100, 1);

Page breaking (more)

Returns a truncated version of post content when the post isn't being displayed on its own. Posts are split either at the comment or at the specified maximums. Use only after applying autop or other paragrpah styling methods.

Format::apply_with_hook_params( 'more', 'post_content_out' );

Create your own formatter

To create a new formatter, all one has to do is extend the Format class. From there, create a new method of that class.


class myFormat extends Format {
	public static function cleanUrl($url) {
		$url = str_replace('http://', '', $url);
		$url = str_replace('https://', '', $url);
		$url = str_replace('www.', '', $url);
                //remove the / at the end of sites //
	        if(substr($url, -1) == '/'):
	            $url= substr($url, 0, -1);
		return $url;

The previous code can be placed in theme.php for example, or even a plugin. It can be called by something like:

Format::apply('cleanUrl', 'comment_url_out');

This would turn the address http: // to

This page describes a PHP class that is in the Habari software. For more comprehensive technical information visit the API Documentation.
Other Class Pages
Personal tools