三明治编码标准


8

我最近发现了用于wp-lunch.php 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; ?>

好的问题,让游戏开始吧!
亚当

我认为这里要问的完美问题应该是the_filling()什么样子
Pieter Goosen

那些WP开发人员在周末做什么?;)
Nicolai

Answers:


5

如果用户没有吃三明治的能力怎么办?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;

如果没有面包,此查询将中断,因为面包不被视为馅料和三明治的重要组成部分。我建议添加get_ingredients();而不是get_fillings_query();面包和馅料的一部分。此外,填充应具有前端JSON API;)
Wyck

5
<!-- 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; ?>

间距已根据编码样式等进行了调整。

草地上吃掉的带树枝的三明治模板看起来像:

{% 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 %}

3
所有这些都<?php让我发冷。
gmazzap

7
@GM我曾想过介绍Mustache模板,但是谁想要三明治中的胡子?
腊斯特2014年

4
如果我在三明治中找到一根树枝,我可以应付,但不可以。
亚当

1

当已经缩进时,不需要所有的开始和结束定界符或空白行:

<?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();
}

之后也应该重新设置填充数据...

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.