Tag escaping

From Textpattern CMS 4.7.0 onwards, a global escape attribute is available which applies to all tags (core tags and plugin tags) that don’t treat it natively. Its value is a comma-separated list of ‘transforms’ that will be applied to the content in the given order. For example:

<txp:variable name="hello" escape="trim, textile">
    Hello, _world_!
</txp:variable>

<txp:variable name="hello" escape="html" />

Will output:

<p>Hello, <em>world</em>!</p>

Available escape values

escape="html" (default)
Encode special HTML characters ("'&<>).
escape="json"
JSON-encode, without the surrounding "" quotes.
escape="trim" (ltrim, rtrim)
Remove surrounding (left, right) spaces/newlines from the content.
escape="tidy"
Remove extra spaces/newlines from the content and trigger a more thorough processing mode.
escape="number"
Format the content as a number.
escape="tidy, number"
Remove spaces/newlines and format the content as a stricter number (force to float or fraction first).
escape="integer"
Format the content as an integer.
escape="tidy, integer"
Remove spaces/newlines and format the content as a stricter integer (ensure it adheres to an integer first).
escape="textile"
Textile the content.
escape="tidy, textile"
Remove spaces/newlines, prepend a single space to remove the surrounding <p> tag, then Textile the content.
escape="some-tag"
Strip any self-closing <some-tag />, or unwrap any container <some-tag>...</some-tag> in the content.
escape="tidy, some-tag"
Remove spaces/newlines, escape any regex characters, then strip any self-closing <some-tag />, or unwrap any container <some-tag>...</some-tag> in the content.

Examples

Example 1: Extract a number from a variable

<txp:variable name="amount" value="Price: £1 234.78" />

<txp:variable name="amount" escape="number" />
<txp:variable name="amount" escape="tidy, number" />

Outputs 0 without tidy (not a number) and 1234.78 with tidy.

Example 2: Format a variable with Textile

<txp:variable name="test">
*This*
is

a _test_
</txp:variable>

<txp:variable name="test" escape="textile" />
<txp:variable name="test" escape="tidy, textile" />

Outputs

<p><strong>This</strong><br />is</p>
<p>a <em>test</em></p>

without tidy and <strong>This</strong> is a <em>test</em> with tidy.

Example 3: Remove all images from Excerpt

<txp:excerpt escape="img" />

Example 4: Required JSON-LD to validate AMP

Create valid JSON-LD for the <head> section of an AMP page, by using escape="json" on various tags along with the smd_thumbnail plugin.

<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "BlogPosting",
    "headline": "<txp:title escape="json" />",
    "mainEntityOfPage": "<txp:permlink />",
    "datePublished": "<txp:posted format="iso8601" />",
    "dateModified": "<txp:modified format="iso8601" />",
    "description": "<txp:meta_description format="" escape="json" />",
    "author": {
      "@type": "Person",
      "name": "<txp:author escape="json" />"
    },
    "publisher": {
      "@type": "Organization",
      "name": "ExampleCorp",
      "logo": {
        "@type": "ImageObject",
        "url": "<txp:site_url />assets/img/amp-publisher-logo.png",
        "width": "320",
        "height": "60"
      }
    },
    "image": {
      "@type": "ImageObject",
      <txp:if_article_image><txp:images limit="1"><txp:smd_if_thumbnail type="amp-696w390h">"url": "<txp:smd_thumbnail type="amp-696w390h" display="url" />",<txp:else />"url": "<txp:site_url />assets/img/amp-article-image-fallback.png",</txp:smd_if_thumbnail></txp:images><txp:else />"url": "<txp:site_url />assets/img/amp-article-image-fallback.png",</txp:if_article_image>
      "width": "696",
      "height": "390"
    }
  }
</script>

Next: Short-tags and shortcodes

See something wrong in this document? Outdated info, a broken link, faulty code example, or whatever? Please write an issue and we’ll fix it.