|
iceat
|
 |
« on: May 16, 2010, 08:52:10 PM » |
|
A new snippet which you can use to check if a section block contains content on the current page. You can use this in your template to hide blocks of html when they're not needed. This way you can make your template smarter (and you don't need to create multiple templates just for changing small details in the page layout). This is how you use it:<?php if (hascontent(XXX) == true){ ?> BLOCK OF HTML TO SHOW OR HIDE <?php } ?>
Replace 'XXX' with the section block id. I haven't checked if it already exists. But I needed it, so I coded it.
|
|
|
|
« Last Edit: May 16, 2010, 09:03:26 PM by iceat »
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #1 on: May 16, 2010, 09:27:51 PM » |
|
Sounds great! Small question:
When you say section block id... do you mean the CSS-name of a content-block? Or do you mean the WB section ID number?
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
crnogorac081
AddOn Development
Offline
Posts: 1706
|
 |
« Reply #2 on: May 16, 2010, 09:43:45 PM » |
|
Hi,
obviously section id from WB
|
|
|
|
|
Logged
|
Wow, I coded something myself: PM Messanger Modul ,Searchbox with suggestions
|
|
|
mr-fan
Offline
Posts: 1556
|
 |
« Reply #3 on: May 16, 2010, 11:12:56 PM » |
|
hi, i've used similar with checking if a whole content block has content to setup html output in templates like this: <div id="tertiary"> <div class="content"> <?php //page content 2 if ($page_content_2 != '') { echo '<div class="roundbox"><h3 class="gradient">Additional Box 2</h3>'.$page_content_2.'<p class="last"></p></div> <!--end Box-->'; } else { echo ''; }?> <?php //page content 3 if ($page_content_3 != '') { echo '<div class="roundbox"><h3 class="gradient">Additional Box 3</h3>'.$page_content_3.'<p class="last"></p></div> <!--end Box-->'; } else { echo ''; }?> only thing is to load the content blocks with ob_start like this - i put this at the beginning of every template: <?php //for the color ob_start(); page_content(1); $page_content_1 = ''.ob_get_contents(); ob_end_clean();
ob_start(); page_content(2); $page_content_2 = ''.ob_get_contents(); ob_end_clean();
ob_start(); page_content(3); $page_content_3 = ''.ob_get_contents(); ob_end_clean(); best regards martin
|
|
|
|
|
Logged
|
|
|
|
|
iceat
|
 |
« Reply #4 on: May 17, 2010, 07:48:16 AM » |
|
Sounds great! Small question:
When you say section block id... do you mean the CSS-name of a content-block? Or do you mean the WB section ID number?
I mean the ID of the content block (for example: show_content(2) -> the id is 2)
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #5 on: May 17, 2010, 08:08:41 AM » |
|
So if I have three (white) contentblocks in my template they will only appear when it has content? Right? 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
|
iceat
|
 |
« Reply #6 on: May 17, 2010, 08:29:56 AM » |
|
yep
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #7 on: May 17, 2010, 08:35:25 AM » |
|
I will check it out. Nice and timesaving snippet!! Thank you for sharing! 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
|
Argos
|
 |
« Reply #8 on: May 17, 2010, 10:59:44 AM » |
|
Nice one! Thanks for sharing.
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #9 on: May 17, 2010, 04:46:17 PM » |
|
I tested it... It's just perfect!! 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
|
|
|
iceat
|
 |
« Reply #11 on: May 17, 2010, 08:15:06 PM » |
|
Too much code, just use my snippet 
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #12 on: May 17, 2010, 08:29:27 PM » |
|
I agree with Iceat 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
elarifr
Offline
Posts: 115
|
 |
« Reply #13 on: May 17, 2010, 08:47:09 PM » |
|
i agree it's too much code !
but the snippet must be added fist and you have to check is exist before it consume an sql query to check, and then page_content wil make the same query to ouput the content if not empty you need to know the id of the block
so hascontent can be used in some case
in fact i was just looking if mr-fan had tested his code with wb2.8.1 ?
thanks
|
|
|
|
|
Logged
|
Accedinfo.com - Société de maintenance informatique sur Strasbourg pour particuliers et entreprises. Tel +33 3 88 33 77 45
|
|
|
|
iceat
|
 |
« Reply #14 on: May 17, 2010, 08:50:20 PM » |
|
i don't think the extra query (or queries) will slow the website down, it's really straight-forward.
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #15 on: May 17, 2010, 09:00:59 PM » |
|
Maybe there should be a small readme file. I think it's not all clear to people howto implement it in their template 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
|
iceat
|
 |
« Reply #16 on: May 18, 2010, 08:26:34 AM » |
|
read my first post  No, I'll create a small webpage for it 
|
|
|
|
|
Logged
|
|
|
|
aadwagner.nl
Offline
Posts: 210
|
 |
