scriptygoddess

22 Nov, 2004

Conditional text despite content not being echoed

Posted by: Jennifer In: WordPress: Lessons Learned

One of the things Christine had asked me to add to pixelog, was, when no "previous" entry was available – to have the text still show up, but be "greyed out". I remembered a post Alex King had made on the hackers mailing list about capturing the text that some functions typically echo into a variable. What he suggested was to do the following:

<?php
ob_start();
the_title();
$my_title = ob_get_contents();
ob_end_clean();
print($my_title);
?>

This was the trick I needed. Here's the code I used do the "greyed out" effect. (The issue here, if you don't know – is that next_post() will simply echo nothing if there is no next post)

<?php
//determine if there is a next post
ob_start();
next_post();
$nextpost = ob_get_contents();
ob_end_clean();
//now print the appropriate link or greyed out text
if ($nextpost) {
next_post('&laquo; % | ','previous','no');
} else { ?>
<span class="grey">&laquo; previous</span> |
<?php } ?>

(Yes I know I'm using the text "previous" with the next_post function… that had to do with her preference of displaying the photos)

6 Responses to "Conditional text despite content not being echoed"

1 | Marcus

November 24th, 2004 at 2:09 pm

Avatar

Honestly, I've always found the output buffering method to be a last resort. But it also bugs me to no end that so many of the WP functions just print things out instead of returning them.

Ideally, why not modify the next_post() function to either return a link, or grayed out text. Seems cleaner to me.

2 | Jennifer

November 24th, 2004 at 2:39 pm

Avatar

I guess I was trying to avoid hacking the core code, or creating duplicate functions just so they didn't echo the text.

Just curious – why do you consider output buffering a last resort. What complications can occur by using it?

3 | Marcus

November 24th, 2004 at 4:20 pm

Avatar

It's probably just a personal preference of mine.

It may sound a bit too philosophical, or idealist, but in a way, certain things make me feel like I'm breaking the natural flow of the code. I don't like any kind of print() or echo() statements in functions, anything resembling goto statements is a big no in my book, I stay away from things such as continue; and break; unless it's in a switch statement (in the case of break; anyway). switch constructs should be kept as short as possible, break them down into functions. I'm an object fanatic and believe that "good code is code that is very readable"(* see footnote) and, thus, maintainable.

For example, I prefer tabs over spaces. They take up less space, and you can set the tab stop in most editors. I have mine set to 4.

My if statements are always formatted like this:

if (USERID_ROOT == $userid) // check for root user
{
// display the IP address of the poster
print("$ip");
}
else // not root user:
{
// display the "report post" link
print("$report_url");
}

1. the { and } are mandatory and always lined up in the same column, you would never see me do something like:

if (condition) {
echo('true');
}

2. the USERID_ROOT is a constant. Never use "magic numbers".

3. I always put the constant on the left side of the equation. If I were to make a typo, if ($userid = USERID_ROOT) would always evaluate as true, but if (USERID_ROOT = $userid) will cause an error message. (I picked this one up not too long ago, and it took me a while to get used to it)

4. I try to comment a lot. I don't think I'm necessarily overcommenting, but do I try to comment in a way so you could remove all code and just by reading the comments you can still understand the flow of the application.

I'm also a HUGE fan of using templates. Whether it's Sigma (my second favorite), Smarty (looked into it, found it too convoluted) or my home-grown, which I primarily use. Here is a good read on the topic.

Back to the original issue: I've used the output buffering trick once. I used the auto_prepend_file and auto_append_file php.ini settings to re-brand an existing web site for a company this investment firm had just bought. The problem was that the company and product names appeared all over the place. Hardcoded in PHP, and all throughout the database. So I used output buffering and did a simple str_replace() on the buffer. But to use that on a simple function just seems overkill to me.

I don't think there are any complications that could occur other than readability suffering a bit, perhaps.

Hmm. I realize that this comment has turned into a huge rant with little nutritional value, and not only that — it sounds like I like it "my way or the highway", but I'll post it anyway. :)

——

* When I started to program more seriously (in the 386 era) "good code" was still a lot about hand-optimizing assembly statements for time-critial portions of C or Turbo Pascal code. Granted, that was for a different market (software and in particular game development). Unrolling loops, using XOR AX,AX instead of MOV AX,0, avoiding CMP statements. In web development it's all about maintainability. It's far more cost-efficient to just create a web server farm than it is to spend additional $ on re-design, code cleanup, and slowly working new features into spaghetti code and convoluting the existing code ever so more.

4 | branson

December 3rd, 2004 at 9:20 pm

Avatar

I've never use the output buffering, just stick to templates for the sake of maintainability. Just installed WordPress and am looking forward to customizing. Glad I found your site!

5 | aeon

December 23rd, 2004 at 8:27 pm

Avatar

Hm. I am not really sure what your next_post function returns – if I were to write it, it would return a boolean value when called without arguments. But why not just do this:
< ?php $nextpost = next_post(); if(!empty($nextpost)) { next_post('« % | ','previous','no'); } else { ?>
« previous |
< ?php } ?>

6 | aeon

December 23rd, 2004 at 8:30 pm

Avatar

erk. didn't escape the tags… sorry. Meant this:
<?php
$nextpost = next_post();
if(!empty($nextpost))
{
    next_post('« % | ','previous','no');
}
else
{
    echo '<span class="grey">« previous</span> | ';
}
?>

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements