Skip to content

Commit

Permalink
Add option to add different layout to view_templates (#1145)
Browse files Browse the repository at this point in the history
* Add option to add different layout to view_templates
  • Loading branch information
Bramjetten authored Dec 11, 2022
1 parent aa99685 commit 89c507a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
23 changes: 19 additions & 4 deletions app/controllers/concerns/spina/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,27 @@ def redirect_or_render_404
end
end

def render_404
render file: "#{Rails.root}/public/404.html", status: 404, layout: false
def render_with_template(page)
render layout: "#{current_theme.name.parameterize.underscore}/#{layout_template_for_page(page)}", template: "#{current_theme.name.parameterize.underscore}/pages/#{view_template_for_page(page)}"
end

def render_with_template(page)
render layout: "#{current_theme.name.parameterize.underscore}/#{page.layout_template || "application"}", template: "#{current_theme.name.parameterize.underscore}/pages/#{Spina::Current.page.view_template || "show"}"
def view_template_for_page(page)
page.view_template.presence || "show"
end

def layout_template_for_page(page)
return page.layout_template if page.layout_template.present?
view_template_config(page)[:layout].presence || "application"
end

def view_template_config(page)
current_theme.view_templates.find do |template|
template[:name] == view_template_for_page(page)
end
end

def render_404
render file: "#{Rails.root}/public/404.html", status: 404, layout: false
end
end
end
5 changes: 0 additions & 5 deletions app/models/spina/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ def cache_key
super + "_" + Mobility.locale.to_s
end

def view_template_config(theme)
view_template_name = view_template.presence || "show"
theme.view_templates.find { |template| template[:name] == view_template_name }
end

private

def set_default_position
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/demo/pages/different_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
View template with a different layout
16 changes: 16 additions & 0 deletions test/dummy/app/views/layouts/demo/different_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title><%= current_spina_account.name %></title>
<%= stylesheet_link_tag 'demo/application', media: 'all', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'demo/shared/languages' %>
<%= render 'demo/shared/navigation' %>
ohi
This is a different layout

<%= yield %>
</body>
</html>
5 changes: 5 additions & 0 deletions test/dummy/config/initializers/themes/demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
description: "Article template",
parts: ["body"],
exclude_from: %w[main]
}, {
name: "different_layout",
title: "Different layout",
parts: %w[body],
layout: "different_layout"
}]

theme.custom_pages = [{
Expand Down

0 comments on commit 89c507a

Please sign in to comment.