Author Topic: TUTORIAL : How to have a default block contents  (Read 11212 times)

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
TUTORIAL : How to have a default block contents
« on: November 30, 2009, 03:20:04 AM »
This is a sample of code who should be implemented in the template (index.php)

Situation : your template have a section block witch, if empty, broke your design. or just plain look empty.

Solution : make a "default" content that will appear if the block is empty.

In you design, you may have your block content called like that :
Code: [Select]
<?php page_content(1); ?>
this code only flush the content and you can't interact with the fonction to know if there is content or not in the block you are asking, so we will use a php fonction called ob_start, witch will "buffer" the content so you can interact with it.

here is your content called with my buffer code :
Code: [Select]
<?php 
ob_start
();
page_content(1);
$out1 ob_get_contents();
ob_end_clean();
?>
this code alone will do nothing, not even show your content, BUT your block content now reside in $out1.

adding a conditionnal to the code you can asked
"if _output_ is empty, then show THIS, else, show the content"
with this code :
Code: [Select]
<?php 
if(empty($out1)) {
echo &
#39;YOUR DEFAULT CONTENT&#39;; 
} else { echo $out1; }
?>

all together to code look like this :
Code: [Select]
<?php 
ob_start
();
page_content(1);
$out1 ob_get_contents();
ob_end_clean();

if(empty(
$out1)) {
echo &
#39;YOUR DEFAULT CONTENT&#39;; 
} else { echo $out1; }
?>

it works well if you got a "header" per pages, but some default pages (like login / account) do not have a header...
« Last Edit: November 30, 2009, 04:10:53 PM by VotreEspace »
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

Offline Vincent

  • Posts: 376
  • Gender: Male
    • websites voor ondernemers
Re: TUTORIAL : How to have a default block contents
« Reply #1 on: November 30, 2009, 03:41:38 PM »
Haven't tested it, but looks like a very clever solution. Thanks for posting!

Vincent

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
Re: TUTORIAL : How to have a default block contents
« Reply #2 on: November 30, 2009, 04:11:27 PM »
it's already working on a website i'm developping right now.
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

Offline elarifr

  • Posts: 126
Re: TUTORIAL : How to have a default block contents
« Reply #3 on: May 17, 2010, 02:53:00 PM »
Hello VotreEspace
Just to say that i'm working on same kind of check
i found some info on
chio page 
http://www.websitebaker2.org/forum/index.php/topic,17409.0.html
and this thread

but it seems that the $content always output an anchor so that it is not empty !!
bellow is tha output code i use / get

is this a change in wb 2.8.1 output filter or does it still works for you  ?

br
elarifr

my code
Code: [Select]
        <!-- start content-top Block 3 -->
        <?php ob_start(); page_content(3); $page_content3=ob_get_contents(); ob_end_clean(); 
        
