为什么有人会使用printf输出html而不是将PHP分解为实际的html?

tru*_*ktr 2 html php syntax wordpress printf

从wordpress的二十个主题看这个可怕的代码:

<?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()
        )
    );
}
?>
Run Code Online (Sandbox Code Playgroud)

为什么有人想这样做?

为什么不这样做呢?

<?php
function twentyten_posted_on() {
    ?>
    <span class="meta-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
}
?>
Run Code Online (Sandbox Code Playgroud)

后者对我来说更清洁.为什么有人会使用第一种方法呢?这只是个人偏好,还是有一些功能上的好处?

Joh*_*ess 18

它的编写方式是可以国际化的.你会看到,在调用中printf(),有一个调用__(),这是WordPress的翻译功能.

通过这种方式,翻译人员可以通过移动%1$s部件轻松移动每个弦的各个部分,以符合他们语言的语法和结构.然后,将转换的格式字符串传递给printf(),可以插入适当的变量.

关于翻译的WordPress'文档页面有一些翻译方面的例子(尽管有更简单的字符串).

并非所有正在发生的事情都是翻译所必需的,但由于他们已经做了一些printf风格的事情,我想理论是,如果它至少是一致的,那么它就更容易理解.