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 /

use url parameter within template


Reply


3 Posts   3692 Views

Avatar
dacar

Community Member, 168 Posts

11 November 2009 at 11:16am

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

Avatar
Dave L

Community Member, 60 Posts

12 November 2009 at 10:08am

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.

Avatar
dacar

Community Member, 168 Posts

12 November 2009 at 11:33am

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.