General
Getting & Giving Support
Administration
Design
- Tut. - Layouts and Themes
- Tut. - Navigation cookbook
- Tut. - How To section
Reference Material
Plugin Development
- Tut. - Writing a plugin
- Ref. - List of Events
- Ref. - Enabling cron jobs
- Ref. - PHPDoc
Core Development
- Ref. - PHPDoc
Press information
Wolf CMS books section
Create multiple/different Layouts with Switch case
Here you can find the short version of code. The whole page with additional comments can be found in this forum post: Setting default page layouts
Please read the whole forum post before continuing.
<?php $i = '/'; switch ($i) { case ($this->level() == 0): $this->includeSnippet('layoutHome'); break; case ($this->slug() == 'about'): $this->includeSnippet('layoutAbout'); break; case ($this->slug() == 'recommendations'): $this->includeSnippet('layoutRecommend'); break; case ($this->slug() == 'experience' || $this->parent()->slug() == 'experience'): $this->includeSnippet('layoutExperience'); break; case ($this->slug() == 'clients' || $this->parent()->slug() == 'clients'): $this->includeSnippet('layoutClients'); break; case ($this->slug() == 'blog'): $this->includeSnippet('layoutBlog'); break; case ($this->parent()->slug() == 'blog'): $this->includeSnippet('layoutBlogposts'); break; case ($this->slug() == 'contact'): $this->includeSnippet('layoutContact'); break; case ($this->slug() == 'sitemap'): $this->includeSnippet('layoutSitemap'); break; default: $this->includeSnippet('layoutAbout'); } ?>
Comments
- You can put that whole thing in a snippet if you like (e.g., “layoutChooser”), and call the snippet in your main Layout, OR you can just put it in the Layout itself. It doesn't matter which you do, really. (I'm assuming you will have the layouts themselves in the snippets called in each “case” obviously!)
- If “switch” statements are new for you, then do note the last one: “default”. If all other “cases” fail, you'll get this one. You can make it whatever you like; if you're using the 404 plugin, then you'll probably want to set the default “layout snippet” to whatever layout it uses.
- I have constructed that set of tests using slug values, because it is the most transparent to the user. If you wanted a more fail-safe system, you could use page ID's, but then you'd want to comment your switch cases, I expect: e.g.,
case ($this->id() == '3'): // About page $this->includeSnippet('layoutAbout'); break;
although if you use the snippet naming convention I've used above, that will be clear enough, won't it!
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2