Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » use url parameter within template

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

Page: 1
Go to End
Author Topic: 3252 Views
  • dacar
    Avatar
    Community Member
    168 Posts

    use url parameter within template Link to this post

    Hi,

    for a menu i parse the third level from sitetree. Every third level item is related to some dataobjects. These dataobject are nested under the related menuitem (<% control getFirmenforBanner %>) and link to the same template (e.g. http://www.blabla.de/handel-und-industrie/show/1/). What i want to do:

    If the show parameter from URL is similar to the $ID within <% control getFirmenforBanner %>, then i want to write a "class="current" to the <li>.

    Can anybody help?

    <ul class="menu">
    <% control Menu(3) %>   
    <li class="sublevel1"><a href="{$Link}" title="weiter in $Title.XML page" class="$LinkingMode"><span>$MenuTitle</span></a>
    <% if LinkOrCurrent = current %>
    <ul>
    <% control getFirmenforBanner %>
    <li class="sublevel2"><a href="{$URLSegment}/show/$ID">$Firmenname</a></li>
    <% end_control %>
    </ul>
    <% end_if %>
    </li>
    <% end_control %>
    </ul>

    Greetings, Carsten

  • Dave L
    Avatar
    Community Member
    56 Posts

    Re: use url parameter within template Link to this post

    Hi Carsten,

    I had a similar problem trying to show the "current" image in a custom gallery page I built. Templates currently do not have this ability. What I did in the end was this:

    class GalleryPage extends Page {

       static $has_many = array (
          'Images' => 'GalleryPage_Image'
       );

    }

    class GalleryPage_Controller extends Page_Controller {

    }

    class GalleryPage_Image extends Image {

    static $has_one = array(
    'GalleryPage' => 'GalleryPage',
    );

    function IsCurrent() {
    return (Director::urlParam("ID") == $this->ID) ? true : false;
    }

    }

    Basically, as the control loops through Images, the Image has a check against the URL. In my template:

    <% control Images %>
    <div <% if IsCurrent %>class="current"<% end_if %>>
    <a href="$CurrentPage.URLSegment/showImage/$ID" onclick="return false;">
    <img src="$URL" alt="$Title" />
    </a>
    </div>
    <% end_control %>

    Some code has been removed for clarity, so copy and paste won't work. Hope this helps.

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: use url parameter within template Link to this post

    Hi Dave,

    very good idea. But i have already solved it by altering my control. I have just added the id to my DataObjectSet.

    $firmen = new DataObjectSet();
              $urlid = Director::urlParam('ID'); // URL PARAMETER VERARBEITEN
              $count = -1;
              foreach($result as $row) {
                if($row['ID'] == $urlid){
                   $row['status'] = ' current';
                } else {
                   $row['status'] = '';
                }
                $firmen->push(new ArrayData($row));
          }    

    Thanks a lot for your reply.

    Greetings from Germany, Carsten.

    3252 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.