Core:Format Class

From Habari Project

(Redirected from Classes/Format)
Jump to: navigation, search

Contents


Description

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]); ?>

or

<?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.

Example

These formatters are extracted from default k2 theme:

<?php
// 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.

Example
<?php
Format::apply('autop','post_content_out');
?>

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").
Example
<?php
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.

Example
<?php
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.

Example
<?php
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)
Example
<?php
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.

Example
<?php
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.

Example

<?php
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);
		endif;
 
		return $url;
	}
?>

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

 <?php
Format::apply('cleanUrl', 'comment_url_out');
?>

This would turn the address http: //www.example.com/ to example.com.


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