Recent Topics

Why do functions echo instead of return strings?

Started by on Oct 09, 2003 – Contents updated: Oct 09, 2003

Oct 09, 2003 04:23    

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

Oct 09, 2003 13:34

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.

Oct 09, 2003 16:43

How should I go about posting changes? Just copy and paste the updated function into a forum post?

Oct 09, 2003 21:49

here's an example of the convention:

/*
 * 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.

Oct 09, 2003 22:22

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?

/*
 * 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

Oct 10, 2003 00:01

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 ;)

Thanks for your contribution. :)

Oct 10, 2003 16:31

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

Oct 10, 2003 19:28

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.

Oct 10, 2003 19:59

Gotcha... hehe, I'm sort of a tinkerer, so I try to come up with most open system I can think of! :)


Form is loading...

Build your own website! – This forum is powered by b2evolution CMS, a complete engine for your website.