Rendering templates in Rails? #145
Replies: 3 comments
|
Hey Josh, Yes, that's correct. We do plan to add a plugin API (probably closer to v1.16), which would, in turn, allow us to add support for HTML rendering. However, taking into account that we are focused on APIs, it would be a pretty basic functionality without layouts or implicit rendering. Until then, you can use the following code added to an initializer to support HTML rendering: module RenderHTML
def render(html: nil, **)
if html
super(plain: html)
headers["content-type"] = "text/html; charset=utf-8"
else
super(**)
end
end
end
RageController::API.prepend(RenderHTML)This can be used to render ERB templates: module RenderTemplate
def render(template: nil, **)
if template
super(plain: find_template(template).result(binding))
headers["content-type"] = "text/html; charset=utf-8"
else
super(**)
end
end
def find_template(path)
@templates ||= {}
@templates[path] ||= ERB.new(Rage.root.join("app/views/#{path}.html.erb").read)
end
end
RageController::API.prepend(RenderTemplate) |
|
With Rails integration, the recommendation is to use Rage controllers specifically for API functionality. If the application also has a full-stack part, it is best to have Rails controllers manage that and then use |
|
@rsamoilov this is great, thank you so much for all the details! 🙏 |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I was quite excited to find this gem, however, looking through the docs, it seems that Rage (integrated with Rails) does not support any form of HTML rendering. Is that correct? Are there any plans to add that capability?
Thanks!
All reactions