WBCE Home | WBCE Hilfe | WBCE Addon Repository | Impressum | Datenschutz

WBCE CMS Forum

WBCE CMS – Way Better Content Editing.

You are not logged in.

#1 22.02.2019 03:14:11

losttrip
Member

Hide block of code if Section is empty

There was a snippet "hascontent" on WB that was useful for hiding a block of code when a particular section was not used on a page.

[== PHP ==]
<?php if (hascontent(XXX) == true){ ?>
BLOCK OF HTML TO SHOW OR HIDE
<?php } ?>

This was helpful to only show associated CSS when there is content of that type on the page.

Is there something similar available which is compatible with the current version of WBCE?

Online

#2 22.02.2019 07:20:03

florian
Administrator

Re: Hide block of code if Section is empty

The snippet is still available, but not compatible with PHP > 5.6, so it's not a WBCE compatibility problem.
But you don't need a snippet.
Instead of <?php page_content(2) ?>, You can either put the following code into your template

<?php
ob_start();
page_content(2);
$content = ob_get_contents();
ob_end_clean();
if (strlen($content) > 50) {
?>
Your HTML code goes here...
<?php  echo $content; // if needed/wanted here ?>
And/ Or here...
<?php } ?>

Or have a look in the newer templates like Buschwerk, how it's done there:
First, all content blocks are fetched:

require_once __DIR__.'/info.php';
		foreach($block as $k=>$v){
			ob_start(); 
			page_content($k); 
			$block[$k] = ob_get_clean();
		}

Then it's simple to check if a block has content:

 <?php if ($block[2]!='') {  ?>
         Your HTML code goes here...
       <?php echo $block[2];   // if needed/wanted here  ?>
          and/or here...
<?php } ?>

Offline

Liked by:

losttrip

#3 22.02.2019 17:21:43

bernd
Developer

Re: Hide block of code if Section is empty

Can someone please give me a link to (or send me) the "hascontent" snippet.
Would like to have a look into it.
Think such a (working) snippet would be helpful especially for users that are not that fit in php.


2 x ROT13 hält besser ...

Offline

#4 22.02.2019 17:26:50

florian
Administrator

Re: Hide block of code if Section is empty

Here you are

(Attachment removed - florian)

Last edited by florian (23.02.2019 08:54:13)

Offline

#5 22.02.2019 19:59:14

stefanek
Developer

Re: Hide block of code if Section is empty

Ich habe es jetzt nicht geprüft, aber der Code unten könnte es tun:

<?php

<?php
function hascontent($iBlockID) {
	global $database;
	$sSqlf = "SELECT COUNT(section_id) FROM `{TP}sections` WHERE `block` = %d AND `page_id` = %d";
	$count = $database->get_one(sprintf($sSqlf, (int) $iBlockID, PAGE_ID));
	return ($count != false) ? true : false;	
}

Wobei ich würde den Funktionsnamen irgendwie sprechender gestalten, sowas wie
block_has_content($iBlockID) oder
has_block_content($iBlockID)

Gruß,
Christian

Last edited by stefanek (22.02.2019 21:25:39)


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

Liked by:

losttrip

#6 22.02.2019 20:16:08

bernd
Developer

Re: Hide block of code if Section is empty

Wollte ich grade schreiben,
nach einem ersten schnellen Blick ist das "hascontent" eigentlich total irreführend wie die übergebene $section_ID (das ist eine Block_ID).
Es wird nicht geprüft ob ein Block Inhalt hat sondern ob ein Block auf der Seite vorhanden ist.
Wenn also ein Block vorhanden ist und (warum auch immer) keinen Inhalt hat ist hascontent trotzdem true.

Christians Schnippsel funktioniert wenn man die letzte Zeile korrigiert ;-)

return ($count != false) ? true : false;	

2 x ROT13 hält besser ...

Offline

Liked by:

stefanek

#7 22.02.2019 20:21:29

stefanek
Developer

Re: Hide block of code if Section is empty

Ja, sollte natürlich false sein...
Danke Bernd.

Übrigens, warum frage ich überhaupt ab, ob $count nicht false ist?
Der Hintergrund ist der, dass die Methode $database->get_one() immer false zurück gibt, wenn es kein Ergebnis gibt, sprich keine Daten bei der Query ermittelt werden konnten.
Und in diesem Fall ist es auch alles, was wir wissen wollen, gibt es Rückgabewerte (true) oder gibt es sie eben nicht (false).

Gruß,
Christian

Last edited by stefanek (22.02.2019 20:22:11)


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

