Timber: post.content (and all other variables) should not return HTML strings, only text

Created on 26 Jan 2018  路  3Comments  路  Source: timber/timber

The post.content variable returns an HTML string. Based on the principles of Timber, I think that Timber should never return HTML strings, because HTML must be managed by the web designer through the Twig templates. In this case the web designer has no control over HTML elements. I would like all Timber variables to return only text strings (eg URLs, classes, etc.).

Expected behavior

Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)

Actual behavior

Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)

What version of WordPress, PHP and Timber are you using?

WordPress 4.9.2, Timber 1.6.0

How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)

WP.org

Most helpful comment

Since the content is managed by WordPress TinyMCE (currently and in future Gutenberg), so it is right that post.content returns HTML content as that is what is saved in the database the_content

Use post.content|striptags if you just want the string.

https://twig.symfony.com/doc/2.x/filters/striptags.html

All 3 comments

Since the content is managed by WordPress TinyMCE (currently and in future Gutenberg), so it is right that post.content returns HTML content as that is what is saved in the database the_content

Use post.content|striptags if you just want the string.

https://twig.symfony.com/doc/2.x/filters/striptags.html

Or you can use post.post_content - it will be RAW data without wpauto and other filters.

I agree with you that the cleanest way to use Twig would be if you wouldn鈥檛 have HTML in any variables. But in the context of WordPress, this is not really possible, especially with the post content. Many plugins use the post content to insert HTML through shortcodes or other methods. Look at WooCommerce, it displays a whole shop with cart and checkout process in post content. And up until now, there wasn鈥檛 really another way for plugins to insert HTML in a way that is compatible with most themes. Maybe we鈥檒l have more control with Gutenberg. We鈥檒l see soon.

Was this page helpful?
0 / 5 - 0 ratings