Why do functions echo instead of return strings?

 
Post new topic   Reply to topic   printer-friendly view    b2evolution Forum Index -> Feature requests and Feedback
View previous topic :: View next topic  
Author Message
Travis S
Seasoned Poster
Seasoned Poster

Joined: 06 Oct 2003
Posts: 46
Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1 add or subtract from this member's reputationadd or subtract from this member's reputation

PostPosted: Wed Oct 08, 2003 21:23    Post subject: Why do functions echo instead of return strings? Reply with quote

Howdy,

I'm just curious if there was a specific reason why the functions such as "the_title()", "the_content()", etc., echo their contents instead of returning them?

I'm setting up b2evo on a site that will be displaying two columns of information. The posts start out in the upper left and should move to the right, then down one and left, then back to the right and so forth as they age. It would be helpful if the functions would return strings that I could put in the correct column before printing them.

I'm thinking the way to do this is to add an additional boolean attribute to the various functions that defaults to echo, but can be overridden to return a string. It could also be done so each function returns a string and prints out the result, but could be overridden to run silently.

Thoughts?
Travis
Back to top
View user's profile Send private message Visit poster's website AIM Address
fplanque
Core Developer
Core Developer

Joined: 13 Jun 2003
Posts: 863
Reputation: 193.4Reputation: 193.4 add or subtract from this member's reputationadd or subtract from this member's reputation
votes: 9

PostPosted: Thu Oct 09, 2003 6:34    Post subject: Reply with quote

there is no good reason for this. it's just b2 legacy.

some functions already have an optional boolean override parameter but not all.

If you want you can add similar params to the function that don't have it yet and contribute the mods to the codebase.

_________________
François - Rambling on... State of the Evolution - fplanque.com
b2evolution project maintainer / main developer

Follow b2evolution on twitter and become a fan of b2evolution on facebook
Back to top
View user's profile Visit poster's website
Travis S
Seasoned Poster
Seasoned Poster

Joined: 06 Oct 2003
Posts: 46
Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1 add or subtract from this member's reputationadd or subtract from this member's reputation

PostPosted: Thu Oct 09, 2003 9:43    Post subject: Reply with quote

How should I go about posting changes? Just copy and paste the updated function into a forum post?
Back to top
View user's profile Send private message Visit poster's website AIM Address
fplanque
Core Developer
Core Developer

Joined: 13 Jun 2003
Posts: 863
Reputation: 193.4Reputation: 193.4 add or subtract from this member's reputationadd or subtract from this member's reputation
votes: 9

PostPosted: Thu Oct 09, 2003 14:49    Post subject: Reply with quote

here's an example of the convention:
Code:
/*
 * locale_charset(-)
 */
function locale_charset( $disp = true )
{
   global $current_charset;
   
   if( $disp )
      echo $current_charset;
   else
      return $current_charset;
}


To contribute changes to b2evolution you need to register on sourceforge and set up a CVS client. I'll add your SF username to the dev team, so you'll be able to check in modifications into the CVS codebase.

_________________
François - Rambling on... State of the Evolution - fplanque.com
b2evolution project maintainer / main developer

Follow b2evolution on twitter and become a fan of b2evolution on facebook
Back to top
View user's profile Visit poster's website
Travis S
Seasoned Poster
Seasoned Poster

Joined: 06 Oct 2003
Posts: 46
Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1 add or subtract from this member's reputationadd or subtract from this member's reputation

PostPosted: Thu Oct 09, 2003 15:22    Post subject: Reply with quote

Ok... I already have a SF account: tswicegood.

Here's what I've done with the_title() function. Am I out in leftfield, or is this formating ok?

Code:
/*
 * the_title(-)
 *
 * Display post title
 */
