Search code examples
phprubyliquidhtmlspecialchars

Liquid equivalent of 'htmlspecialchars'


I'm trying to escape the input fields in a standard HTML form while using the Liquid templating engine in Octopress.

<input type="hidden" name="post_title" value="{{page.title}}" />

What is the Ruby/Liquid equivalent to PHP's htmlspecialchars? Is there already a filter that does this, or do I need to use a custom replace?


Solution

  • It seems that the standard escape filter will do the trick.

    <input type="hidden" name="post_title" value="{{ page.title | escape }}" />

    Although the documentation on the filter is annoyingly non-helpful, checking the code, it uses CGI.escapeHTML which has a much better documentation page and seems to escape the same characters as the PHP equivalent.

    Note that it only escapes double quotes, so single quote escaping will need to be done manually if needed.