Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

conditional html in templates


Go to End


2 Posts   2006 Views

Avatar
MrElf

Community Member, 9 Posts

5 September 2008 at 9:49am

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?

Avatar
Willr

Forum Moderator, 5523 Posts

5 September 2008 at 3:52pm

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