Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Double HTML encoding


Go to End


2 Posts   1877 Views

Avatar
cmc

Community Member, 33 Posts

8 March 2012 at 9:57am

Basically my function is setting a Text string that has HTML entities encoded. When this is output on the public page the entities have been encoded again, so for example, & becomes & and ' becomes '

I've tried using html_entity_decode() and a custom function to decode the string prior to sending it to the template. I also tried changing the class from Text to HTMLText. Finally I tried .XML and .RAW in the template. All of those methods still produce double encoding.

Sorry if this has already been answered. I've spent several hours searching the SS Forum and Internet prior to posting here.

Here is the relevant code snippet setting the string in the function:

$post_text = new Text('PostText');
$post_text->setValue($item->get_description());

$output->push(new ArrayData(array(
	'PostText' => $post_text
)));

Avatar
cmc

Community Member, 33 Posts

8 March 2012 at 1:19pm

In case anyone else runs into this problem, this is how I fixed it. The only changes are in the first two lines of the original code.

$post_text = new HTMLText('PostText');
$post_text->setValue(htmlspecialchars_decode($item->get_description()));