4

I am considering using Dynamic Visualforce components for a project, however I have hit a potential showstopper with my approach.

If you consider the following (valid) Visualforce:

<apex:outputPanel rendered='{!oppt.StageName=="Closed Won"}'>
<!-- some content -->
</apex:outputPanel>

I would like to replicate that in Apex as follows:

Component.Apex.OutputPanel panel = new Component.Apex.OutputPanel();
panel.rendered = '{!oppt.StageName=="Closed Won"}';

However, the compiler rightfully complains that rendered is a Boolean and I'm trying to assing a String.

I guess its probably a long shot, but does anyone know if there is anyway of achieving this?

Thanks!

2
  • Why not use an apex variable? Commented Nov 21, 2013 at 19:50
  • Because that would mean that I need to know at design time the condition which gives me true of false...which unfortunately I don't (I provided the above example to illustrate a point). I suspect what I'm trying to achieve is too dynamic but I wondered if I was missing a trick. Commented Nov 21, 2013 at 19:56

1 Answer 1

3

You can use the "expressions" attribute to assign an expression such as rendered.

panel.expressions.rendered = '{!oppt.StageName=="Closed Won"}';
7
  • Awesome, thanks so much @sfdcfox - I knew about setting expressions.value but not other attributes such as rendered. Thanks again. Commented Nov 21, 2013 at 21:19
  • 1
    Any attribute that accepts an expression will be visible in expressions. This means that ID, for example, doesn't have an expression, but ones like URL, headerClass, headerValue, etc, do.
    – sfdcfox
    Commented Nov 21, 2013 at 21:22
  • Cool...don't suppose you happen to know if by chance you can access those properties dynamically too? Commented Nov 21, 2013 at 21:24
  • I just experimented with this, and yes, it works immediately if you read the value. For example, I was able to get this to work: Component.Apex.outputText text = new Component.apex.outputText(); text.expressions.rendered = '{!1=2}'; System.assertEquals(false, text.rendered);
    – sfdcfox
    Commented Nov 21, 2013 at 22:11
  • 2
    No, unfortunately, it's not that expressive. You have to use hard references to the attributes.
    – sfdcfox
    Commented Nov 21, 2013 at 22:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .