Light-weight, fast, simple and powerful!
Translations of this page?:

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.

$i = '/';
switch ($i) {
    case ($this->level() == 0):
    case ($this->slug() == 'about'):
    case ($this->slug() == 'recommendations'):
    case ($this->slug() == 'experience' || $this->parent()->slug() == 'experience'):
    case ($this->slug() == 'clients' || $this->parent()->slug() == 'clients'):
    case ($this->slug() == 'blog'):
    case ($this->parent()->slug() == 'blog'):
    case ($this->slug() == 'contact'):
    case ($this->slug() == 'sitemap'):


  1. 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!)
  2. 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.
  3. 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

although if you use the snippet naming convention I've used above, that will be clear enough, won't it!

tutorials/multiple_layouts_with_switch_case.txt · Last modified: 2011-09-12 00:46 (external edit)
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 / design by yello studio / Wolf CMS Inside