{"id":779,"date":"2011-04-09T09:34:00","date_gmt":"2011-04-09T09:34:00","guid":{"rendered":"https:\/\/www.keenertech.com\/?p=779"},"modified":"2011-04-09T09:34:00","modified_gmt":"2011-04-09T09:34:00","slug":"recipe-page-specific-content-for-the-html-head-section","status":"publish","type":"post","link":"https:\/\/staging.keenertech.com\/?p=779","title":{"rendered":"Recipe: Page-Specific Content for the HTML Head Section"},"content":{"rendered":"\n<p>Rails layouts are a nice way to organize boiler-plate HTML content in a DRY (&#8220;Don&#8217;t Repeat Yourself&#8221; in case you&#8217;ve been asleep for a while) fashion. But your standard layout may not be sufficient to meet the needs of all web pages, particularly when it comes to including CSS style sheets or JavaScript files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Problem<\/h3>\n\n\n\n<p>In a Rails 3.x application, how can you provide an easy method for web pages to include page-specific CSS style sheets and JavaScript files within the &lt;head&gt; section of the HTML?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Solution<\/h3>\n\n\n\n<p>Here&#8217;s a straight-forward solution to the problem:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   &lt;!DOCTYPE html&gt;\n   &lt;html&gt;\n   &lt;head&gt;\n     &lt;title&gt;Sample&lt;\/title&gt;\n     &lt;%= stylesheet_link_tag :all %&gt;\n     &lt;%= javascript_include_tag :defaults %&gt;\n     %lt;%= csrf_meta_tag %&gt;\n     &lt;%= yield :head %&gt;\n   &lt;\/head&gt;\n   &lt;body&gt;\n\n   &lt;%= yield %&gt;\n\n   &lt;\/body&gt;\n   &lt;\/html&gt;\n<\/pre>\n\n\n\n<p>Note the &#8220;yield :head&#8221; statement in the &lt;head&gt; section of the document. With this construct in place, any web page can add custom content to the &lt;head&gt; element of the page.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &lt;% content_for :head do %&gt;\n    &lt;%= javascript_include_tag 'custom.css' %&gt;\n  &lt;% end %&gt;\n<\/pre>\n\n\n\n<p>In the example above, a custom CSS file is added for one specific web page.<\/p>\n\n\n\n<p>With this method, no other page has to bear the burden of including a CSS file that will not be used on that page. For any page that does not provide content for the &lt;head&gt; element, well, no content will be inserted. This is a simple way to support per-page customization capabilities while only affecting the pages that need their own custom support files.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>To the extent possible, it&#8217;s beneficial to limit the number of CSS stylesheets your website has. So I&#8217;m not advocating tons of little CSS stylesheets for your website. But when one page has a bunch of special-purpose CSS, it&#8217;s good to segregate that code in a separate stylesheet and include it in only where it&#8217;s needed.<\/p>\n\n\n\n<p>The recipe described in this article serves to meet those occasional CSS needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails layouts are a nice way to organize boiler-plate HTML content in a DRY (&#8220;Don&#8217;t Repeat Yourself&#8221; in<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[160],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-technology","tag-rails"],"_links":{"self":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=779"}],"version-history":[{"count":0,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/779\/revisions"}],"wp:attachment":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}