2

Is there a way in visualforce to output text from a controller that does NOT get rendered in a span tag?

I want to just output a text string from my controller inside an <h4> tag. When I use <apex:outputText value="{!someFieldValue}" /> it generates a <span> tag with my value inside it and I just want the text with no additional HTML generated.

3

It is not always required to bind field value with outputtext when you put anything inside {!} it render value directly on visualforce page so you can do that in following way and it will render value with h4 tag

<h4>{!someFieldValue}</h4>
  • 1
    Do you still get the automatic escaping outputText provides in this case? Isn't this a security vulnerability? – Adrian Larson Apr 26 '16 at 19:41
  • @AdrianLarson no it will not do automatic escaping but he has mentioned that he has only text string not html string so it will work. – Himanshu Apr 26 '16 at 19:45
  • Not so, he mentioned he wants to merge in someFieldValue. For all we know it could be rich text. – Adrian Larson Apr 26 '16 at 19:46
  • I want to just output a text string from my controller this is what initial question was. but your question will also help – Himanshu Apr 26 '16 at 19:49
2

If you still want to use the outputText tag without your content being wrapped in a <span>, you need to refrain from applying styles. From the documentation on apex:outputText:

apex:outputText
Displays text on a Visualforce page. You can customize the appearance of using CSS styles, in which case the generated text is wrapped in an HTML tag.

A simple page can be used to demonstrate this behavior.

<apex:page>
    <apex:outputText value="test 1" />
    <apex:outputText value="test 2" style="" />
    <apex:outputText value="test 3" styleClass="" />
</apex:page>

In the above, test 1 will just be a text node, while the latter two will be wrapped in a <span>.

You should continue to use outputText unless you are using a trusted input source and can verify it will not contain markup.

0

Use the expression syntax like the below:

"{!HTMLENCODE(controllerString)}"

this can mask the HTML Keywords in the string.. This way you can do your string manipulation..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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