1

I am currently very stuck on some logic concerning a visualforce page and it's controller.

Say I have the following code (I don't have any example code because I am stuck without any idea of how to proceed with this problem):

<apex:repeat value="{!some-record-list}" var="record">
    <div style="{!IF(CONDITION_A, '', 'none')}">
        <!-- Lots of code here that processes various sObject fields on the record and displays various info, requires a lot of memory -->
    </div>
    <div style="{!IF(CONDITION_B, '', 'none')}">
        <!-- Different, but equally if not more memory requiring code here -->
    </div>
    <!-- undefined number of varying divs, not similar enough to make a component, but considerably similar -->
</apex:repeat>

It seems as if, even though some of the above divs are not rendered because of the conditioning, the memory requirement is equivalent to the memory requirement of all of the divs, instead of just the ones rendered.

My question is, how do I go about cleaning this up, so that only the code that fits the conditions appears in the page, so that the memory usage could be dramatically decreased?

I've thought about using apex:outputText escape="false"... However I don't think I can pass parameters from outputText to the controller, which is necessary because of the apex:repeat, I need to process specific information to the current record in the repeat. I also cannot store the current iteration's record into a page parameter or into a variable on the controller, because users will be able to manipulate any fields within the divs at any time.

So... Is there a way to make it so code on the visualforce page is completely ignored if a condition is true? I.e. the presence of that code has no effect on memory/cpu usage at all?

4

Wrap the code you want within an apex:outputText:

<apex:repeat value="{!some-record-list}" var="record">
    <apex:outputText rendered="{!CONDITION_A}">
        <!-- Lots of code here that processes various sObject fields on the record and displays various info, requires a lot of memory -->
    </apex:outputText>
    <apex:outputText rendered="{!CONDITION_B}">
        <!-- Different, but equally if not more memory requiring code here -->
    </apex:outputText>
    <!-- undefined number of varying divs, not similar enough to make a component, but considerably similar -->
</apex:repeat>

Visualforce will completely omit the code during the rendering phase, reducing the amount of code downloaded and parsed by the browser.

| improve this answer | |
  • Is that any different from outputPanel with layout="none" or any other alternatives (can't think of any at the moment)? – Adrian Larson Jul 13 '16 at 3:47
  • @AdrianLarson Yes, layout="none" still renders the elements but sets the style to "display: none", so the browser still has to parse extra HTML. "rendered" is the only attribute that actively omits the contents of whatever its set on. I prefer to use apex:outputText, but you could also use apex:outputPanel with a rendered attribute and layout="block" (so when rendered, produces div tags for you, but when not rendered, produces nothing), apex:panel/apex:panelGrid to render tables, etc. – sfdcfox Jul 13 '16 at 10:05

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.