Configuring Typogrify

There's no configuration necessary for Typogrify, but it does provide a config.php file that you can use to tweak its behavior to your heart's content.

If you want to change Typogrify's default settings, copy the src/config.php to craft/config as typogrify.php and make your changes there.

Once copied to craft/config, this file will be multi-environment aware as well, so you can have different settings groups for each environment, just as you do for general.php.

Using Typogrify

Typogrify provides two filters for prettifying your text:

typogrify

Uses php-typography to apply typographic best practices to text, rich text, and HTML.

Usage:

{{ content |typogrify }}

Or:

{{ typogrify(content) }}

Or:

{% filter typogrify %}
<p>Your text here</p>
{% endfilter %}

Or:

{{ craft.typogrify.typogrify(content) }}

There is also an optional second parameter isTitle (which defaults to false) to give Typogrify a hint as to whether the entity that is being passed in is a title or not. This allows it to not hyphenate titles, for instance, depending on the set_hyphenate_headings config setting (see below). Examples:

...and more. If you don't like the default behavior, you can enable, disable, or change any of the settings via the config.php file. See the Configuring Typogrify section for details.

For RSS feeds, there is an additional typogrifyFeed filter/function that applies all of the same typogrify treatments, but in a way that is appropriate for RSS (or similar) feeds — i.e. excluding processes that may cause issues in contexts with limited character set intelligence.

Usage:

{{ content |typogrifyFeed }}

Or:

{{ typogrifyFeed(content) }}

Or:

{% filter typogrifyFeed %}
<p>Your text here</p>
{% endfilter %}

Or:

{{ craft.typogrify.typogrifyFeed(content) }}

Security

In ordered to work, Typogrify outputs raw HTML. Any untrusted string coming from user input, etc. should be escaped before passing it into Typogrify, e.g.:

{{ craft.request.getParam('untrusted') | escape | typogrify }}

Advanced Usage

Should you need advanced control over Typogrify in your templates, you can use the getPhpTypographySettings() Twig function:

{% set phpTypographySettings = getPhpTypographySettings() %}

This returns a \PHP_Typography\Settings object to your templates that Typogrify uses to do its thing. Then you can do advanced or dynamic configuration changes such as:

smartypants

The smartypants filter is provided primary to make upgrading sites from Craft 2.x to Craft 3.x easier, if you were using the Craft 2.x Tyopgrify plugin already. The typogrify filter does everything that the smartypants filter can do, and more.

Usage:

{{ content |smartypants }}

Or:

{% filter smartypants %}
<p>Your text here</p>
{% endfilter %}

Or:

{{ craft.typogrify.smartypants(content) }}

Text Manipulation

Typogrify provides a several text manipulation functions, and will also give you a full Stringy object in your templates if you have advanced string manipulation needs.

{{ someString |striptags |truncate(20) }}

truncate - Truncates a string to a given length in a multi-byte friendly way. It first strips any HTML tags from the string before performing truncation.

Usage:

{{ someString |truncate(20) }}

Or:

{{ craft.typogrify.truncate(someString, 20) }}

truncate also accepts an optional parameter that will be appended to the string if it is truncated (this defaults to '…'):

{{ truncate(someString, 20, '-') }}

truncateOnWord - Truncates a string to a given length in a multi-byte friendly way, while ensuring that it does not split words. It first strips any HTML tags from the string before performing truncation.

Usage:

{{ someString |truncateOnWord(20) }}

Or:

{{ craft.typogrify.truncateOnWord(someString, 20) }}

truncateOnWord also accepts an optional parameter that will be appended to the string if it is truncated (this defaults to '…'):

{{ truncate(truncateOnWord, 20, '-') }}

wordLimit - Truncates a string by the number of words to a given length. It first strips any HTML tags from the string before performing truncation.

Usage:

{{ someString |wordLimit(20) }}

Or:

{{ craft.typogrify.wordLimit(someString, 20) }}

wordLimit also accepts an optional parameter that will be appended to the string if it is truncated (this defaults to '…'):

{{ wordLimit(someString, 20, '-') }}

stringy - Returns a new Stringy object to your templates, so you can access all of the advanced string manipulation that Stringy has to offer.

It does not strip HTML tags from the string, if you wish to do that, you can use the Twig striptags filter.