Skip to content

Commit 0829202

Browse files
authored
Add several layout hooks to provide extra content (#2574)
This commit allows us to add content in some additional places, from around the header to around the main body, which means users can more easily customise their templates without needing to override the whole file.
1 parent 8ce0514 commit 0829202

File tree

7 files changed

+114
-66
lines changed

7 files changed

+114
-66
lines changed

app/views/administrate/application/_index_header.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<%= content_for(:title) %>
88
</h1>
99

10+
<%= content_for(:header_middle) %>
11+
1012
<% if show_search_bar %>
1113
<%= render(
1214
"search",
@@ -25,4 +27,6 @@
2527
class: "button",
2628
) if accessible_action?(new_resource, :new) %>
2729
</div>
30+
31+
<%= content_for(:header_last) %>
2832
</header>

app/views/administrate/application/edit.html.erb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,27 @@ It displays a header, and renders the `_form` partial to do the heavy lifting.
2222
<%= content_for(:title) %>
2323
</h1>
2424

25+
<%= content_for(:header_middle) %>
26+
2527
<div>
2628
<%= link_to(
2729
t("administrate.actions.show_resource", name: page.page_title),
2830
[namespace, page.resource],
2931
class: "button",
3032
) if accessible_action?(page.resource, :show) %>
3133
</div>
34+
35+
<%= content_for(:header_last) %>
3236
</header>
3337

34-
<section class="main-content__body">
35-
<%= render "form", page: page %>
36-
</section>
38+
<%= content_for(:before_main) %>
39+
40+
<% if content_for?(:main) %>
41+
<%= content_for(:main) %>
42+
<% else %>
43+
<section class="main-content__body">
44+
<%= render "form", page: page %>
45+
</section>
46+
<% end %>
47+
48+
<%= content_for(:after_main) %>

app/views/administrate/application/index.html.erb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,23 @@ It renders the `_table` partial to display details about the resources.
3232
)
3333
%>
3434

35-
<section class="main-content__body main-content__body--flush">
36-
<%= render(
37-
"collection",
38-
collection_presenter: page,
39-
collection_field_name: resource_name,
40-
page: page,
41-
resources: resources,
42-
table_title: "page-title"
43-
) %>
35+
<%= content_for(:before_main) %>
36+
37+
<% if content_for?(:main) %>
38+
<%= content_for(:main) %>
39+
<% else %>
40+
<section class="main-content__body main-content__body--flush">
41+
<%= render(
42+
"collection",
43+
collection_presenter: page,
44+
collection_field_name: resource_name,
45+
page: page,
46+
resources: resources,
47+
table_title: "page-title"
48+
) %>
49+
50+
<%= render("pagination", resources: resources) %>
51+
</section>
52+
<% end %>
4453

45-
<%= render("pagination", resources: resources) %>
46-
</section>
54+
<%= content_for(:after_main) %>

app/views/administrate/application/new.html.erb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,23 @@ to do the heavy lifting.
2727
<%= content_for(:title) %>
2828
</h1>
2929

30+
<%= content_for(:header_middle) %>
31+
3032
<div>
3133
<%= link_to t("administrate.actions.back"), :back, class: "button" %>
3234
</div>
35+
36+
<%= content_for(:header_last) %>
3337
</header>
3438

35-
<section class="main-content__body">
36-
<%= render "form", page: page %>
37-
</section>
39+
<%= content_for(:before_main) %>
40+
41+
<% if content_for?(:main) %>
42+
<%= content_for(:main) %>
43+
<% else %>
44+
<section class="main-content__body">
45+
<%= render "form", page: page %>
46+
</section>
47+
<% end %>
48+
49+
<%= content_for(:after_main) %>

app/views/administrate/application/show.html.erb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ as well as a link to its edit page.
2323
<%= content_for(:title) %>
2424
</h1>
2525

26+
<%= content_for(:header_middle) %>
27+
2628
<div>
2729
<%= link_to(
2830
t("administrate.actions.edit_resource", name: page.page_title),
@@ -38,28 +40,38 @@ as well as a link to its edit page.
3840
data: { turbo_confirm: t("administrate.actions.confirm") }
3941
) if accessible_action?(page.resource, :destroy) %>
4042
</div>
43+
44+
<%= content_for(:header_last) %>
4145
</header>
4246

