Sandwich Coding Standards

时间:2014-05-24 作者:Tom J Nowell

我最近发现code for a wp-lunch.php file 用于WordPress模板,如果遵循适当的WordPress编码标准,这会是什么样子?

wp-lunch.php

<?php if ( current_user_can(\'has_sandwich\') ): ?>

    <?php get_sandwich_header(); ?>

        <?php while( has_filling() ): the_filling(); ?>

            <?php get_sandwich_part( \'thick_layer\',
    get_filling() ); ?>

        <?php endwhile; ?>

    <?php get_sandwich_footer(); ?>

<?php endif; ?>

3 个回复
最合适的回答,由SO网友:gmazzap 整理而成

如果用户没有能力吃三明治会怎么样?WSOF?

如果我想遵循一般的默认主题模板,我会选择

// eat-sandwich.php (as @Rarst said avoid wp-lunch.php as it\'s not part of WP core)

get_header( \'sandwich\' );

if ( current_user_can( \'eat_sandwich\' ) ) {

  get_template_part( \'eat-sandwich\', \'content\' );

} else { // user can\'t eat sandwich. An apple?

  $alternative = apply_filters( \'alternative_to_sandwich\', \'apple\' );

  if ( \'sandwich\' == $alternative ) {
     // No sandwich allowed!
     $alternative = \'apple\';
  }

  get_template_part( "eat-$alternative", \'content\' );

}

get_footer( \'sandwich\' );
然后

// eat-sandwich-content.php

$fillings = get_fillings_query(); // in functions.php

if ( $fillings->have_posts() ) : while ( $fillings->have_posts() ) :

   get_template_part( \'filling\', get_filling_type() );

endwhile;

wp_reset_postdata();

else :

  _e( \'Sorry, no fillings found. Eating an apple may help to stop hunger.\', \'txtdomain\');

endif;

SO网友:Rarst

<!-- file shouldn\'t be named wp-lunch.php as it\'s not part of WP core -->

<?php if ( current_user_can( \'eat_sandwich\' ) ): // more specific verb makes more sense to me ?>

    <?php get_header( \'sandwich\' ); // native function accepts type argument ?>

    <?php while ( have_fillings() ): the_filling(); // maybe native API, but feels acceptable wrapper for semantics ?>

        <?php get_template_part( \'filling\', get_filling_type() ); // native API, what would be `thick_layer` base? ?>

    <?php endwhile; wp_reset_postdata(); // reset $post global ?>

    <?php get_footer( \'sandwich\' ); // native function accepts type argument ?>

<?php endif; ?>
根据编码样式等调整间距。

带小枝的三明治模板,在上面吃Meadow 将看起来像:

{% if ( current_user_can( \'eat_sandwich\' ) ) %}

    {% include \'header-sandwich.twig\' %}

    {% loop fillings %}

        {% include \'filling-\' ~ get_filling_type() ~ \'.twig\' ignore missing %}

    {% endloop %}

    {% include \'footer-sandwich.twig\' %}

{% endif %}

SO网友:GaryJ

如果已经缩进,则无需使用所有开头和结尾分隔符或清除行距:

<?php
if ( current_user_can( \'has_sandwich\' ) ) {
    get_sandwich_header();
    while ( has_filling() ) {
        the_filling();
        get_sandwich_part( \'thick_layer\', get_filling() );
    }
    get_sandwich_footer();
}
之后可能也会重置填充数据。。。

结束

相关推荐