Avoid stripping of whitespace around outputText elements

I find it annoying that in VisualForce if you do something like this:

Campaign name is <apex:outputText value="{!Campaign.Name}" />

You get output like this:

Campaign name isCampaign 1

The whitespace for some reason gets stripped out before the component.

I usually am (lazy?) and address it by doing this:

Campaign name is&nbsp;<apex:outputText value="{!Campaign.Name}" />

But this looks a little dirty, plus depending on CSS and browser you may get a different width for the nbsp element than a “normal” space.

Is there a cleaner way to avoid this behavior that I’m missing? I know I could be putting the text all inside the outputText like so:

<apex:outputText value="Campaign name is {!Campaign.Name}" />

But I find this harder to parse as a developer and it looks odd to me, especially for very long strings.

Is there another way to avoid this issue that you prefer? Stopping the “trim whitespace around VF component” behaviour would be ideal but I don’t believe that’s possible.

Answer

I’ve just realized, that all this examples give desired result:

Test: <apex:outputText value="{!' '+acc.Name}" />
Test: <apex:outputText value=" {!acc.Name}" />
Test: {!acc.Name}

Not sure we can do something on the visualforce side or css 🙁

Attribution
Source : Link , Question Author : jkraybill , Answer Author : Sergej Utko

Leave a Comment