0

I'm trying to conditionally render a table with 2 columns per row. This is what I've tried:

<table width="100%" cellpadding="0" cellspacing="10" border="0">
   <apex:variable var="i" value="{!1}" />
   <apex:repeat value="{!relatedTo.Sample__r}" var="sample">
       <apex:outputText rendered="{!IF(MOD(i,2)=1,true,false)}">
           <tr>
       </apex:outputText>
              <td><apex:OutputText value="{!sample.Id}"/></td>
       <apex:outputText rendered="{!IF(MOD(i,2)=1,true,false)}">
           </tr>
       </apex:outputText>
       <apex:variable var="i" value="{!i+1}"/>
    </apex:repeat>
 </table>

I'm getting this error while trying to save the template:

Error: The element type "tr" must be terminated by the matching end-tag "</tr>".

I know it's complaining about the conditional code that does not include the end tag but how do I resolve this?

4
  • Why do you put <tr> tag within <apex:outputText> tag? Commented Jun 3, 2019 at 11:14
  • @OlehBerehovskyi My idea was to display <tr> and </tr> after every 2 <td>s. Not sure if my logic or the syntax is incorrect. Commented Jun 3, 2019 at 11:22
  • I recommend you to read about the <table> HTML element and its structure developer.mozilla.org/en-US/docs/Web/HTML/Element/table Commented Jun 3, 2019 at 11:30
  • @OlehBerehovskyi I know the table structure. I'm just struggling to get this to work using the Visualforce syntax ie within the <apex:repeat>. Commented Jun 3, 2019 at 11:34

1 Answer 1

2

If I am getting your requirement correctly, you want to display the data in rows with two columns i.e. each tr should only have two tds

If that is the case, then you should make sure that you end all the tags properly. Also, when using outputText, you should set the escape attribute to false, so that it does not escape its values.

Your visualforce code should be something like below

<table width="100%" cellpadding="0" cellspacing="10" border="0">
    <tr>
        <apex:variable value="{!0}" var="i"/>
        <apex:repeat value="{!relatedTo.Sample__r}"  var="sample">
            <apex:variable var="i" value="{!i +1}"/>
            <apex:outputText rendered="{!IF(MOD(i,2)!=0, true, false)}" value="</tr><tr>" escape="false"></apex:outputText>
                <td width="50%">
                    <apex:OutputText value="{!sample.Id}"/>
                 </td>
                <apex:outputText rendered="{!IF(MOD(i,2)!=0, true, false)}" value="<td/>" escape="false"></apex:outputText>
        </apex:repeat>
    </tr>
</table>
2
  • Awesome! Thanks. However when I tried your code, I got this error: Unknown property 'core.email.template.EmailTemplateComponentController.i. So I used this instead: <apex:variable value="{!0}" var="i"/> and removed the VALUE() functions. Then it seemed to work. I'm still quite new to Salesforce. So I'm not sure what's going on. But anyway, your code has given me enough hints to proceed with my project. Commented Jun 3, 2019 at 14:11
  • Yup that was for the visualforce page with controller, updated with the corrections you mentioned so that it works for visualforce template. Commented Jun 3, 2019 at 14:20

You must log in to answer this question.

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