New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sitepress does not seem to work with Phlex based layouts #48
Comments
Here's a fresh repro repo, each step in the process is a commit: https://github.com/johnmcdowall/sitepress-phlex-layout-bug |
Here's how I got this working in one of my projects: class SiteController < Sitepress::SiteController
layout false
protected
def pre_render(rendition)
rendition.output = render_to_string(layout do
render_to_string inline: rendition.source, type: rendition.handler
end)
end
def layout(&)
# You might have a different class name.
PageView.new \
title: current_page.data.fetch("title"),
subtitle: current_page.data["subtitle"],
&
end
end I'm still thinking through the pre-render API because there's cases where people want to render a page within a page to extract bits of it via Nokogiri, etc. Curious what feedback you have to make it clearer. I think the code snippet above isn't super clear, but it does work. One problem with this approach is the For example, in Rails with Erb files, a page might have this:
But in Phlex it might look like this:
I think that's OK? But something feels off about it, especially if people are running Sitepress inside of a Rails app with both Phlex and Erb. |
I guess it depends on what you, the lib author, want the behaviour to be. Do you specifically want to force Phlex users to eject and customize, or do you want to bake magic into Sitepress? My PR makes it magical and just works, but I can see a case for either way to be honest. With regards to the Personally I'd rather not have to eject SitePress just for this, I'd expect it to work out of the box, and I'd rather not have to learn about |
Btw my PR will fall back to expected ERB lookup behaviour if the layout is not actually a Phlex component. So it covers all bases, even if you are using ERB and Phlex in the same project. |
I was playing around with this today, tired Let's say a page has this frontmatter:
This would look up the class SiteController < Sitepress::SiteController
layout false
protected
def article_layout(page)
ArticleView.new title: page.data.fetch("title"), subtitle: page.data["subtitle"] do
render_resource_inline page
end
end
def page_layout(page)
PageLayout.new title: page.data.fetch("title"), subtitle: page.data["subtitle"] do
render_resource_inline page
end
end
private
def render_resource_with_handler(resource)
render layout_component(resource)
end
def render_resource_inline(resource)
render inline: resource.body, type: resource.handler
end
def layout_component(resource)
# rendition.resource.data.fetch("layout", "PageView").constantize
method_name = resource.data.fetch("layout", "page").concat("_layout")
layout_method = self.method(method_name)
layout_method.call resource
end
end I think this approach could be used with Erb renditions, have sensible fallbacks, and remain performant. Using Phlex would require ejecting the controller to have a place to add the view constructors, but that's fine. I also think the ejected controller would have the Phlex integration packaged up into a module so it would look like this: class SiteController < Sitepress::SiteController
layout false
include Sitepress::Phlex
protected
def article_layout(page)
ArticleView.new title: page.data.fetch("title"), subtitle: page.data["subtitle"] do
render_resource_inline page
end
end
def page_layout(page)
PageLayout.new title: page.data.fetch("title"), subtitle: page.data["subtitle"] do
render_resource_inline page
end
end
end There's some other cleanup too, like the |
Started work on this in branch https://github.com/sitepress/sitepress/tree/simplify-renderer |
Hello - thanks for the super cool gem!
Just trying it out on a new Rails 7.1 app, and following the docs for installation of Sitepress and Phlex. Default Sitepress routes are in play in the
routes.rb
file.I set my
ApplicationController
to use theApplicationLayout
view as shown in the docs:And indeed I have an
application_layout.rb
file underapp/views/layouts/appliation_layout.rb
, which contains the following:But straight away I'm getting a
Template is missing
exception saying theApplicationLayout
can't be found:Maybe this is a bug with using Sitepress?
I see this in the stack trace:
Relevant section of stack trace:
Seems like Sitepress isn't expecting a callable constant as a Layout?
The text was updated successfully, but these errors were encountered: