A better Phlex layout pattern
Context necessary: Phlex, Rails
Phlex guides like to do this callback pattern to adhere to Rails view layout conventions:
class MyLayout < Phlex::HTML
def around_template
doctype
html {
head { }
body { super }
}
end
end
class MyView < MyLayout
def view_template = p("hello world")
endThis felt weird to me, what is this mysterious around_template and why are we overwriting and calling super. So, I came up with a more straightforward alternative. Top level views define "view_body" and no callback/monkeypatch necessary. The method being more descriptive is an added bonus. This is how I am writing division929.
class MyLayout < Phlex::HTML
def view_template
doctype
html do
head { }
body { view_body }
end
end
end
class MyView < MyLayout
def view_body = p("hello world")
end