<br />
<b>Warning</b>:  The magic method SFML_Singleton::__wakeup() must have public visibility in <b>/home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php</b> on line <b>72</b><br />
{"id":153,"date":"2017-02-20T10:52:19","date_gmt":"2017-02-20T17:52:19","guid":{"rendered":"http:\/\/www.munderwood.ca\/?p=153"},"modified":"2017-02-20T10:52:19","modified_gmt":"2017-02-20T17:52:19","slug":"stacking-sections-in-laravel-blade-templates","status":"publish","type":"post","link":"https:\/\/www.munderwood.ca\/index.php\/2017\/02\/20\/stacking-sections-in-laravel-blade-templates\/","title":{"rendered":"Stacking sections in Laravel Blade templates"},"content":{"rendered":"<p>With Laravel&#8217;s Blade templating engine, it is possible to stack multiple sections with the same name throughout a template, and yield them all together in one place. This is particularly useful if you have multiple partial views that each need to append some JavaScript to a\u00a0<span class=\"lang:default decode:true crayon-inline \">&lt;script&gt;<\/span>\u00a0tag at the end of your page, for instance.<\/p>\n<h3>tl;dr<\/h3>\n<p>Unlike when using\u00a0the <span class=\"lang:default decode:true crayon-inline \">@section<\/span>\u00a0 Blade directives, there can be as many <span class=\"lang:default decode:true crayon-inline \">@push(&#8216;name&#8217;)<\/span>\u00a0 directives with the same name as you need. Using the <span class=\"lang:default decode:true crayon-inline \">@stack(&#8216;name&#8217;)<\/span>\u00a0 directive in place of <span class=\"lang:default decode:true crayon-inline \">@yield<\/span>\u00a0, will then pop them all out at once in a single location in your template (in reverse order).<\/p>\n<h2>Example usage<\/h2>\n<p>Suppose you&#8217;re using jQuery and that you have two partial views that each need a bit of JavaScript to run on page load:<\/p>\n<h3>Desired output<\/h3>\n<pre class=\"lang:default decode:true\" title=\"Desired output example\">&lt;!-- From first partial --&gt;\r\n&lt;div class=\"must-init\"&gt;\r\n    &lt;p&gt;Sample&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- From second partial --&gt;\r\n&lt;div class=\"needs-setup\"&gt;\r\n    &lt;p&gt;Example&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;script&gt;\r\n    $(function () {\r\n        \/\/ Required by first partial\r\n        $('.must-init').initialise();\r\n        \/\/ Required by second partial\r\n        $('.needs-setup').setup();\r\n    )};\r\n&lt;\/script&gt;<\/pre>\n<p>Adding the scripts adjacent to the views is problematic, because likely jQuery won&#8217;t be loaded when they are executed. You could try to always remember to yield two different sections in the main layout view, but that is rather error prone.<\/p>\n<h2>Enter stacks<\/h2>\n<p>By stacking the script sections in your views, the same output that you&#8217;d create above if you weren&#8217;t using partials is easy to achieve. First, <span class=\"lang:default decode:true crayon-inline \">@push<\/span>\u00a0 the script needed by each view onto the stack:<\/p>\n<h3>Partial template one<\/h3>\n<pre class=\"lang:default decode:true \" title=\"first_partial.blade.php\">&lt;!-- From first partial --&gt;\r\n&lt;div class=\"must-init\"&gt;\r\n    &lt;p&gt;Sample&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n@push('readyscripts')\r\n    \/\/ Required by first partial\r\n    $('.must-init').initialise();\r\n@endpush<\/pre>\n<h3>Partial template two<\/h3>\n<pre class=\"lang:default decode:true \">&lt;!-- From second partial --&gt;\r\n&lt;div class=\"needs-setup\"&gt;\r\n    &lt;p&gt;Example&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n@push('readyscripts')\r\n    \/\/ Required by second partial\r\n    $('.needs-setup').setup();\r\n@endpush<\/pre>\n<h2>Popping the stack<\/h2>\n<p>You can&#8217;t use <span class=\"lang:default decode:true crayon-inline \">@yield<\/span>\u00a0 to insert the pushed scripts\u00a0into another template, since they weren&#8217;t defined in <span class=\"lang:default decode:true crayon-inline \">@section<\/span>\u00a0 directives. Instead, simply use <span class=\"lang:default decode:true crayon-inline \">@stack<\/span>\u00a0 in otherwise the same way:<\/p>\n<pre class=\"lang:default decode:true \" title=\"Partial page layout to push scripts to\">&lt;script&gt;\r\n    $(function () {\r\n        @stack('readyscripts')\r\n    });\r\n&lt;\/script&gt;<\/pre>\n<p>One potentially important caveat to keep in mind is that since this is indeed a\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Stack_(abstract_data_type)\">stack<\/a>, the <em>last<\/em>\u00a0section that you push onto it will be the <em>first<\/em> to appear in your rendered output. So instead of obtaining an exact formulation of the code we initially wanted, in this example we&#8217;d end up with something like this:<\/p>\n<pre class=\"lang:default decode:true \">&lt;!-- From first partial --&gt;\r\n&lt;div class=\"must-init\"&gt;\r\n    &lt;p&gt;Sample&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- From second partial --&gt;\r\n&lt;div class=\"needs-setup\"&gt;\r\n    &lt;p&gt;Example&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;script&gt;\r\n    $(function () {\r\n        \/\/ Required by *second* partial\r\n        $('.needs-setup').setup();\r\n        \/\/ Required by *first* partial\r\n        $('.must-init').initialise();\r\n    )};\r\n&lt;\/script&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With Laravel&#8217;s Blade templating engine, it is possible to stack multiple sections with the same name throughout a template, and yield them all together in one place. This is particularly useful if you have multiple partial views that each need to append some JavaScript to a\u00a0&lt;script&gt;\u00a0tag at the end of your page, for instance. tl;dr &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.munderwood.ca\/index.php\/2017\/02\/20\/stacking-sections-in-laravel-blade-templates\/\">[Read more&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":13,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}