function the_title(
   $before='#',          // HTML/text to be displayed before title
   $after='#',          // HTML/text to be displayed after title
   $add_link = '#',       // Added link to this title?
   $format = '#',          // Format to use (example: "htmlbody" or "xml")
   $run_silent = false )   // Operate silently?
{
//////
//   ADDED: 03.10.08 by Travis S.
//      Created shorthand defaults to stream PHP-code in display.
   if( $before == '#' ) $before = '';
   if( $after == '#' ) $after = '';
   if( $add_link == '#' ) $add_link = true;
   if( $format == '#' ) $format = 'htmlbody';
   global $postdata;
   
   $title = get_the_title();
   $url = trim($postdata['Url']);

   if( empty($title) && $add_link )
   {
      $title = $url;
   }

   if( empty($title) )
      return;

   if( $add_link && (!empty($url)) )
   {
      $title = $before.'<a href="'.$url.'">'.$title.'</a>'.$after;
   }
   else
   {
      $title = $before.$title.$after;
   }

//////
//   ADDED: 03.10.08 by Travis S
//      Support for silent operation
   $return_str = format_to_output( $title, $format );
   if( $run_silent == false )
      echo $return_str;
   
   return $return_str;
}


Travis S
Back to top
View user's profile Send private message Visit poster's website AIM Address
fplanque
Core Developer
Core Developer

Joined: 13 Jun 2003
Posts: 863
Reputation: 193.4Reputation: 193.4 add or subtract from this member's reputationadd or subtract from this member's reputation
votes: 9

PostPosted: Thu Oct 09, 2003 17:01    Post subject: Reply with quote

Can you please use $disp = true instead of $run_silent = false ? It would be better to stick to the existing convention across the whole app.

I'm not sure about the value of the shorthands, especially when '#' is a shorthand for '' ... The only one I would keep is for 'htmlbody'.

I've given you access to CVS. Please use it wisely Wink

Thanks for your contribution. Smile

_________________
François - Rambling on... State of the Evolution - fplanque.com
b2evolution project maintainer / main developer

Follow b2evolution on twitter and become a fan of b2evolution on facebook
Back to top
View user's profile Visit poster's website
Travis S
Seasoned Poster
Seasoned Poster

Joined: 06 Oct 2003
Posts: 46
Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1 add or subtract from this member's reputationadd or subtract from this member's reputation

PostPosted: Fri Oct 10, 2003 9:31    Post subject: Reply with quote

I'll change the $run_silent to $disp. I just didn't look through the code hard enough to find the current convention I guess.

As to the shorthand, at first I started to leave it out or just use ''. There are intstances when someone would want to use a blank field for an attribute, but I could see it as entirely possible that they would have a default that is not a blank field. For example, $before and $after could have a <span class...> tag as their default, but could need to use a blank field in one spot on their blog.

Another thing I was thinking is that it would be a good idea to create a /conf/_function_defaults.php file containing a bunch of arrays with the default function settings. This would move all of the defaults for each setting into a seperate file and keep people out of the function files that they might be able to mess up if they don't know what they are doing. I don't think you can call globals in as the default for a function attribute can you?

Travis
Back to top
View user's profile Send private message Visit poster's website AIM Address
fplanque
Core Developer
Core Developer

Joined: 13 Jun 2003
Posts: 863
Reputation: 193.4Reputation: 193.4 add or subtract from this member's reputationadd or subtract from this member's reputation
votes: 9

PostPosted: Fri Oct 10, 2003 12:28    Post subject: Reply with quote

Travis, you are introducing unecessary complexity.

People are NOT supposed to change the DEFAULTs. That's why they are defaults. They can ovveride them in the templates. If however they feel a need to edit the core functions and for some reason run into the confict you describe with '', then they can also add the '#' handling themselves. However, I am sure 99+ % of the users never will edit the core functions at all. So no need to add extra complexity there.

_________________
François - Rambling on... State of the Evolution - fplanque.com
b2evolution project maintainer / main developer

Follow b2evolution on twitter and become a fan of b2evolution on facebook
Back to top
View user's profile Visit poster's website
Travis S
Seasoned Poster
Seasoned Poster

Joined: 06 Oct 2003
Posts: 46
Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1Reputation: 55.1 add or subtract from this member's reputationadd or subtract from this member's reputation

PostPosted: Fri Oct 10, 2003 12:59    Post subject: Reply with quote

Gotcha... hehe, I'm sort of a tinkerer, so I try to come up with most open system I can think of! Smile
Back to top
View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    b2evolution Forum Index -> Feature requests and Feedback All times are GMT - 5 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
b2evolution Support Forum RSS Feed Forums powered by php Bulletin Board