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.

Template Questions

Controlling Widgets in your Templates - If Statements based on Titles & Widget Counts


Reply

2583 Views

Avatar
NathanB

10 January 2009 at 8:35am Community Member, 2 Posts

Hey Everybody. I just had some issues since I'm still learning SilverStripe templating, and I figure there're a few others like me. So, this is a brief write-up of what I've learned. Maybe it'll help a few others out there?

IF Statements that were based on the page/Widget's $Title variable:
First of all, two requirements of the template needed me to hide some Widget titles, but show the titles on other pages.
Though it's a bit messy, I realize that some (if not all) Widgets have default titles.

Since I was using the Custom HTML Widget, I put the following code into my template's custom WidgetArea.ss :

   

<% if Title = Info %> <% else %> $Title <% end_if %>

Since I was so used to PHP, I was expecting the IF statement to require a $ before Title, or quotes around "Info" to signify that it was a string. Since SilverStripe somehow managed to figure this out itself, I was good.

Altering CSS/HTML Based on Widgets a page may Possess:
This new template may have pages that have no side bar-- and if there is no side bar, I needed the HTML to be altered to pertain to the CSS. In my case, I wanted to alter/add Classes to a div tag.

Example:
Desired Page code without widgets:

<div id="container">

Desired Page code WITH Widgets:

<div id="container" class="sidebar">

Here is the code I tried:

<div id="container"
<% control Widgets %>a <% if TotalItems %>class="sidebar"<% else %> <% end_if %> b <% end_control %>
>

This won't work because I am not controlling the right thing--. <% control Sidebar %> worked out. See the revised, working code below:

<div id="container"
   class="<% control Sidebar %><% if Widgets.TotalItems %>sidebar<% else %><% end_if %><% end_control %>
" >

I managed to accomplish the above when remembering that controls/IF statements didn't want the $ sign, and when I realized that Widgets were the children of the Sidebar item.

I realize that some of this may seem pretty easy to an experienced SilverStripe coder, but I wanted to put the word out there.
Let me know if this helps you! 8)

~Nathan B
[url=http://www.polardesign.com]Web Design[/url] by [url=http://www.polardesign.com]Polar Design[/url]