if ( (SET_CONTENT3==&#39;1&#39;) && ( ($page_content3!="") || ( ($page_content3=="") && (SET_CONTENT3_EMPTY==&#39;1&#39; ) ) ) ) { 
                        
echo &#39;<div id="content-top"><div class="post">&#39;.$page_content3.&#39;</div></div>&#39;;} ?>

                       
        <!-- end content  // emptypc3:'.empty($page_content3).'.sc3e:'.SET_CONTENT3_EMPTY.'.pc3:'. -->


but content3 always include some anchors

Code: [Select]
        <!-- start content-top Block 3 -->
        <div id="content-top"><div class="post"><a class="section_anchor" id="wb_17" name="wb_17"></a></div></div>                       
        <!-- end content  // emptypc3:'.empty($page_content3).'.sc3e:'.SET_CONTENT3_EMPTY.'.pc3:'. -->

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

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
Re: TUTORIAL : How to have a default block contents
« Reply #4 on: May 17, 2010, 03:10:55 PM »
the website i did it on is still in wb 2.8 and not 2.8.1 but work...
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

Offline elarifr

  • Posts: 126
Re: TUTORIAL : How to have a default block contents
« Reply #5 on: May 17, 2010, 03:55:53 PM »
thanks
hope you'll have time to check with 2.8.1 some next time...
but seems filters add in any page content an anchor so output can no more be empty()  :(

elarifr
Accedinfo.com - Société de maintenance informatique sur Strasbourg pour particuliers et entreprises. Tel +33 3 88 33 77 45

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
Re: TUTORIAL : How to have a default block contents
« Reply #6 on: May 17, 2010, 04:02:10 PM »
there is always a workaround

like insted of empty()

use strlen.

like if
Code: [Select]
<?php 
ob_start
();
page_content(1);
$out1 ob_get_contents();
ob_end_clean();

if((
strlen($out1) < 60) && (substr($out1,0,2) == &#39;<a&#39;) && (substr($out1,-4) == &#39;</a>&#39;)) {
echo &#39;YOUR DEFAULT CONTENT&#39;; 
} else { echo $out1; }
?>
it checks if the output is less than 60 chars, and starts and end with an anchor... so there is a good chance it's only the WB anchor

but I havent test it.
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

Offline elarifr

  • Posts: 126
Re: TUTORIAL : How to have a default block contents
« Reply #7 on: May 17, 2010, 09:06:02 PM »
should work...
just not 100% true, but it is a workaround :)

the strangest is that icecat post a snippet that make the same test
http://www.websitebaker2.org/forum/index.php/topic,18231.0/topicseen.html
and mr-fan answer
http://www.websitebaker2.org/forum/index.php/topic,18231.msg121401.html#msg121401
without having an issue about the anchor that a have on ly test page..

thanks
elarifr


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

Offline pcwacht

  • Posts: 2923
  • Gender: Male
    • Dutch ICT info
Re: TUTORIAL : How to have a default block contents
« Reply #8 on: May 18, 2010, 11:13:05 AM »
You could easely remove the anchor, one feature of wich I think should be disabled by default btw.

goto settings - show advanced options
remove or empty field : Section-Anchor text

have fun,
John
http://www.ictwacht.nl = Dutch ICT info
http://www.pcwacht.nl = My first
both still work in progress, since years.....

erpe0812

  • Guest
Re: TUTORIAL : How to have a default block contents
« Reply #9 on: May 18, 2010, 12:06:25 PM »
Quote
wich I think should be disabled by default btw
yepp

rgds

eroe

Offline elarifr

  • Posts: 126
Re: TUTORIAL : How to have a default block contents
« Reply #10 on: May 18, 2010, 01:07:02 PM »
thanks pcwacht
that was what a setting i was missing

erpe:
maybe anchor could be moved to a snippet / added in fckeditor

br
elarifr
Accedinfo.com - Société de maintenance informatique sur Strasbourg pour particuliers et entreprises. Tel +33 3 88 33 77 45

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
Re: TUTORIAL : How to have a default block contents
« Reply #11 on: May 18, 2010, 01:08:04 PM »
@elarifr shure... and they did it almost 6 month after I originaly post this here. ;)
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

Offline Xagone

  • Posts: 482
  • Gender: Male
  • Developper
    • Xagone Inc
Re: TUTORIAL : How to have a default block contents
« Reply #12 on: May 18, 2010, 01:15:28 PM »
here's MY final solution :
Code: [Select]
place_content(1,'YOUR DEFAULT CONTENT');
even if I never done any snippet, I think this is kind of one :
Code: [Select]
// place this
function place_content($content_id,$content_default = false) {
  ob_start();
  page_content($content_id);
  $out1 = ob_get_contents();
  ob_end_clean();

  if(empty(str_replace('<a class="section_anchor" id="wb_'.$content_id.'" name="wb_'.$content_id.'"></a>','',$out1)) && $content_default != false) {
    echo $content_default;
  } else { echo $out1; }
}
?>
Xagone Inc. (formerly VotreEspace)
http://xagone.com/

 

postern-length