Skip to content

Commit

Permalink
Add block variable for body attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Green committed Mar 23, 2017
1 parent a7f530f commit 975db35
Show file tree
Hide file tree
Showing 22 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can [view a collection of auto-generated examples](http://alphagov.github.io

## Requirements

The Ruby language (1.9.3+), the build tool [Rake](http://rake.rubyforge.org/) & the dependancy management tool [Bundler](http://bundler.io/)
The Ruby language (1.9.3+), the build tool [Rake](http://rake.rubyforge.org/), the dependency management tool [Bundler](http://bundler.io/) & [Java](https://www.java.com/en/)

## Detailed Docs

Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/django_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def self.statement_tag_for(key, default_value)
@@yield_hash = {
after_header: block_for(:after_header),
body_classes: block_for(:body_classes),
body_attributes: block_for(:body_attributes),
body_start: block_for(:body_start),
body_end: block_for(:body_end),
content: block_for(:content),
Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/ejs_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.partial_for(key)
@@yield_hash = {
after_header: partial_for(:after_header),
body_classes: "<%= bodyClasses %>",
body_attributes: "<%= bodyAttributes %>",
body_start: partial_for(:body_start),
body_end: partial_for(:body_end),
content: partial_for(:content),
Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/jinja_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.unescaped_statement_tag_for(key, default_value)
@@yield_hash = {
after_header: block_for(:after_header),
body_classes: block_for(:body_classes),
body_attributes: block_for(:body_attributes),
body_start: block_for(:body_start),
body_end: block_for(:body_end),
content: block_for(:content),
Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/liquid_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.include_for(key)
@@yield_hash = {
after_header: include_for(:after_header),
body_classes: include_for(:body_classes),
body_attributes: include_for(:body_attributes),
body_start: include_for(:body_start),
body_end: include_for(:body), # Note that this differs from the key!
content: "{{ content }}",
Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/mustache_inheritance_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.tag_for(lowerCamelCaseKey, default_value="")
@@yield_hash = {
after_header: tag_for(:afterHeader),
body_classes: tag_for(:bodyClasses),
body_attributes: tag_for(:bodyAttributes),
body_start: tag_for(:bodyStart),
body_end: tag_for(:bodyEnd),
content: tag_for(:content),
Expand Down
1 change: 1 addition & 0 deletions build_tools/compiler/mustache_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def self.unescaped_html_tag_for(lowerCamelCaseKey)
@@yield_hash = {
after_header: unescaped_html_tag_for(:afterHeader),
body_classes: tag_for(:bodyClasses),
body_attributes: unescaped_html_tag_for(:bodyAttributes),
body_start: unescaped_html_tag_for(:bodyStart),
body_end: unescaped_html_tag_for(:bodyEnd),
content: unescaped_html_tag_for(:content),
Expand Down
3 changes: 2 additions & 1 deletion build_tools/compiler/play_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class PlayProcessor < TemplateProcessor
# top_of_page has a special purpose: it is required by Play to define the
# parameters to pass when rendering
# https://www.playframework.com/documentation/2.2.x/ScalaTemplates#Template-parameters
top_of_page: '@(title: Option[String], bodyClasses: Option[String], htmlLang: Option[String] = None)(head:Html, bodyStart:Html, bodyEnd:Html, insideHeader:Html, afterHeader:Html, footerTop:Html, footerLinks:Html, headerClass:Html = HtmlFormat.empty, propositionHeader:Html = HtmlFormat.empty, homepageUrl:Option[Html] = None, globalHeaderText:Option[Html] = None, cookieMessage: Option[Html] = None, skipLinkMessage:Option[Html], logoLinkTitle:Option[Html] = None, licenceMessage:Html, crownCopyrightMessage:Option[Html])(content:Html)',
top_of_page: '@(title: Option[String], bodyClasses: Option[String], bodyAttributes: Option[String], htmlLang: Option[String] = None)(head:Html, bodyStart:Html, bodyEnd:Html, insideHeader:Html, afterHeader:Html, footerTop:Html, footerLinks:Html, headerClass:Html = HtmlFormat.empty, propositionHeader:Html = HtmlFormat.empty, homepageUrl:Option[Html] = None, globalHeaderText:Option[Html] = None, cookieMessage: Option[Html] = None, skipLinkMessage:Option[Html], logoLinkTitle:Option[Html] = None, licenceMessage:Html, crownCopyrightMessage:Option[Html])(content:Html)',
head: '@head',
body_classes: '@bodyClasses.getOrElse("")',
body_attributes: '@bodyAttributes.getOrElse("")',
header_class: '@headerClass',
proposition_header: '@propositionHeader',
content: '@content',
Expand Down
2 changes: 1 addition & 1 deletion build_tools/packager/webjar_packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def prepare_contents
end
File.open(@target_dir.join('VERSION'), 'w') {|f| f.write "#{GovukTemplate::VERSION}\n" }
end

def process_template(file)
target_dir = @internal_dir.join(File.dirname(file))
target_dir.mkpath
Expand Down
1 change: 1 addition & 0 deletions docs/template-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| page_title | Text inside the `<title>` element | GOV.UK - The best place to find government services and information
| head | Before closing `</head>` element | Insertion point
| body_classes | Classes to be added to the `<body>` element | Insertion point
| body_attributes | Attributes to be added to the `<body>` element | Insertion point
| body_start | After opening `<body>` element | Insertion point
| skip_link_message | Text inside the skip to main content link | Skip to main content
| cookie_message | Text inside the cookie message banner | `<p>GOV.UK uses cookies to make the site simpler. <a href="https://www.gov.uk/help/cookies">Find out more about cookies</a></p>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def rendered_html_files
end

it "should allow setting_body_classes" do
expect(subject).to have_tag("body", with: { class: "custom_body_class" })
expect(subject).to have_tag("body", with: { class: "custom_body_class", custom_body_attribute: 'test' })
end

it "should allow inserting content into body_start" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% block body_classes %}custom_body_class{% endblock body_classes %}

{% block body_attributes %}custom_body_attribute="test"{% endblock %}

{% block body_start %}<inserted-into-body-start></inserted-into-body-start>{% endblock body_start %}

{% block cookie_message %}Custom cookie message{% endblock cookie_message %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% block body_classes %}custom_body_class{% endblock body_classes %}

{% block body_attributes %}custom_body_attribute="test"{% endblock %}

{% block body_start %}<inserted-into-body-start></inserted-into-body-start>{% endblock body_start %}

{% block cookie_message %}Custom cookie message{% endblock cookie_message %}
Expand Down
1 change: 1 addition & 0 deletions integration_tests/integrations/mustache/test_render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
pageTitle: "This is a custom page title",
head: "<inserted-into-head></inserted-into-head>",
bodyClasses: "custom_body_class",
bodyAttributes: 'custom_body_attribute="test"',
bodyStart: "<inserted-into-body-start></inserted-into-body-start>",
cookieMessage: "Custom cookie message",
headerClass: "custom_header_class",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ compiledTemplate = Hogan.compile("\
{{$pageTitle}}This is a custom page title{{/pageTitle}} \
{{$head}}<inserted-into-head></inserted-into-head>{{/head}} \
{{$bodyClasses}}custom_body_class{{/bodyClasses}} \
{{$bodyAttributes}}custom_body_attribute=\"test\"{{/bodyAttributes}} \
{{$bodyStart}}<inserted-into-body-start></inserted-into-body-start>{{/bodyStart}} \
{{$cookieMessage}}Custom cookie message{{/cookieMessage}} \
{{$headerClass}}custom_header_class{{/headerClass}} \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% block body_classes %}custom_body_class{% endblock %}

{% block body_attributes %}custom_body_attribute="test"{% endblock %}

{% block body_start %}<inserted-into-body-start></inserted-into-body-start>{% endblock %}

{% block cookie_message %}Custom cookie message{% endblock %}
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/integrations/rails/views/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<% content_for(:body_classes, "custom_body_class") %>
<% content_for(:body_attributes) do %>custom_body_attribute="test"<% end %>
<% content_for(:body_start) do %><inserted-into-body-start></inserted-into-body-start><% end %>
<% content_for(:cookie_message, "Custom cookie message") %>
Expand Down
2 changes: 1 addition & 1 deletion source/views/layouts/govuk_template.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<%= yield :head %>
</head>

<body<%= content_for?(:body_classes) ? raw(" class=\"#{yield(:body_classes)}\"") : '' %>>
<body<%= content_for?(:body_classes) ? raw(" class=\"#{yield(:body_classes)}\"") : '' %> <%= content_for?(:body_attributes) ? raw("#{yield(:body_attributes)}") : '' %>>
<script>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>

<%= yield :body_start %>
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/django_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def valid_sections
{
after_header: "{% block after_header %}{% endblock %}",
body_classes: "{% block body_classes %}{% endblock %}",
body_attributes: "{% block body_attributes %}{% endblock %}",
body_end: "{% block body_end %}{% endblock %}",
content: "{% block content %}{% endblock %}",
cookie_message: "{% block cookie_message %}{% endblock %}",
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/ejs_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def valid_sections
page_title: "<%- partial('partials/_page_title') %>",
head: "<%- partial('partials/_head') %>",
body_classes: "<%= bodyClasses %>",
body_attributes: "<%= bodyAttributes %>",
content: "<%- partial('partials/_content') %>",
body_end: "<%- partial('partials/_body_end') %>",
footer_top: "<%- partial('partials/_footer_top') %>",
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/jinja_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def valid_sections
{
after_header: "{% block after_header %}{% endblock %}",
body_classes: "{% block body_classes %}{% endblock %}",
body_attributes: "{% block body_attributes %}{% endblock %}",
body_end: "{% block body_end %}{% endblock %}",
content: "{% block content %}{% endblock %}",
cookie_message: "{% block cookie_message %}{% endblock %}",
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/mustache_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def valid_sections
page_title: "{{ pageTitle }}",
head: "{{{ head }}}",
body_classes: "{{ bodyClasses }}",
body_attributes: "{{{ bodyAttributes }}}",
content: "{{{ content }}}",
body_end: "{{{ bodyEnd }}}",
top_of_page: "{{{ topOfPage }}}",
Expand Down

0 comments on commit 975db35

Please sign in to comment.