为什么有人会使用printf输出html而不是将PHP分解为实际的html?
|
从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()
)
);
}
?>
为什么有人要这么做?
为什么不执行以下操作呢?
<?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
}
?>
后者对我来说更干净。为什么有人会改用第一种方法呢?仅仅是个人喜好,还是有一些功能上的好处?
没有找到相关结果
已邀请:
3 个回复
妊辽剁茧
的调用中,有一个对
的调用,这是WordPress的翻译功能。 这样,翻译人员只需移动
部分即可轻松地移动每个字符串的各个部分,以符合其语言的语法和结构。然后,将转换后的格式字符串传递给
,后者可以插入适当的变量。 WordPress的翻译文档页面上有一些翻译方面的示例(尽管字符串比较简单)。 并非所有翻译都严格需要翻译,但是由于他们已经在做一些printf风格的事情,所以我认为理论是,如果要翻译的话,会更容易理解。最不一致。
撵穆
桔马牛
和
。 附带说明一下,此代码示例的真正问题是它如何从函数内部输出HTML。...我本来会使用\“ view \”文件(例如:
)来对代码进行正确分离和组织。