function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JoyDJoyD 

Rendering HTML in a VisualForce IF statement

This should be super-simple, I'm just not seeing how to fix this.  I'd like to output HTML (a simple <br>) IF a certain condition is made.  Here is a snippet of my code:

 

<apex:outputField value="{!facility.Facility_Address_1__c}" />
{! IF(facility.Facility_Address_2__c!='','<br/>','NOPE')}
<apex:outputField value="{!facility.Facility_Address_2__c}" />

 

Basically, just want to output the first line of the address, if there is a second line to the address, add a new line break, then output the second line of the address.  Instead of a new line break, all I get is straight text, "<br/>" is stuck between the two address lines.

I also tried using br() and HTMLENCODE to no avail...even tried moving the text concatenation to the controller...I'm so lost...

Best Answer chosen by Admin (Salesforce Developers) 
Etherios DanEtherios Dan

Try using an Apex:Output tag to create a span with br tag. 

 

<apex:outputField value="{!facility.Facility_Address_2__c}" />
 <apex:outputPanel render="{!facility.Facility_Address_2__c!=''}"><br /></apex:outputPanel>

 

That might work better.

 

 

Dan Grandquist
Etherios - Technical Architect

Dan GrandquistEtherios - Technical Architect

 

 

All Answers

Etherios DanEtherios Dan

Try using an Apex:Output tag to create a span with br tag. 

 

<apex:outputField value="{!facility.Facility_Address_2__c}" />
 <apex:outputPanel render="{!facility.Facility_Address_2__c!=''}"><br /></apex:outputPanel>

 

That might work better.

 

 

Dan Grandquist
Etherios - Technical Architect

Dan GrandquistEtherios - Technical Architect

 

 

This was selected as the best answer
RamboRambo

Try using this...

<apex:outputField value="{!facility.Facility_Address_1__c}"/><br/>
<apex:outputField value="{!facility.Facility_Address_2__c}" />
JoyDJoyD

Thanks Dan, that worked.  But it seems like there might be a better solution if I need to output HTML, not text, in the future...right?  I think eventually I'll need to understand how to make some HTML code from the controller...maybe?

 

Rambo, not sure you understood what I was getting at...that's what I had originally, but when there isn't a second address line (which quite frequently happens), I want the address to 'shrink up' and therefore don't need that <br/>...

Etherios DanEtherios Dan

As a best practice I try to avoid getting HTML markup from the controller. Now there are some times when you can't avoid it, like when you are using a web callout. But in those cases you should try to make those areas of the page well defined div or OutputField tags.

 

The real solution here depends on you layout:

 

In Salesforce Style layouts (those that use the Salesforce UI) - I use blank apex:pageBlockSectionItem with a render to line up UI components. 

 

In Non-Salesforce Style layouts (those that need to look like the company Web site) - I use CSS to solve the problem, formatting the page div to line up correctly 

 

Having coded HTML for a while, one thing I had to learn in this framework is to use the Salesforce tags to render the HTML I wanted and not try to fight the framework.

 

Thanks for the Solve,

-Dan

JoyDJoyD

Thanks for the guidance.  I'm still learning what to put where and thought in the future I might need to work some more complex logic through the controller, and therefore pass HTML to the VF page.  But I guess maybe that is not necessary and I can get the logic from the VF components.

kevoharakevohara

I know this is old, but cant you accomplish this using escape="false" in an outputText tag?

 

 

<apex:outputText escape="false" rendered="{!IF(facility.Facility_Address_2__c!='')}" value="<br />" />

 

 

Ven 777Ven 777

Yes escape="false" works well, I was able to use it as below -

 

<apex:outputText escape="false" rendered="{!dtl.attachments.size==1}" value="<a href={!URLFOR($Action.Attachment.Download, dtl.attachments[0].Id)} target=_blank>View Attachment</a>" />