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!

  • Why not use an apex variable? – Sergej Utko Nov 21 '13 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. – Phil Hawthorn Nov 21 '13 at 19:56
3

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

panel.expressions.rendered = '{!oppt.StageName=="Closed Won"}';
  • Awesome, thanks so much @sfdcfox - I knew about setting expressions.value but not other attributes such as rendered. Thanks again. – Phil Hawthorn Nov 21 '13 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 Nov 21 '13 at 21:22
  • Cool...don't suppose you happen to know if by chance you can access those properties dynamically too? – Phil Hawthorn Nov 21 '13 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 Nov 21 '13 at 22:11
  • 2
    No, unfortunately, it's not that expressive. You have to use hard references to the attributes. – sfdcfox Nov 21 '13 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.