Comment renvoyer le contenu de la boucle
-
-
[Jene pensepas que lamiseen mémoiretampon de sortie soit unproblème deperformances significatif] (http://stackoverflow.com/questions/1502955/does-output-buffering-in-php-require-more-resources).Il y a une surcharge supplémentairepour stocker le contenuen mémoire,mais celane serapasperceptiblepour un articletypiqueet sonbalisage.Deplus,gardez à l'esprit que l'utilisation d'unplugin demiseen cachefait quetouteperformance atteint unnon-problème.Ilesttoujoursimportant depenser à l'efficacité,maisilestplusimportant d'écrire du codemaintenable.Ainsi,lamiseen mémoiretampon de sortieest définitivementpréférable aux commentaires HEREDOC ou à d'autres hacks horribles.[I don't think output buffering is a significant performance issue](http://stackoverflow.com/questions/1502955/does-output-buffering-in-php-require-more-resources). There is some extra overhead to store the content in memory, but it's not going to be noticeable for a typical post and its markup. Also, keep in mind that using a caching plugin makes any performance hit a non-issue. It's still important to think about efficiency, but it's more important to write maintainable code. So, output buffering is definitely preferable to HEREDOC comments or other ugly hacks.
- 1
- 2012-09-13
- Ian Dunn
-
1 réponses
- votes
-
- 2012-06-30
Ilexiste des remplacements qui retournent des chaînespurespourtoutes lesparties,pasbesoin d'imprimer quoi que ce soit dans untampon de sortie. J'aime
sprintf()
et j'écrirais votreexemple comme ceci:<?php if ( $cms_pl_pages->have_posts() ) { $content = '<section class="cms-pl-gallery">'; while ( $cms_pl_pages->have_posts() ) { $cms_pl_pages->the_post(); $content .= sprintf( '<article class="cms-pl-item clearfix"> %1$s <h2> <a href="%2$s" title="Read %3$s">%4$s</a> </h2> %5$s </article>', get_the_post_thumbnail(), apply_filters( 'the_permalink', get_permalink() ), the_title_attribute( array( 'echo' => FALSE ) ), the_title( '', '', FALSE ), has_excerpt() ? apply_filters( 'the_excerpt', get_the_excerpt() ) : '' ); } $content .= '</section> <!-- end .cms-pl-gallery -->'; } wp_reset_postdata(); return $content;
There are replacements that return pure strings for all parts, no need to print anything into an output buffer. I like
sprintf()
and would write your example like this:<?php if ( $cms_pl_pages->have_posts() ) { $content = '<section class="cms-pl-gallery">'; while ( $cms_pl_pages->have_posts() ) { $cms_pl_pages->the_post(); $content .= sprintf( '<article class="cms-pl-item clearfix"> %1$s <h2> <a href="%2$s" title="Read %3$s">%4$s</a> </h2> %5$s </article>', get_the_post_thumbnail(), apply_filters( 'the_permalink', get_permalink() ), the_title_attribute( array( 'echo' => FALSE ) ), the_title( '', '', FALSE ), has_excerpt() ? apply_filters( 'the_excerpt', get_the_excerpt() ) : '' ); } $content .= '</section> <!-- end .cms-pl-gallery -->'; } wp_reset_postdata(); return $content;
Parfois,je dois renvoyer la sortie d'uneboucle (généralement avec
WP_Query
comme dans cetexemple)pour l'utiliser dans un shortcode ou avec unfiltre surle_contenu
.Le code suivant qui utilise lamiseen mémoiretampon d'objetsfonctionne,maisj'ai lu à d'autresendroits que lamiseen mémoiretamponpeut êtreinefficace. J'ai aussi vu HEREDOC,maisje ne voispas comment celafonctionneraitici àmoins d'avoir d'abordenregistré chaquebalise demodèleen tant que variable (ce qui semble ànouveauinefficace).
Ma questionest donc: quelleest lameilleurefaçon de renvoyer la sortie d'uneboucle?