Utilisation correcte du tampon de sortie
2 réponses
- votes
-
- 2012-02-02
Non,vousn'avezpasbesoin demiseen mémoiretampon de sortie dans ce cas.En règlegénérale:n'utilisezpas lamiseen mémoiretampon de sortie sauf si vous y êtes vraiment obligé.
Imaginez ce qui sepasse si quelqu'un d'autre utilise également lamiseen mémoiretampon de sortie d'unpluginet que cela croise le vôtre:
// plugin ob_start(); // later, you in your theme ob_start(); // you call a function where the plugin author hooked in to call: print ob_get_clean(); // you call *your*: return ob_get_clean(); // is is empty!
C'est vraiment difficile à déboguer.Évitez-le.
Vousn'avezpasbesoin d'unefonction distinctepour couvrir un simple
do_action()
.Écrivez simplementdo_action('roots_content_before');
dans votrethème.No, you don’t need output buffering in this case. As a rule of thumb: Don’t use output buffering unless you really have to.
Just imagine what happens if someone else uses output buffering too from a plugin and it crosses with yours:
// plugin ob_start(); // later, you in your theme ob_start(); // you call a function where the plugin author hooked in to call: print ob_get_clean(); // you call *your*: return ob_get_clean(); // is is empty!
This is really hard to debug. Avoid it.
You don’t need a separate function to cover a plain
do_action()
. Just writedo_action('roots_content_before');
in your theme.-
D'accord avectoschoici.N'utilisezpas detampon de sortie.Utilisez l'actionet filtrez les appels sinécessaire.Agree with toscho here. Don't use Output Buffering. Use action and filter calls where needed.
- 2
- 2012-02-02
- Scott
-
Salut @toscho J'apprécie vraiment l'entrée.J'avais seulement commencé à [lire] (http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide) sur lamiseen mémoiretampon de sortie,mais àpartir de cet article,ça sonnait comme làétait une augmentation desperformances.Qu'il yen ait réellement ounon,on diraitpresque que si vous contrôlez l'ensemble de l'application,ce seraitbien,mais comme WordPress a desmilliers deplugins,vouspourriez demander desennuis;) Merciencore!Hi @toscho I really appreciate the input. I had only started to [read](http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide) about output buffering, but from that article, it sounded like there was a performance increase. Whether there actually is or not, it almost sounds like if you are controlling the entire application then it would be good, but since WordPress has thousands of plugins, you just might be asking for trouble ;) Thanks again!
- 0
- 2012-02-02
- Zach
-
@Zach Cet articleesterroné dès lapremière ligne.:) Envoyez votre sortie à l'utilisateur dès quepossible.Dans lesthèmes avec du code detravail long,j'appellemême [`flush ()`] (http://www.php.net/flush) avantet après le code lentpour accélérer le rendu.@Zach This article is wrong from the first line. :) Send your output to the user as soon as possible. In themes with long working code I even call [`flush()`](http://www.php.net/flush) before and after the slow code to speed the rendering up.
- 4
- 2012-02-02
- fuxia
-
Les Enfoirés!;) Appréciez lesinformations supplémentaires sur ce @toschoThe bastards! ;) Appreciate the further insight into that @toscho
- 0
- 2012-02-03
- Zach
-
[Le renduprogressif viaplusieurs vidages] (http://www.phpied.com/progressive-rendering-via-multiple-flushes/)est unebonne lecture.[Progressive rendering via multiple flushes](http://www.phpied.com/progressive-rendering-via-multiple-flushes/) is a good read.
- 1
- 2012-02-03
- fuxia
-
toscho,vouspouvez avoir destampons de sortieimbriqués.Cen'estpas unebonne chose deperdre le contrôle :)toscho, you can have nested output buffers. It's not a good thing to lose control over though :)
- 0
- 2012-04-11
- offroff
-
@offroff Dans WP,quelqu'un d'autrepeut ajouter uneimbricationindésirable.Par conséquent,ne l'utilisezpas dans desplugins ou desthèmes.@offroff In WP somebody else may add an unwanted nesting. Therefore, don't use it in plugins or themes.
- 0
- 2012-04-11
- fuxia
-
Cette réponseest unpeutrompeuse.Ilignore les situations dans lesquelles vouspouvez utiliser OBtrèsbien.Parexemple,vous avez unefonction quigénère du HTMLet vous souhaitez renvoyer ce HTML.Tant que vousne déclenchezpas d'actions courantes oun'appelezpas defonctions WP commeget_header (),vous devriez utiliser OBpourproduire un codeplus lisible.This answer is a bit misleading. It ignores situations in which you can use OB just fine. For example you have a function that outputs some HTML, and you want to return that HTML. As long as you don't fire common actions or call WP functions like get_header() in it, you should use OB to produce more readable code.
- 0
- 2012-05-16
- onetrickpony
-
@OneTrickPony Selon les opérationsexactes que vousexécutezplustôt dans la limite demémoire avec lamiseen mémoiretampon de sortieparce que…ehbien…toutest stockéen mémoirejusqu'à ce que `ob_end_clean ()` soit appelé.Si vousen avez vraimentbesoin,utilisez-le.C'est ce quej'ai dit dansma réponse.:)@OneTrickPony Depending on the exact operations you run earlier into memory limit with output buffering because … well … everything is stored in memory until `ob_end_clean()` is called. If you really need it tough – use it. That's what I said in my answer. :)
- 0
- 2012-05-16
- fuxia
-
- 2015-03-12
La réponse de @toschoesttotalementfausse.
-
Lamiseen mémoiretampon de sortiepeut êtreimbriquée ,il y a pasbesoin de s'inquiéter des autresplugins.
-
Dans cepost de 2009 ,ilexiste unemanièretrès élégante de récupéreret demanipuler la sortiefinale de Wordpress.
@toscho's answer is totally wrong.
Output buffering can be nested, there's no need to worry about other Plugins.
In this post from 2009 there's a very elegant way to fetch and manipulate the final output of Wordpress.
-
C'est unpeu dur.Jepense que ce que @toscho aessayé de diren'estpas que vousne pouvezpasimbriquer,mais que sipour une raison quelconque vous appelez `ob_get_clean ()` avant le code deparexempleun autreplugin qui a démarré lepremier `ob_start ()` appelle `ob_get_clean ()` vous obtiendrez des résultatsinattendus.Il avertit simplement que vousne devriez vraiment l'utiliser que s'iln'y apas d'autremoyen caril y a desinconvénients que vouspourriezne pas remarquer au début.This is a bit harsh. I think what @toscho tried to say isn't that you can't nest, but that if for some reason you call `ob_get_clean()` before the code of e.g. another plugin which started the first `ob_start()` calls `ob_get_clean()` you'll get unexpected results. He just warns that you should really only use it if there is no other way as there are drawbacks which you might not notice at first.
- 1
- 2015-03-12
- kraftner
-
Avez-vous regardé le deuxième lien?Il y a unebellemanipulation qui répondexactement auproblème de cette question.Iln'y a rien d'inattendu lors de lanidification;essayez-le vous-même: `ob_start (); echo "0"; ob_start (); echo "a"; echo ob_get_clean (); écho "1"; $ a=ob_get_clean (); echo $ a; `Did you look at the second link? There's a nice handling that addresses exactly the problem of this question. There's nothing unexpected when nesting; try it yourself: `ob_start(); echo "0"; ob_start(); echo "a"; echo ob_get_clean(); echo "1"; $a = ob_get_clean(); echo $a;`
- 0
- 2015-03-12
- Robert
-
Pour être honnête -non,je ne l'aipasfait.Encore unefois,tout ce queje dis,c'est qu'ilexiste denombreusesfaçons def *** ça,alorsmieux vautne l'utiliser que lorsqu'iln'y apas d'autremoyen.Enpassant: c'est la raisonexactepour laquelle les réponsespar lien uniquement sontmal vuesici.Si vous aviez ajouté l'essentiel de vos liens à la réponse,cela auraitbeaucoup aidé.To be honest - no I didn't. Again all I am saying is that there are a lot of ways to f*** this up, so better only use it when there is no other way. On a sidenote: This is the exact reason why link-only answers are frowned upon here. If you had added the gist of your links to the answer that would have helped a lot.
- 2
- 2015-03-13
- kraftner
J'essaie d'utiliser des actionspour remplacer les appels defonction quej'ai actuellement dans unmodèle (pourfaciliter lamise àjour de certaines sections répliquées). Parexemple,dans
archives.php
j'ai ce qui suit:Vouspouvez voir quelques-unes desfonctions,comme
roots_content_before();
Dans unfichier séparé,j'ai ce qui suit:function roots_content_before() { do_action('roots_content_before'); }
et utilisez-le comme suit:
D'après ce quej'ai lu,surtout sije vais avoir degrosmorceaux de code,je devrais utiliser letampon de sortie,mais quandj'essaie defaire cela,je m'accroupis complètement:
Est-ce queje pense à cela complètementfaux? J'apprendsencore,maisj'essaye depuis unmoment sans succès. Toutpointeur dans labonne direction serait vraiment apprécié. Merci!