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.

Archive /

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

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

Custom Controls - Totally lost


Reply


18 Posts   6350 Views

Avatar
Jona

Community Member, 19 Posts

5 September 2008 at 8:53am

I have another question that might solve my original one... Where can I find the code that SS uses to built all of the existing $Control methods? If I can get some examples or modify them as I see fit, this whole thing may be resolved.

Avatar
Willr

Forum Moderator, 5513 Posts

5 September 2008 at 4:14pm

FYI You code must be in the mysite/code/ folder or the tutorial/code folder. The themes/yourtheme/ folder is for HTML and CSS not the php methods.

Theres a list of methods you can call from templates here - http://doc.silverstripe.com/doku.php?id=built-in-page-controls#quick_reference . Most of these are defined on ContentController (which your Page_Controller should extend) or Controller or Object...

Avatar
Jona

Community Member, 19 Posts

5 September 2008 at 6:25pm

Willr, thank you. You've been most helpful. I was putting my Page.php file in themes/themeName/code/ instead of tutorial/code/. Putting the Page.php file in tutorial/code/ worked. It turns out the solution was as simple as I'd thought: my lack of understanding the structure of SS.

Avatar
Willr

Forum Moderator, 5513 Posts

5 September 2008 at 6:31pm

Hopefully we will make this clearer in the future as we revisit the tutorials

Avatar
Jona

Community Member, 19 Posts

5 September 2008 at 7:43pm

I do hope so. The structure is very specific, and it's not necessarily a bad thing unless you're unfamiliar with said structure, which is the case with newcomers such as myself.

I have another question, if it's not too much to ask. How can I set a $ControlVariable to change based on the iteration of its parent Control? (Sorry if my terminology is wrong.) For example, if I have:

      <ul>
       <% control Menu(2) %>
         <li><a href="$Link" id="$lowercaseMenuTitle">$MenuTitle</a></li>
    <% end_control %>
      </ul>

How can my custom lowercaseMenuTitle method reference the current menu item? It seems that my method outputs an empty string when I place it within the Menu(2) control, yet works fine outside of it. I have tried things like SiteTree::getField("MenuTitle") and DataObject::getField("MenuTitle") but to no avail.

Avatar
Willr

Forum Moderator, 5513 Posts

5 September 2008 at 7:54pm

lowercaseMenuTitle will point to the database field lowercaseMenuTitle() which is defined on Page or Sitetree. Do you have a lowercaseMenuTitle in your db array in Page.php.

A neat thing you can do in SS is even tho $lowercaseMenuTitle points to the lowercaseMenuTitle field in the db it looks first to a lowercaseMenuTitle() method, that means on Page.php you can define a lowercaseMenuTitle() function which has

function lowercaseMenuTitle() {
return strtolower($this->MenuTitle);
}

Now I think that works if you put that in the page controller class in page.php

Avatar
Jona

Community Member, 19 Posts

5 September 2008 at 8:02pm

I do not have a lowercaseMenuTitle field in the database. The reason is because I don't see a need to store information when it's something so simple. If the information were unique or required excessive processing, then I would add it to the db, but that isn't the case in this situation.

The code you described works when I use it outside of a control, but not inside a control. I am still, for some reason, getting an empty string inside of the Menu(2) control.

Avatar
Willr

Forum Moderator, 5513 Posts

5 September 2008 at 8:20pm

Ok well try define it as getlowerCaseTitle() and try it in the page class rather then controller