Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function: per page components.
#1
For a layout I was messing around with I wanted to be able to adjust the footer for certain pages only. As I thought I had seen something like that here on the forum I did a little search. I found a question by TwentyTwoBelow asking about the ability to set up components on a per page basis.

Chris came up with a way to do it:
Code:
if ( return_page_slug() == 'about' ) {
    get_component('about-component');
} elseif ( return_page_slug() == 'index' ) {
    get_component('homepage-component');
} else {
    get_component('generic-component');
}
However, I feel this will get very frustrating in the long run as I will have to edit the template for every page I want to use a different component on. I do like the idea Chris coined, placing different labels in front of the component name. If only this could be automated...

Well, of course it can be automated!

I wrote my own get_component() function called component_master(), that you can find at the bottom of this topic. If you want to use it for your template just put it in the functions.php that you have (or must create) in your theme folder.

How to use it, an example.

In the basic theme Cardinal you will find the following piece of code in the template.php:
Code:
<div class="section">
            <?php get_component('sidebar'); ?>
        </div>
Change it to this:
Code:
<div class="section">
            <?php component_master('sidebar'); ?>
        </div>
That's all. Nothing should break and it should still include the contents of the sidebar component.

To change the sidebar on the homepage we don't have to change anything in the theme any more. All we do is add a new component on the components page of the admin panel. If we create a component called index-sidebar this component will overwrite the one called sidebar on our homepage but on no other pages!

By simply putting the slug of the page we want to target in front of the component name it will show up. Of course, all pages that you don't specifically target with different components get the one with the name we put into the component_master() function. In the case of our example all pages that don't have a matching component will be showing the content of the component called sidebar.

What if I don't want to overwrite but just want to add something?

I thought about this and came up with a solution. If you want the generic component to show but want to add a per page component you can create components named with the following pattern: slug-extra-component. To fall back on my previous example I could create a component called index-extra-sidebar and this one will be appended to the sidebar component. Of course if slug-component (eg. index-sidebar) exists the extra component is ignored.

In case your template needs the extra addition to the component to not follow the generic one but be rendered before it you want to change your template.php a little.

You will need to call the component_master() function with an extra argument:
Code:
component_master('sidebar',true);

Now the order of the way it prints the components will be turned around.

The function as is.

I provide this function as it is. I will probably update it when a GetSimple update breaks it or when others come up with suggestions, but I won't promise that. If anything suddenly wipes your system clean when you use this, don't look at me either.
Code:
function component_master($id,$first=false) {
        if (file_exists('data/other/components.xml')) {
            $master = return_page_slug();
            $extra = false;
            $standard = false;
            $thisfile = file_get_contents("data/other/components.xml");
            $data = simplexml_load_string($thisfile, NULL, LIBXML_NOCDATA);
            $components = $data->item;
            
            if (count($components) != 0) {
                foreach ($components as $component) {
                    if ($master.'-'.$id == $component->slug) {
                        eval("?>" . stripslashes(htmlspecialchars_decode($component->value, ENT_QUOTES)) . "<?php ");
                        return;
                    } elseif ($master.'-extra-'.$id == $component->slug) {
                        $extra = $component->value;
                    } elseif ($id == $component->slug) {
                        $standard = $component->value;
                    }
                }
                if ($standard) {
                    eval("?>" . ($extra&&$first?stripslashes(htmlspecialchars_decode($extra, ENT_QUOTES)):'') . stripslashes(htmlspecialchars_decode($standard, ENT_QUOTES)) . ($extra&&!$first?stripslashes(htmlspecialchars_decode($extra, ENT_QUOTES)):'') . "<?php ");
                }
            }
        }
    }
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply


Messages In This Thread
Function: per page components. - by Zegnåt - 2009-10-31, 23:37:48
Function: per page components. - by GetSimple.RU - 2009-11-13, 22:11:24
Function: per page components. - by Zegnåt - 2009-11-14, 23:43:17
Function: per page components. - by focoves - 2009-12-15, 06:53:28
Function: per page components. - by Zegnåt - 2009-12-15, 06:58:50
Function: per page components. - by tyee - 2010-02-10, 12:50:10
Function: per page components. - by tyee - 2010-02-10, 14:26:47
Function: per page components. - by Origin - 2010-05-20, 02:17:03
Function: per page components. - by jasonbill - 2010-10-12, 22:41:05
Function: per page components. - by Zegnåt - 2010-10-13, 01:36:32
Function: per page components. - by quite_me - 2010-11-19, 21:07:46
Function: per page components. - by Zegnåt - 2010-11-20, 00:20:20
Function: per page components. - by quite_me - 2010-11-20, 00:39:26
Function: per page components. - by vladislav - 2011-04-13, 21:40:00
Function: per page components. - by Oleg06 - 2011-04-13, 23:36:23
Function: per page components. - by crusher88 - 2011-05-16, 22:20:17
Function: per page components. - by mvlcek - 2011-05-17, 01:19:35
Function: per page components. - by crusher88 - 2011-05-17, 04:47:59



Users browsing this thread: 1 Guest(s)