Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 09:16:52 AM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155535 Posts in 21713 Topics by 7737 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1] 2   Go Down
Print
Author Topic: New snippet: hascontent  (Read 3839 times)
iceat
Development Team
*****
Offline Offline

Posts: 97



WWW
« 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:

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

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« 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 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 Offline

Posts: 1556


WWW
« 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:

Code:
<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:

Code:
<?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
Development Team
*****
Offline Offline

Posts: 97



WWW
« 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

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« 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?  grin
Logged

Groeten, Aad Wagner.nl
iceat
Development Team
*****
Offline Offline

Posts: 97



WWW
« Reply #6 on: May 17, 2010, 08:29:56 AM »

yep
Logged

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« Reply #7 on: May 17, 2010, 08:35:25 AM »

I will check it out. Nice and timesaving snippet!! Thank you for sharing!  grin
Logged

Groeten, Aad Wagner.nl
Argos
Moderator
**
Offline Offline

Posts: 2161


WWW
« Reply #8 on: May 17, 2010, 10:59:44 AM »

Nice one! Thanks for sharing.
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
aadwagner.nl

Offline Offline

Posts: 210



WWW
« Reply #9 on: May 17, 2010, 04:46:17 PM »

I tested it... It's just perfect!!

 grin

Logged

Groeten, Aad Wagner.nl
elarifr

Offline Offline

Posts: 115



WWW
« Reply #10 on: May 17, 2010, 08:12:42 PM »

hi mr-fan
i was working to check if page_content is empty until i found this thread now
may you plese check this post
http://www.websitebaker2.org/forum/index.php/topic,16228.msg121434.html#msg121434


in fact if i use something like
Code:
empty($content ) or ==""
it is always false as the output filter add an anchor to each block !
Code:
<a class="section_anchor" id="wb_17" name="wb_17"></a>
VotreEspace proposed a workaround
http://www.websitebaker2.org/forum/index.php/topic,16228.msg121446.html#msg121446
Code:
if((strlen($out1) < 60) && (substr($out1,0,2) == '<a') && (substr($out1,-4) == '</a>')) {
echo 'YOUR DEFAULT CONTENT';
} else { echo $out1; }
Logged

Accedinfo.com - Société de maintenance informatique sur Strasbourg pour particuliers et entreprises. Tel +33 3 88 33 77 45
iceat
Development Team
*****
Offline Offline

Posts: 97



WWW
« Reply #11 on: May 17, 2010, 08:15:06 PM »

Too much code, just use my snippet Smiley
Logged

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« Reply #12 on: May 17, 2010, 08:29:27 PM »

I agree with Iceat  grin
Logged

Groeten, Aad Wagner.nl
elarifr

Offline Offline

Posts: 115



WWW
« 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
Development Team
*****
Offline Offline

Posts: 97



WWW
« 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

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« 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  afro
Logged

Groeten, Aad Wagner.nl
iceat
Development Team
*****
Offline Offline

Posts: 97



WWW
« Reply #16 on: May 18, 2010, 08:26:34 AM »

read my first post Smiley

No, I'll create a small webpage for it Smiley
Logged

Webdesign/-Development, Freelance

> http://www.medialoft.be
aadwagner.nl

Offline Offline

Posts: 210



WWW
« Reply #17 on: May 18, 2010, 08:32:42 AM »

That would be nice smiley
Logged

Groeten, Aad Wagner.nl
mjm4842

Offline 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
Development Team
*****
Offline Offline

Posts: 97



WWW
« 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

Webdesign/-Development, Freelance

> http://www.medialoft.be
mjm4842

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

Posts: 360


WWW
« Reply #21 on: October 01, 2010, 09:18:22 AM »

Comes in very handy. Thanks for sharing.

Vincent
Logged
Hans

Offline Offline

Posts: 565


« Reply #22 on: October 01, 2010, 01:04:15 PM »

Hello Iceat,

any news regarding the new version and the website? (http://www.websitebaker2.org/forum/index.php/topic,18231.msg126734.html#msg126734)?
Thanks
Hans
Logged

Hans - Nijmegen - The Netherlands
Re-Mi

Offline 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.

Code:
<?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
Development Team
*****
Offline Offline

Posts: 1252


« 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

Code: (/template/index.php)
<?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!!
Pages: [1] 2   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!