Hi Matt,
OK, quick run-down. Lets say, you wanted a function to do a str_replace on a page title, and replace spaces with hyphens for displaying on your page. (A rather useless function, but simple)
Go to /tutorial/code/Page.php and look at the second class on that page:
class Page_Controller extends ContentController {
function init() {
parent::init();
Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}
}
There is only one method there, init(), and this is called when ever this page type loads. For instance, I use init() for setting a getting cookie values.
To do what I said above, I could simply add a function to the controller class that does exactly what I want, in raw PHP. Like so:
class Page_Controller extends ContentController {
function init() {
parent::init();
Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}
// Method to return the title with spaces swapped for hyphens
function hypenTitle() {
return str_replace(' ', '-', $this->Title);
}
}
Note: Title is an element that I know is available to the Page class. You might have another custom field declared in the Page class above the controller that is accessable to the controller using $this->FieldName.
Now, in my template for the page, located in /tutorial/Page.ss, I can put this code anywhere I want and it will run the method I created above, and display whatever that method returns:
<% hypenTitle %>
That's it. If, in the CMS, I made a page with the title 'My Page', That peice of code in the template above will output 'My-Page' whereever I put it. No PHP tags, no embedded function, no echo or print, just <% functionName %>.
Do try to check out the tutorials in the link in my first post. It is definately the best way to come to grips the way this system and other MVC frameworks work. The first thing I did when I found SS is work through each one. They will give you all the basics, you will see a pattern emerging as you do each one, and you will see the true benefits of a system like this.
Cheers
Aaron