#8 22.02.2019 20:45:47

florian
Administrator

Re: Hide block of code if Section is empty

please write in english to give the TO the chance to follow the discussion.

Offline

#9 22.02.2019 21:07:06

bernd
Developer

Re: Hide block of code if Section is empty

Sorry,
the snippet Christian posted is working with the corrected last line see post #6.
But the initial name "hascontent" is totaly misleading, also the so called variable $section_ID (should be $block_ID) :
The snippet simply checks if there is a block on a page, it does not check if this block has any content.

I can make later this evening a new snippet with Christians content, but would prefer to call it something like "pageHasBlock" ...


2 x ROT13 hält besser ...

Offline

#10 22.02.2019 21:20:19

stefanek
Developer

Re: Hide block of code if Section is empty

bernd wrote:

The snippet simply checks if there is a block on a page, it does not check if this block has any content.

Yes, it actually does.
It checks if the block has any section (section_id) associated with it on the page (PAGE_ID). And if it is the case the function returns TRUE.

Regards,
Christian

Last edited by stefanek (22.02.2019 21:20:58)


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

#11 22.02.2019 21:23:11

losttrip
Member

Re: Hide block of code if Section is empty

No need to apologize.  I read this forum with Google Translate on, so I didn't even realize that your posts were not written in English.

@Christian - Thank you for taking the time to look into and re-work the snippet.  Much appreciated!

@florian - thank you for your solutions.  Are those methods "better" in some way than the "hascontent" or Christian's reworking?  Or are those simply different ways of achieving the same result?  I ask because, it was just always so easy for me to remember how to add the "hascontent" code to my templates, but if it is not the best method, I will evolve.

@bernd - I realize the name "hascontent" is misleading, but I always knew what the snippet did, and I missed it's ease of use and functionality.  I agree a more appropriate name would be beneficial moving forward.

Thank you all for your participation!

Online

Liked by:

stefanek, bernd

#12 22.02.2019 22:56:47

stefanek
Developer

Re: Hide block of code if Section is empty

Hello losttrip,

I prepared a new snippet module with this function and attach it to this post.

I expanded the code a little bit and renamed the function name for it to be less confusing and phrased it in a way that it poses as a question "Has Block Content?"

So the new function name is:
has_block_content($iBlockID)

Enjoy.
And I think it could be also uploaded to the Addon Repository.

You wrote:

losttrip wrote:

@florian - thank you for your solutions.  Are those methods "better" in some way than the "hascontent" or Christian's reworking?  Or are those simply different ways of achieving the same result?  I ask because, it was just always so easy for me to remember how to add the "hascontent" code to my templates, but if it is not the best method, I will evolve.

I'm not Florian, but from my perspective both ways are legit.
There is some benefit in using it the way Florian proposes.
You don't need the snippet/module to be present on the installation for the template to work. This is one benefit.
Another benefit might be that you can do some processing on the content of the block if you need to, because the content of the block is now in the variable , let's say $block[2].
For example you could run a str_replace operation on it like so:
$block[2] = str_replace('h1>', 'h2>', $block[2]);
and doing so replace all erronously placed <h1></h1> headings with h2 headings.

But generally if you have been doing good using the function up till now, you just can keep it as a working solution and if you need more you can always come back and use Florians suggestions.

Hope this helps to see the different solutions in a better perspective.

Regards,
Christian

(Attachment removed, newer version below - florian)

Last edited by florian (23.02.2019 08:55:05)


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

Liked by:

bernd, losttrip

#13 23.02.2019 05:25:29

losttrip
Member

Re: Hide block of code if Section is empty

Thank you for doing this.

I installed the module (snippet).  On page load, I get a white screen with this error:

Parse error: syntax error, unexpected '==' (T_IS_EQUAL), expecting ')' in /home/.../modules/has_block_content/include.php on line 10

I get this error even without any calling code in my template index.php file.

Last edited by losttrip (23.02.2019 05:54:58)

Online

#14 23.02.2019 06:32:59

florian
Administrator

Re: Hide block of code if Section is empty

Here's the working one, there was just one '=' too much.
Available at AOR too: https://addons.wbce.org/pages/addons.ph … m&item=108

Please note: even if there's an empty section the snippet returns true.


(Attachment removed, newer version below - florian)

Last edited by florian (23.02.2019 08:55:39)

Offline

Liked by:

losttrip

#15 23.02.2019 07:11:19

losttrip
Member

Re: Hide block of code if Section is empty

Thank you!  Working perfectly now.

