Documentation

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.

<?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

  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
    $this->includeSnippet('layoutAbout');
break;

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 wolfcms.org / design by yello studio / Wolf CMS Inside