开发者

Why would anyone use printf to output html instead of breaking out of PHP into the actual html?

开发者 https://www.devze.com 2023-03-09 08:56 出处:网络
Look at this horrible-to-look-at code from wordpress\'s twentyten theme: <?php function twentyten_posted_on() {

Look at this horrible-to-look-at code from wordpress's twentyten theme:

<?php
function twentyten_posted_on() {
    printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
        'meta-prep meta-prep-author',
        sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
            get_permalink(),
            esc_attr( get_the_time() ),
            get_the_date()
        ),
        sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
            get_author_posts_url( get_the_author_meta( 'ID' ) ),
            sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
            get_the_author()
        )
    );
}
?>

Why would anyone want to do that??

Why not do the following instead?

<?php
function twentyten_posted_on() {
    ?>
    <span class="me开发者_高级运维ta-prep meta-prep-author">Posted on</span>
    <a href="<?php= get_permalink() ?>" title="<?php= esc_attr( get_the_time() ) ?>" rel="bookmark">
        <span class="entry-date">get_the_date()</span>
    </a>
    <span class="meta-sep">by</span>
    <span class="author vcard">
        <a class="url fn n" href="<?php= get_author_posts_url( get_the_author_meta( 'ID' ) ) ?>" title="<?php= esc_attr__( 'View all posts by '.get_the_author() ) ?>"><?php= get_the_author() ?></a>
    </span>
    <?php
}
?>

The latter is much cleaner to me. Why would anyone use the first method instead? Is it just personal preference, or is there some functional benefit?


It's written the way it is so it can be internationalized. You'll see that, inside the call to printf(), there's a call to __(), which is WordPress' translation function.

This way, the translators can easily move the portions of each string around, by just moving the %1$s parts, to comply with their language's grammar and structure. Then, the translated formatting string is passed to printf(), which can insert the appropriate variables.

WordPress' doc page on translation has some examples of the translator side of this (albeit with simpler strings).

Not all of what's going on there is strictly necessary for the translation, but since they're doing some things printf-style already, I guess the theory is that it's easier to understand if it's at least consistent.


I's only a matter of personal taste. The author using sprintf may have preferred it because it separates presentation from logic. I find your way easier to read (I also prefer to avoid using short open tags).


It depends on what you need to render. I personally use sprintf and printf when the first argument is a variable string, or a localized string.

On a side note, the real problem with this code sample is how it outputs HTML from inside a function.... I would have used a "view" file instead (ex: include $view_file;) so the code is properly separated and organised.

0

精彩评论

暂无评论...
验证码 换一张
取 消