Last edited by losttrip (23.02.2019 07:11:40)

Online

#16 23.02.2019 08:10:08

stefanek
Developer

Re: Hide block of code if Section is empty

@Florian

Thank you for uploading the Snippet to the AOR.

I attach a new version (0.0.2) of has_block_content.

The calls are the same
has_block_content(YOUR_BLOCK_ID)
but it now actually checks for CONTENT if a section or sections are present in that block on that page.

It now will only return TRUE if there actually is any content. (At least it works with WYSIWYG, I didn't check any other modules).
It will also consider the Publication Time (publ_start and publ_end), so if the section is not public it will return FALSE.

The new function code is:

[== PHP ==]
<?php 
/**
 * @platform  WBCE CMS
 * @license   GNU/GPL v.2
 * @copyright Christian M. Stefan
 * 
 * @brief   Check if a templates layout block has any content (section) in it.
 *
 * @global  object   $database      
 * @global  object   $wb
 * @global  array    $globals    several global vars
 * @global  array    $TEXT
 * @global  array    $MENU
 * @global  array    $HEADING
 * @global  array    $MESSAGE
 * @param   integer  $iBlockID   The ID of the layout block of the template 
 * @return  bool     true if has content, else returns false 
 */

function has_block_content($iBlockID = NULL) {
	global $database;
	$sBlockContents = '';
	if ($iBlockID != NULL) {
		$sSqlf = "SELECT COUNT(section_id) FROM `{TP}sections` WHERE `block` = %d AND `page_id` = %d";
		$count = $GLOBALS['database']->get_one(sprintf($sSqlf, (int) $iBlockID, PAGE_ID));
		
		if ($count != false){
			$sSqlf = "SELECT `section_id`, `module`, `publ_end`, `publ_start` FROM `{TP}sections` WHERE `block` = %d AND `page_id` = %d";
			if ($rSections = $database->query(sprintf($sSqlf, (int) $iBlockID, PAGE_ID))){
				if ($rSections->numRows() > 0){
					while ($aSection = $rSections->fetchRow(MYSQL_ASSOC)) {
						 
						// skip this section if it is out of publication date
						$now = time();
						if (!(
								($now <= $aSection['publ_end'] || $aSection['publ_end'] == 0) 
										&& 
								($now >= $aSection['publ_start'] || $aSection['publ_start'] == 0)
							)) {
							continue;
						}
						
						// check if module exists
						$sModuleViewFile = WB_PATH . '/modules/' . $aSection['module'] . '/view.php';
						if (is_readable($sModuleViewFile)) {
							ob_start(); 
							$page_id    = PAGE_ID;
							$section_id = $aSection['section_id'];
							// we need those global vars to correctly operate the view.php
							global $database, $wb, $globals, $TEXT, $MENU, $HEADING, $MESSAGE;
							$admin = $wb;
							if (isset($globals) and is_array($globals)) {
								foreach ($globals as $sGlobalName) {
									global $$sGlobalName;
								}
							}
							require $sModuleViewFile; // fetch content using the view.php
							$sSectionContent = ob_get_clean();
							if($sSectionContent != ''){
								$sBlockContents .= '~';
							}
							unset($sSectionContent);
						}
					}
				}
			}
		}
		return ($sBlockContents == '') ? false : true;	
	}
}

Have fun.
Christian

P.S.
   @losttrip, please check if it works for you. You just can upgrade it via addons/modules

P.P.S
   Since this is a very new module with a very new code, I removed the Author of "hascontent" snippet in the info.php

Last edited by stefanek (23.02.2019 08:13:56)


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

#17 23.02.2019 08:52:45

florian
Administrator

Re: Hide block of code if Section is empty

Thank you, I've updated the AOR.
Btw, I think it would be better if the versioning starts with 0.1 and so on and not 0.0.1, 0.0.2...
And for $module_platform, please use 1.3 (the given platform version is the version of the other™ CMS - in which the snippet probably won't work)

Last edited by florian (23.02.2019 08:56:23)

Offline

#18 23.02.2019 09:19:59

stefanek
Developer

Re: Hide block of code if Section is empty

Hello Florian,

feel free to adjust the variables in the info.php to your liking.

The Snippet could work easily with WebsiteBaker CMS with a little change, but I don't care too much about this other CMS (or actually not at all, to be honest).

Christian


For any activity, creative or business oriented alike, always ask yourself what the ROI of your contribution will be?
And by the way: Appreciation is a valid form of exchange for help.

Offline

Board footer

Powered by FluxBB

up