43-
<section class="main-content__body">
44-
<dl>
45-
<% page.attributes.each do |title, attributes| %>
46-
<fieldset class="<%= "field-unit--nested" if title.present? %>">
47-
<% if title.present? %>
48-
<legend><%= t "helpers.label.#{page.resource_name}.#{title}", default: title %></legend>
49-
<% end %>
47+
<%= content_for(:before_main) %>
48+
49+
<% if content_for?(:main) %>
50+
<%= content_for(:main) %>
51+
<% else %>
52+
<section class="main-content__body">
53+
<dl>
54+
<% page.attributes.each do |title, attributes| %>
55+
<fieldset class="<%= "field-unit--nested" if title.present? %>">
56+
<% if title.present? %>
57+
<legend><%= t "helpers.label.#{page.resource_name}.#{title}", default: title %></legend>
58+
<% end %>
59+
60+
<% attributes.each do |attribute| %>
61+
<dt class="attribute-label" id="<%= attribute.name %>">
62+
<%= t(
63+
"helpers.label.#{resource_name}.#{attribute.name}",
64+
default: page.resource.class.human_attribute_name(attribute.name),
65+
) %>
66+
</dt>
5067

51-
<% attributes.each do |attribute| %>
52-
<dt class="attribute-label" id="<%= attribute.name %>">
53-
<%= t(
54-
"helpers.label.#{resource_name}.#{attribute.name}",
55-
default: page.resource.class.human_attribute_name(attribute.name),
56-
) %>
57-
</dt>
68+
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
69+
><%= render_field attribute, page: page %></dd>
70+
<% end %>
71+
</fieldset>
72+
<% end %>
73+
</dl>
74+
</section>
75+
<% end %>
5876

59-
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
60-
><%= render_field attribute, page: page %></dd>
61-
<% end %>
62-
</fieldset>
63-
<% end %>
64-
</dl>
65-
</section>
77+
<%= content_for(:after_main) %>

docs/customizing_page_views.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,28 @@ rails generate administrate:views:layout
9494
# It only generates the sidebar partial
9595
# -> app/views/admin/application/_navigation.html.erb
9696
```
97+
98+
## Customizing for a specific layout
99+
100+
You can use several hook points to add elements to specific layouts or specific pages:
101+
102+
* header_middle
103+
* header_last
104+
* before_main
105+
* main
106+
* after_main
107+
108+
For example, you can add a button in the middle of the header as follows:
109+
110+
```eruby
111+
<%# app/views/admin/customers/_index_header.html.erb %>
112+
113+
<% content_for(:header_middle) do %>
114+
<div>
115+
You are logged in as <em><%= pundit_user.name %></em>.
116+
<%= link_to("Become the Admin", become_admin_customer_path("admin")) unless pundit_user.admin? %>
117+
</div>
118+
<% end %>
119+
120+
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>
121+
```
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
<% content_for(:title) do %>
2-
<%= display_resource_name(page.resource_name) %>
3-
<% end %>
4-
5-
<header class="main-content__header">
6-
<h1 class="main-content__page-title" id="page-title">
7-
<%= content_for(:title) %>
8-
</h1>
9-
1+
<% content_for(:header_middle) do %>
102
<div>
113
You are logged in as <em><%= pundit_user.name %></em>.
124
<%= link_to("Become the Admin", become_admin_customer_path("admin")) unless pundit_user.admin? %>
135
</div>
6+
<% end %>
147

15-
<% if show_search_bar %>
16-
<%= render(
17-
"search",
18-
search_term: search_term,
19-
resource_name: display_resource_name(page.resource_name)
20-
) %>
21-
<% end %>
22-
23-
<div>
24-
<%= link_to(
25-
t(
26-
"administrate.actions.new_resource",
27-
name: display_resource_name(page.resource_name, singular: true).downcase
28-
),
29-
[:new, namespace, page.resource_path.to_sym],
30-
class: "button",
31-
) if accessible_action?(new_resource, :new) %>
32-
</div>
33-
</header>
8+
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>

0 commit comments

Comments
 (0)