ExpressionEngine

2.5.5 User Guide

Typography Class

Calling the Typography Class

The Typography class is used in ExpressionEngine to parse type, providing tools for automatic XHTML, auto line-breaking, email encoding, word censoring, decoding BBCode, syntax highlighting, and gateway access to formatting plugins.

To use the these features in your modules, you need to first instantiate the Typography Class

$this->EE->load->library('typography');
$this->EE->typography->initialize();

Note that after loading the Typography library you need to initialize it with $this->EE->load->typography->initialize(); or you will be inheriting the class properties of whatever code last used it.

Typography Class Properties

The Typography class has a number of class properties that you may wish to set before calling any methods. Below is a description of available class properties and their default values (in bold).

$allow_img_url

(string) [ y / **n* ]* — Allow inline images?

$allow_js_img_anchors

(bool) [ TRUE / **FALSE* ]* — Whether to allow JavaScript submitted within <a href> and <img> tags

$bounce

(string) — Used to construct redirect links, to prevent control panel URLs from showing up in referrer logs, and on the front-end for rank denial. This property is set dynamically based on site preferences and is not necessary to set directly.

$censored_replace

(string) — String that censored words are replaced with, taken from site preferences.

$censored_words

(array) — Array of words to be censored, taken from site preferences.

$code_chunks

(array) — Array of temporary markers and content used to prevent formatting from being applied to syntax highlighted code.

$code_counter

(int) — Used as keys of the $code_chunks array, to keep the temporary markers organized.

$convert_curly

(bool) [ **TRUE* / FALSE ]* — Convert curly brackets ( “{” and “}”) into entities?

$emoticon_path

(string) — The preference setting for the URL path to the site’s emoticons. This property is set dynamically based on site preferences and is not necessary to set directly.

$encode_email

(bool) [ **TRUE* / FALSE ]* — Whether or not email addresses are encoded.

$encode_type

(string) [ **javascript* / noscript ]* — Type of encoding applied to email addresses if email address encoding is enabled. “noscript” renders in a human readable format (e.g. “name at example dot com)”, suitable for use where JavaScript is inappropriate, such as in a feed.

$file_paths

(array) — Array of file upload directories in key (ids) => value (urls) pairs.

$highlight_code

(bool) [ **TRUE* / FALSE ]* — Perform PHP syntax highlighting on [pre] and [code] blocks?

$html_fmt_types

(array) [ **array(‘safe’, ‘all’, ‘none’)* ]* — Array of standard HTML handling types available to the Typography class.

$html_format

(string) [ **safe* / all / none ]* — Controls how HTML is handled in text.

$parse_images

(bool) [ **TRUE* / FALSE ]* — Whether or not {filedir_#} variables are to be parsed.

$parse_smileys

(bool) [ **TRUE* / FALSE ]* — Replace text smileys with smiley images?

$single_line_pgfs

(bool) [ **TRUE* / FALSE ]* — Whether to treat single lines as paragraphs in auto-XHTML

$site_index

(string) — Deprecated.

$smiley_array

(mixed) [ **FALSE* ]* — If emoticons are enabled for the site, this property will contain an array of smiley conversions in key (smiley) => value (image) pairs. If emoticons are not enabled, this will be set to FALSE.

$text_fmt_plugins

(array) — Array of available installed plugins.

$text_fmt_types

(array) [ **array(‘xhtml’, ‘br’, ‘none’, ‘lite’)* ]* — Array of standard formatting types available to the Typography class.

$text_format

(string) [ **xhtml* / br / none / lite ]* — Controls what formatting is applied to text.

$use_span_tags

(bool) [ **TRUE* / FALSE ]* — Use <span> tags for font color and size BBCode? Setting to FALSE uses <font> tags.

$word_censor

(bool) [ **FALSE* ]* — Whether or not word censoring is applied. This property is set dynamically based on site preferences and is not necessary to set directly.

$yes_no_syntax

(array) [ **array(‘y’, ‘n’)* ]* — Array of valid Yes / No strings for use in properties. Used to ensure that valid settings are being provided for a Yes / No type preference.

Parsing Type

str $this->EE->typography->parse_type ( str $str, array $prefs )

This function returns a string of parsed type. It is the most common use of the Typography class, and many of the individual functions also described in this document are used within the parse_type() method. The format the string is returned in is determined by both the class properties and the array of properties provided in the second argument.:

$str = $this->EE->typography->parse_type($str);

Example of Parsing Type with Preferences

You may override class properties directly in the $prefs array for the following:

  • text_format
  • html_format
  • auto_links
  • allow_img_url
$prefs = array(
        'text_format'   => 'xhtml',
        'html_format'   => 'all',
        'auto_links'    => 'y',
        'allow_img_url' => 'y'
        );

$str = $this->EE->typography->parse_type($str, $prefs);

Using a Plugin for Text Formatting

Any installed formatting plugin may be used to parse type. Simply use the class name of the plugin, in lowercase letters.

$str = $this->EE->typography->parse_type($str, array('text_format' => 'markdown'));

If you attempt to use a plugin that is not installed, no text formatting will be performed. It may be wise to check for the existence of plugins before using them, so if they are not installed, you can fall back on one of the native formatting types.

$text_format = (in_array('markdown', $this->EE->typography->text_fmt_plugins)) ? 'markdown' : 'xhtml';
$str = $this->EE->typography->parse_type($str, array('text_format' => $text_format));

Encode Email Addresses

str $this->EE->typography->encode_email ( str $email, str $title, bool $anchor )

This function encodes email addresses with Javascript, to assist in prevention of email harvesting by bots.:

$str = "brett.bretterson@example.com";
$str = $this->EE->typography->encode_email($str, "Email Brett Bretterson");

$email

(string) — Email address. Required

$title

(string) [ **empty string* ]* — The text to use as the title of the email link.

$anchor

(bool) [ **TRUE* / FALSE ]* — Whether or not a clickable link is created for the email address.

If you want to return a human readable “encoded” email address instead, you can also set the $encode_type class property to “noscript”.

$str = "brett.bretterson@example.com";
$this->EE->typography->encode_type = "noscript";
$str = $this->EE->typography->encode_email($str, '', FALSE);

Returns:

brett dot bretterson at example dot com

Auto (XTHML) Typography

str $this->EE->typography->auto_typography ( str $str )

This function takes a string of text and returns typographically correct XHTML.:

   $str = $this->EE->typography->auto_typography($str);

Its primary modifications are:
  • It turns double spaces into paragraphs.
  • It adds line breaks where there are single spaces.
  • It turns single and double quotes into curly quotes.
  • It turns three dots into ellipsis.
  • It turns double dashes into em-dashes.

$str

(string) Text to apply XHTML typography to

Formatting Characters for XHTML (“Light” Typography)

str $this->EE->typography->format_characters ( str $str )

This function performs the character transformation portion of the XHTML typography only, i.e. curly quotes, ellipsis, ampersand, etc.:

$str = $this->EE->typography->format_characters($str);

$str

(string) Text to apply character formatting to