0

我有以下代码:

  <% control StaffMembers %>
    <li>
        Name: <h2>$Name</h2>
    </li>
    <% end_control %>

我想问一下,仅当名称文本字段不为空或不为空时,如何才能显示它。控件中的 if 语句不起作用:

 <% control StaffMembers %>
   <% if $Name %>
     <li>
        Name: <h2>$Name</h2>
    </li>
   <% end_if %>
<% end_control %>

谢谢

4

1 回答 1

2

Your code looks correct. Calling an if statement inside the control should work.

I would also recommend putting an if statement around your control to check if there are any StaffMembers before looping through them:

<% if $StaffMembers %>
    <ul>
    <% control $StaffMembers %>
        <% if $Name %>
        <li>
            Name: <h2>$Name</h2>
        </li>
        <% end_if %>
    <% end_control %>
    </ul>
<% end_if %>

Or in Silverstripe 3 using loop instead of control:

<% if $StaffMembers %>
    <ul>
    <% loop $StaffMembers %>
        <% if $Name %>
        <li>
            Name: <h2>$Name</h2>
        </li>
        <% end_if %>
    <% end_loop %>
    </ul>
<% end_if %>
于 2013-09-30T22:07:24.897 回答