Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Controlling Widgets in your Templates - If Statements based on Titles & Widget Counts

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

Page: 1
Go to End
Author Topic: 2572 Views
  • NathanB
    Community Member
    2 Posts

    Controlling Widgets in your Templates - If Statements based on Titles & Widget Counts Link to this post

    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.

    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
    Web Design by Polar Design

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.