[wp-hackers] no 'before' or 'after' parameters for wp_list_pages()?

Jeremy Clarke jer at simianuprising.com
Mon Nov 30 18:19:20 UTC 2009


On a related note I recently wrote a simple function to handle what seems to
be a common trope for the template tags api:

the_{object}($before = '', $after = '', $echo = TRUE);

Almost all the_* functions use this and IMHO its a good system for display
functions. I started using it in my own display functions and got sick of
duplicating the if ($before) logic, so I wrote this instead:


/**
 * Handle $before $after and handle $echo parameters for display functions
 *
 * If $output is empty nothing will happen.
 *
 * @param string $output actual stuff to display
 * @param string $before html to show before $output
 * @param string $after html to show after $output
 * @param boolean $echo if FALSE return instead of printing
 */
function gv_before_after_echo ($output, $before = '', $after = '', $echo =
TRUE) {

    if (!$output) return

    $final_output = '';
    if ($before)
        $final_output .= $before;

    $final_output .= $output;

    if ($after)
        $final_output .= $after;

    // use $echo to determine if it should be returned or printed

    if (!$echo)
        return $final_output;

    echo $final_output;
}


This means that instead of having logic in each function you just generate
the actual output without $before and $after, then pass it to this, like

function the_thing($output, $before = '', $after = '', $echo = TRUE) {
 $output = get_thing();
 return gv_before_after_echo($output, $before, $after, $echo);
}

IMHO having this in core would simplify things a lot and would make it easy
to create intelligent template tags. Like wp_parse_args() I think it would
also have the effect of getting more people to write more extensible
template tags rather than forgetting to include $before and $after.

I haven't written a patch for it yet, so let me know if you have any ideas
as to why its a dumb idea or anything. It would be a big project to convert
every template tag from the start, but we could switch a few and catch the
others as they come around.



-- 
Jeremy Clarke | http://jeremyclarke.org
Code and Design | http://globalvoicesonline.org


More information about the wp-hackers mailing list