0

(Preamble: Am new to PHP, coming from a C# background where I am used to very clean code. Am currently working on my own Wordpress site which has a purchased theme.)

I have seen this type of code in a WordPress theme:

<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo"/></a>

I find this very hard to read compared to the refactored:

<?php
            echo '<a href="';
            echo esc_url( home_url( '/' ) ); 
            echo "><img src=";
            echo esc_url( $logo ); 
            echo " alt=";
            echo esc_attr( get_bloginfo( 'name' ) ); 
            echo '" id="logo"/></a>'
?>

But this is the easiest by far:

<?php
        get_anchor($url, $imgsource, $alt, $id);
?>

get_anchor being a custom function that echos an anchor configured according to the parameters.

But surely I am not the first to think of this. Are there any existing libs that have a set of functions that return properly formatted html like in this example? Is there something I am missing?

6
  • 1
    Are there any existing libs that do this? -- do what? Commented Oct 20, 2013 at 18:10
  • 1
    WordPress has a very extensive API, and there are plenty of themes out there which don't take full advantage of it. Commented Oct 20, 2013 at 18:12
  • 1
    For an attachment link, use codex.wordpress.org/Function_Reference/wp_get_attachment_link Commented Oct 20, 2013 at 18:14
  • @CBroe, I know what you are saying and I originally write it without the echo. But the echoless version provides less typing and it's pretty obvious what it is doing. Commented Oct 20, 2013 at 18:26
  • “less typing” can hardly be an argument when the subject is clean code. And what’s even worse, a function like this mixes data with the desired output format – maybe that link will need an additional id attribute at some point, or the image an additional class or whatever – and you will have to go change your function. And then even worse than that, if you were to use that function in multiple places, but only one needs the change – what are you gonna do then? (Please don’t say don’t say “simple, I’ll just add an additional parameter to the function” now …) Commented Oct 20, 2013 at 18:30

3 Answers 3

1

I've written a function that returns a HTML tag based on the pure PHP output:

function tag($name, $attrs, $content) {
    $res = '';
    $res .= '<' . $name;
    foreach($attrs as $key => $val)
        $res .= ' ' . $key . '="' . $val . '"';

    $res .= isset($content) ? '>' . $content . '</'.$name.'>' : ' />';

    return $res;
}
  • $name is the tagname (e.g. a)
  • $attrs is a key, value array with attributes (e.g. array('href','http://google.com/'))
  • $content is the content / body of the tag (an other element or text)

Example basic use:

echo tag('a', array('href' => 'http://google.com/'),'Google');

Example nested use with multiple children:

echo tag('ul',array(),
        tag('li',array(),'one') . 
        tag('li',array(),'two') . 
        tag('li',array(),'three')
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice. I am going to try this out.
0

I believe what you are looking for are templates like Smarty. They are the cleanest way to display information as code and view are completely separated.

However Wordpress do not use them, I don't know why actually, probably because most PHP programmers are not used to it.

Comments

0

Most of the PHP frameworks provide such libraries to out put html through parameterized functions, most of them are part of view layer if the framework follows MVC pattern.

but if you are not using any of the framework then you may use these libraries from here

PHP Pear Packages

And for building forms in particular see

HTML_QuickForm2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.