Sha256: 2ab34586eed0695f71893fa39c0a755063cf9559a786afcda499d2fb36e3b001

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Pages
	module Rails
		class Layouts < ApplicationPage
			def template
				render Layout.new(title: "Getting started with Rails") do
					render Markdown.new <<~MD
						# Layouts in Rails

						Rails has an implicit layouts feature that automatically wraps views in a layout template, usually `views/layouts/application.html.erb`. When using Phlex, you'll probably want to by-pass this feature completely. Here's why:

						Sometimes you need to pass some argument to the layout, such as a page title that needs to be rendered in a `<title>` tag in the HTML `<head>`. Rails lets you do this from another part of the view using the `content_for` feature, but this pattern precludes defining an explicit interface for your layout through its initializer.

						If layouts are Phlex views, they can be rendered just like any other view and can require that `title` argument from their initializer. The trick is the page view renders its content into the layout view.
					MD

					render Example.new do |e|
						e.tab "layout.rb", <<~RUBY
							module Views
								class Layout < Phlex::HTML
									def initialize(title:)
										@title = title
									end

									def template(&)
										html do
											head do
												title { @title }
											end

											body(&)
										end
									end
								end
							end
						RUBY

						e.tab "index.rb", <<~RUBY
							module Views
								class Index < Phlex::HTML
									def template
										render Layout.new(title: "Hello") do
											h1 { "👋" }
										end
									end
								end
							end
						RUBY

						e.execute "Views::Index.new.call"
					end

					render Markdown.new <<~MD
						If you're using a Phlex view as a layout, you'll want to disable layouts from your Rails controller. You can do this by adding `layout false` to your controller. In a new app, you'll probably want to add this to the `ApplicationController`.
					MD
				end
			end
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phlex-0.5.3 docs/pages/rails/layouts.rb
phlex-0.5.2 docs/pages/rails/layouts.rb
phlex-0.5.1 docs/pages/rails/layouts.rb
phlex-0.5.0 docs/pages/rails/layouts.rb