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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

A question of style.


Go to End


4 Posts   1601 Views

Avatar
meganub

Community Member, 15 Posts

4 December 2009 at 9:17am

Edited: 04/12/2009 9:18am

Hi!

I'm fairly new to SilverStripe, but not to OO PHP5 or development in general. Firstly, from what I've seen so far I'm really liking SilverStripe.

I am currently using it to build a site wherein most of the pages that need to be under CMS control are pretty much identical to the base Page class. I.e. A title and a big rich text field is all that's needed.

However, the look and feel of some of these pages is radically different, hence they need different templates.

Now there seems to be two options (possibly more, but like I said, I'm new) for me to get the different templates working for these different, but similar pages.

1) I can just put a switch statement in the index action function within PageController and test something unique to each instance and then apply different templates.

I.e.

Switch (UniqueThing) {
    case '1':
       use template A;
    ...
    case 'n':
       use template N;
}

2) Create a different sub page class for each unique page design and let that call the required template automatically. I.e. HomepagePage, ArticlePage, TermsPage etc etc.

I can see minor pro's and con's with either approach. I just wanted to ask the forum which way would be considered the 'SilverStripe way'?

Cheers in advance!

Avatar
Sean

Forum Moderator, 922 Posts

4 December 2009 at 9:41am

Edited: 04/12/2009 9:43am

2.) definitely. That's how we typically create projects using SilverStripe.

Polymorphism is better than a lot of switch or if statements.

Sean

Avatar
bummzack

Community Member, 904 Posts

4 December 2009 at 10:03am

Yes, I second what Sean said!
Creating different classes doesn't create a lot of overhead (actually just a new entry in the ClassName enum) and you're still free to extend a given page-type later on... much cleaner and easier to extend.

Avatar
meganub

Community Member, 15 Posts

4 December 2009 at 10:10am

Thanks guys! Really prompt and helpful. :)