Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » conditional html in templates

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1285 Views
  • MrElf
    Avatar
    Community Member
    9 Posts

    conditional html in templates Link to this post

    In my sample example I have a collection of members who are sorted by industry ( a text field) What I want to do is have a conditional header that is displayed when the industry group changes.

    This is an outline of what I want to do.

          <div id="MemberList">
             <% control MemberList %>
                <% if $lastindustry <> $Industry %>
                   <h2>$Industry</h2>
                   $lastindustry = $Industry
                <% end_if %>
                <p>$Title - $CompanyName</p>
             <% end_control %>
          </div>

    How best to achieve this?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: conditional html in templates Link to this post

    the template parser is pretty basic in what you can do, eg you cant assign variables and change $lastIndustry inside the template.

    What you would need to do is change your datamodel a bit for the template so you have a DataObjectSet of Industries. Then each industry has a DataObjectSet of Members so in the template you would do

    <% control Industries %>
    <h2>$Title</h2>
    <% control IndustryMembers %>
    $Title - $CompanyName
    <% end_control %>
    <% end_control %>

    And so your PHP method Industries() would need to make a DataObjectSet of all the Industries then for each Industry generate a list of members for it

    1285 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.