1

Apologies if this has been asked before, I can only find questions dealing with the reverse scenario (i.e. replacing special characters with html entities)

I need to output special characters as part of a JavaScript array using Craft. Currently the values are output with all special characters replaced by their html equivalent. (e.g.: ampersands are replaced by &amp )

I have tried restoring the special characters using craft replace but without success.

The following code works to replace text, but does not, for example, replace &amp with &, or rather perhaps it does not distinguish these values as distinct.

<script>

    var myVariable = [{% for value in myArray %}"{{ value|replace('raw-html-entity', 'special-character') }}"{%- if not loop.last -%},{% endif %}{% endfor %}];

</script>

This code is being used for an array which is used for jQuery autocomplete values, so raw html is not appropriate.

Any suggestions appreciated.

1 Answer 1

2

I have a Craft Helpers plugin on GitHub, which has a htmlEntityDecode Twig filter.

{{ value|htmlEntityDecode}}
3
  • 1
    Thanks, htmlEntityDecode did the trick. Didn't seem to work with the striptags filter by the way. Commented Jan 27, 2017 at 12:44
  • I up-voted but it wont display as I'm a noob. Commented Jan 27, 2017 at 12:44
  • Sorry, should have made clear that striptags would further get rid of HTML tags after you converted all your HTML entities. Seems like you have non, as I could have already guessed from your question. Will get rid of it in the answer. Oh and thanks for the up-vote anyway :)
    – carlcs
    Commented Jan 27, 2017 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.