Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check an empty component
#1
Hi guys!
I'm developing 3 new free responsive html5 themes for Get simple.
I'll post them soon.

By the way, here's my question.
I need to check if a given component is empty or not.

If it is not empty, the template shows some code and the component itself, if it is empy, it won't.

I've tried using
Code:
<?php
if (get_component('something'){ ... }
?>

but the function does not work (it prints out the content, since the function get_component doesn't return a value, but prints it out directly).

Any ideas please?

Thank you guys!
Marco
Reply
#2
Put this code in your theme (I suggest you create a functions.php file and put it there):

Code:
<?php
if (!function_exists('component_exists')) {
    function component_exists($id) {
        global $components;
        if (!$components) {
             if (file_exists(GSDATAOTHERPATH.'components.xml')) {
                $data = getXML(GSDATAOTHERPATH.'components.xml');
                $components = $data->item;
            } else {
                $components = array();
            }
        }
        $exists = FALSE;
        if (count($components) > 0) {
            foreach ($components as $component) {
                if ($id == $component->slug) {
                    $exists = TRUE;
                    break;
                }
            }
        }
        return $exists;
    }
}
?>

Then use it in your template file(s) like this:

Code:
<?php if (component_exists('mycomponent')) { ?>

... conditional html code ...

<?php get_component('mycomponent');    ?>

... more html code ...

<?php } ?>

That would be for enclosing a component, only if it exists, between some html code.

If you just want to display alternative content when the component does not exist, it could be like:

Code:
<?php
    if (component_exists('mycomponent')) {
        get_component('mycomponent');
    } else {
?>

... alternative text or html code ...

<?php } ?>
Reply




Users browsing this thread: 1 Guest(s)