+ Start a Discussion
infowelders_nsinfowelders_ns 

Return string containing HTML / Apex tags is being safe-encoded

I am trying to return a string with Apex tags, but the pointy brackets (<, > ) are being encoded to &lt; and &gt;.


    Example:
    public String getNavString() {
        return '<apex: outputLink value="http://www.google.com/">Google</apex: outputLink>';
    }


    Resulting HTML:
    &lt;apex: outputLink value="http://www.google.com/"&gt;Google&lt;/apex: outputLink&gt;

    Expected HTML:
    <apex: outputLink value="http://www.google.com/">Google</apex: outputLink>


  

Is there a way to make sure that the tag characters are not encoded?

Thank you!

~ Nick

Message Edited by infowelders_ns on 08-27-2009 12:23 PM
paul-lmipaul-lmi
read the visualforce developer guide.  most tags that return text can be overridden not to encode HTML chars.
infowelders_nsinfowelders_ns

I've found four encoding functions (HTMLENCODE, JSENCODE, JSINHTMLENCODE, and URLENCODE), but no decoding functions.

 

I don't know if this helps, but this is how I am referencing the variable in the Visualforce page:

 

    <apex:repeat value="{!NavString" var="navstring">

        {!navstring}

    </apex:repeat>

 

There are more tags surrounding it, of course, but that's the relevant section.

 

~ Nick

paul-lmipaul-lmi
<apex:outputText value="{!navstring}" escape="false" />
XactiumBenXactiumBen
I'm waiting on a method that doesn't require escape="false".  When packaging an app and putting on the app exchange the Salesforce Review people frown upon this method.
infowelders_nsinfowelders_ns

The escape="false" does not work here, unfortunately.  I will try to give more details into the structure.

 

Apex Controller:

public with sharing class web_controller_test {

public String getNavString() {
return '<apex:outputLink value="http://www.google.com/">Google</apex:outputLink>';
}

}

 

Force.com Page:

 

<apex:page controller="web_controller_test" standardStyleSheets="false" sidebar="false" showHeader="false" title="Test Page">

<apex:outputPanel id="subnav">
<apex:repeat value="{!NavString}" var="navstring">
{!navstring}
</apex:repeat>
</apex:outputPanel>

</apex:page>

 

Now, this is a bit simplified, but I think this combination should work after a direct copy and paste.  The actual logic behind constructing the navigation string is a bit more complicated, involving a SOQL query, a loop, and some text parsing.  The end output we are aiming for is this:

 

Desired Output Code:

 

<div id="container_subnav">
<a href="partners_detail?id=123456789012345678">Company 1</a> |
<a href="partners_detail?id=123456789012345678">Company 2</a> |
<a href="partners_detail?id=123456789012345678">Company 3</a> |
<a href="partners_detail?id=123456789012345678" class="current">Company 4</a> |
<a href="partners_detail?id=123456789012345678">Company 5</a> |
<a href="partners_detail?id=123456789012345678">Company 6</a> |
<a href="partners_detail?id=123456789012345678">Company 7</a>
</div>

 

Desired Output Rendered Display:

 


    Company 1 | Company 2 | Company 3 | Company 4 | Company 5 | Company 6 | Company 7



 

 

Now, I would have looped through a list in a repeat section, like this:

 

Close Solution:

 

<apex:page controller="web_controller_test" standardStyleSheets="false" sidebar="false" showHeader="false" title="Test Page">

<apex:outputPanel id="subnav">
<apex:repeat value="{!NavList}" var="navlist">
<a href="{!navlist.website}">{!navlist.name}</a>
</apex:repeat>
</apex:outputPanel>

</apex:page>

 

... except that I can't specify class="current" for the page that the user is currently on, and the appropriate link will not be assigned the current style (bold, underlined, red).  I need the apex to handle the logic behind that.  Which, btw, the code in the apex class works just fine, and constructs the html segment perfectly well.  Except when it is returned to the Force.com page, it escapes the characters.  :-/

 

Adding escape="false" to the output string inside of the Apex code does not work, because it just treats it as part of the string that it is returning.

 

><

 

~ Nick

Message Edited by infowelders_ns on 08-28-2009 11:51 AM
d3developerd3developer

Nice to know someone is having the same problem. Escape = "false" is also not working for me, even though I'm doing nothing more complicated than including a BR tag in my output.

 

Not sure why...

oswaldmehoffoswaldmehoff

Bumping this.

 

I need to create a PDF in Visualforce.

 

The PDF will have a lot of tables that need to change in structure dynamically based upon whats in them.

 

I need to leverage APEX to build the table structure, but it seems that you can't get APEX to spit out HTML code.

 

It just shows up in the PDF as the code "<table><tr>..."and not the actual table.

hm.klemenchm.klemenc

hello,

have you already found a solution on this issue? is there a way to make apex create html for a pdf rendered page?

thanks

hans