I have an custom object called Events__c, which returns a image banner link stored in a URL field. I'm using this data to display the banner in a Visualforce page during a registration window, based on date boundaries.
My controller has a getter method that constructs the banner in form of clickable link with an embedded image and returns to the VF page. I'm using an outputText call in the VF page with escape="false" attribute to render the banner as HTML.
Controller getter
public String eventBanner {
get {
// Today
Date today = Date.today();
String banner = '';
// Get upcoming events
List<Event__c> lstEvent = [
SELECT
Id,
Registration_Start__c,
Registration_End__c,
Registration_Banner__c
FROM
Event__c
WHERE
( Registration_Start__c <= :today AND Registration_End__c >= :today )
];
// Results returned
if ( lstEvent.size() > 0 ) {
for ( Event__c mc : lstEvent ) {
// Registration
if ( mc.Registration_Start__c <= today &&
mc.Registration_End__c >= today &&
mc.Registration_Banner__c != null ) {
banner = '<a href="/Register?Id=' +
mc.Id +
'">' +
'<img src="' +
mc.Registration_Banner__c +
'"/></a>';
} // if
} // for
} // if
// Return
return banner;
}
}
VF page
<apex:outputText value="{!eventBanner}"
escape="false"/>
All of this is working fine. My issue is with the block of code I'm using to construct the banner HTML.
banner = '<a href="/Register?Id=' +
mc.Id +
'">' +
'<img src="' +
mc.Registration_Banner__c +
'"/></a>';
To me this is a very crude & inefficient approach at crafting the HTML block by string concatenation. Are there any supporting Apex classes that allow you to take a more elegant approach?
Thanks.