« Reply #17 on: May 18, 2010, 08:32:42 AM » |
|
That would be nice 
|
|
|
|
|
Logged
|
Groeten, Aad Wagner.nl
|
|
|
mjm4842
Offline
Posts: 227
|
 |
« Reply #18 on: August 09, 2010, 05:58:35 AM » |
|
According to the source code, the snippet doesn't check to see if there is any content. It only check to see if there are sections on the given page. Even if the sections are empty, the snippet will still return true.
|
|
|
|
|
Logged
|
Frustrated? Spending too much time developing when all you really want is a working website? Contact me directly if I can be of services to you. ___________________ ________ www.tngconsulting.c a
|
|
|
|
iceat
|
 |
« Reply #19 on: August 09, 2010, 08:15:13 PM » |
|
That's correct! But why would you add modules without output to a section?
Btw, I finished the new version of hascontent. This time it only uses one query to check all of your sections.
On top of that you can use it to check if a page has submenu-items, or is part of a submenu. This way you can show/hide menu blocks.
I'll release it with my new website later this month.
|
|
|
|
|
Logged
|
|
|
|
mjm4842
Offline
Posts: 227
|
 |
« Reply #20 on: August 10, 2010, 04:53:20 AM » |
|
Hi iceat,
Thank you for taking the time to respond and contributing your snippet. While it does not address my current need, I may very well have a need for it in the future.
The reason it is important for me to detect if the section is empty is because there are parts of the webpage that actually only show up if there is content. If there isn't any content, the sections automagically vanish.
The reason I have empty sections is because I have a Global-Blocks page is pre-configured with multiple sections. My clients like things very simple and are not the kind to think about creating sections if they don't already exist on a page. So most of the time, the sections just sit there empty. It's actually pretty cool.
Just trying to keep WebsiteBaker as simple to use as possible for my clients.
Best regards,
Michael
|
|
|
|
|
Logged
|
Frustrated? Spending too much time developing when all you really want is a working website? Contact me directly if I can be of services to you. ___________________ ________ www.tngconsulting.c a
|
|
|
Vincent
Offline
Posts: 360
|
 |
« Reply #21 on: October 01, 2010, 09:18:22 AM » |
|
Comes in very handy. Thanks for sharing.
Vincent
|
|
|
|
|
Logged
|
|
|
|
Hans
Offline
Posts: 565
|
 |
« Reply #22 on: October 01, 2010, 01:04:15 PM » |
|
|
|
|
|
|
Logged
|
Hans - Nijmegen - The Netherlands
|
|
|
Re-Mi
Offline
Posts: 87
|
 |
« Reply #23 on: February 09, 2012, 03:32:42 PM » |
|
Hi, I know it's an old thread. But i hope someone can help me. I'm using hascontent for a website i'm currently developing. On my local server it works just fine and exactly as it should But after uploaded to the external server the defined content doesn't show up anymore. I'm using the latest WB 2.8.2 And this is the piece of code i'm using. As you can see, im using hascontent(2) 2 times but changing that doesn't work. <?php if (hascontent(2) == true){ ?> <div class="contenttitel"><?php echo WEBSITE_HEADER; ?></div> <div class="slider"></div> <div class="frontblokken"> <div class="blok1"></div> <div class="blok2"></div> <div class="blok3"></div> </div> <?php } ?> <?php if (hascontent(1) == true){ ?> <?php page_content(); ?> <?php } ?> </div> <?php if (hascontent(2) == true){ ?> <div class="frontcontent"> <div class="frontcontentlinks"><?php page_content(2); ?></div> </div> <?php } ?> Hope someone can help me
|
|
|
|
|
Logged
|
|
|
|
|
DarkViper
|
 |
« Reply #24 on: February 10, 2012, 02:30:05 AM » |
|
here is a new version of this small function. Recoded and tested for all WB 2.8.x versions <?php /** * returns TRUE if at least one section is assigned to the requested * block on the current page. Otherwise returns FALSE * !! the function does not care for visibility and rights of the content !! */ function hascontent($iBlockId) { global $database; $sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'sections` ' . 'WHERE `block`='.intval($iBlockId).' AND `page_id`='.intval(PAGE_ID); return (intval($database->get_one($sql)) != 0); } ?>
...
<?php if (hascontent(2)) { ?> <div class="frontcontent"> <div class="frontcontentlinks"><?php page_content(2); ?></div> </div> <?php } ?>
|
|
|
|
« Last Edit: February 10, 2012, 02:46:08 AM by DarkViper »
|
Logged
|
Anleitungen lesen und selber nachdenken ist anstrengend... Da lass ich doch lieber andere für mich denken...
In 1984: Nineteen Eighty-Four is a unrealistic utopia!! In 2012: Nineteen Eighty-Four is a little piece only of our reality!!
|
|
|
|