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
Rafael.Martins.SantosRafael.Martins.Santos 

is it possible to create a tag in visualforce using APEX code?

Hi,

I want know if is possible to create a tag using apex code.

for example

List<String> accounts

accounts.add(<tr><td>account.Field</td></tr>)

When the code is executed in the visualforce will appear
<table>
//TR and TD created by apex
<tr>
<td>Account Value</td>
</tr>
</table>

is it possible to do?

Best Regards
Rafael
Shruti SShruti S
Yes, it is possible to create tags in Visualforce using Apex code. For that, you can define your tag in a String variable in Apex and then render that variable in an Output Text tag in the Visualforce page.
Here is an example - 

Apex Code
public class SampleController {
    public String userName { get; set; }
    
    public SampleController() {
        userName =  '<table border="1"><tr><td>' + UserInfo.getFirstName() + '</td></tr></table>';
    }
}
Markup
<apex:page controller="SampleController">
    <apex:outputText value="{!userName}" escape="false">
        
    </apex:outputText>
</apex:page>
Also please note that you will have to do an escape=false , then only the text will be rendered as HTML tags.
 
Rafael.Martins.SantosRafael.Martins.Santos
Hi Shruti
is it work if I use a variable of type list?
The point is I must print each register separated by tags
 
Shruti SShruti S
I am not sure if I understood you well. Could you please tell me what you meant by each register seperated by tags?
Rafael.Martins.SantosRafael.Martins.Santos
I need display a list in visualforce.
But when I display the list, all the data fo account for example, are displayed all in only one cell.
So I need separate each data with TR and TD.

When I use your example with a simple variable String name, it works, but when I use the variable like LIST<String> name it occurs the follow error:
Failure Message: "System.NullPointerException: Attempt to de-reference a null object.