5

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.

1

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 :(

2
  • The main difference is that the &nbsp prevents a line wrap which can be good or bad depending on the desired results. The actual amount of space it occupies shouldn't be any different than in your example Test 1 or Test 2 because of the way the browser will handle kerning of a blank space or what I believe is referred to as an "em" space (apologies if I've used the wrong term, it's been a while). You're absolutely correct that an <apex:outputText value= > tag isn't needed at all unless you need currency or other formatting for the object specified as a parameter.
    – crmprogdev
    Apr 5 '13 at 7:17
  • This is a really weird glitch. I'm sending emails and have fields displayed using apex:outputText, and some sandbox display a different result than others.
    – jpmonette
    Oct 23 '14 at 9:52

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.