Comments (9)
@Intrepidd is the proposed change scoped to rendering from a controller? Something like
def create
respond_to do |format|
format.turbo_stream { render turbo_stream: ..., layout: ... }
end
end
Since Turbo Streams are their own MIME type to Rails, the typical layout rendering system should suffice.
I've relied on a layout for this exact use case to render the FlashHash#alert and FlashHash#notice:
<%# app/views/layouts/application.turbo_stream.erb %>
<% if alert.present? %>
<%= turbo_stream.append "flash_messages" do %>
<%= flash_tag alert, type: :alert %>
<% end %>
<% end %>
<% if notice.present? %>
<%= turbo_stream.append "flash_messages" do %>
<%= flash_tag notice, type: :notice %>
<% end %>
<% end %>
<%= yield %>
The "application" part of the application.turbo_stream.erb
corresponds to the ApplicationController
, which most controllers inherit from. If a specific controller needs a different layout, you can declare something like articles.turbo_stream.erb
and it'll be rendered instead. I've had success with this without specifying a set of respond_to
blocks, and instead just assigning instance variables and letting Rails decide how to render.
Have you tried something like this? I'm not sure if it'll work while rendering from within a format.turbo_stream { ... }
block, but it might.
from turbo-rails.
@Intrepidd is the proposed change scoped to rendering from a controller? Something like
def create respond_to do |format| format.turbo_stream { render turbo_stream: ..., layout: ... } end endSince Turbo Streams are their own MIME type to Rails, the typical layout rendering system should suffice.
I've relied on a layout for this exact use case to render the FlashHash#alert and FlashHash#notice:
<%# app/views/layouts/application.turbo_stream.erb %> <% if alert.present? %> <%= turbo_stream.append "flash_messages" do %> <%= flash_tag alert, type: :alert %> <% end %> <% end %> <% if notice.present? %> <%= turbo_stream.append "flash_messages" do %> <%= flash_tag notice, type: :notice %> <% end %> <% end %> <%= yield %>
The "application" part of the
application.turbo_stream.erb
corresponds to theApplicationController
, which most controllers inherit from. If a specific controller needs a different layout, you can declare something likearticles.turbo_stream.erb
and it'll be rendered instead. I've had success with this without specifying a set ofrespond_to
blocks, and instead just assigning instance variables and letting Rails decide how to render.Have you tried something like this? I'm not sure if it'll work while rendering from within a
format.turbo_stream { ... }
block, but it might.
Sorry for reviving this, but this should still work?
I'm doing some POC and it never calls application.turbo_stream.erb
Thanks!
from turbo-rails.
I did this very naive approach for now :
main...Intrepidd:feature/layout
Some pain points :
- Can't use
options[:layout]
as it already contains informations about the layout used in the controller - Can't properly use
yield
in the layout code and had to use regular locals instead. Didn't find a way in rails to render a template with a layout when the template is actually a string of already rendered HTML
I probably missed a lot of things, if someone gives me pointers on a better way to achieve this I'll be happy to look at it.
from turbo-rails.
Thanks for coming back to me !
So this doesn't work if you explicitly call render
inside your action. it will bypass the layout.
This indeed works if you have an action.turbo_stream.erb
file though and don't explicitly render from the controller
from turbo-rails.
I guess, as long as you put something in a template file.
I personally prefer not using a template file for turbo stream responses and use render turbo_stream:
but there are ways to accomplish what I initially described, namely :
# application_controller.rb
def turbo_flashes
# Add some logic to only replace if there is a flash, add error flashes, etc
turbo_stream.replace("flash_success", partial: "flash/success")
end
# some_controller.rb
def some_action
respond_to do |format|
format.html
format.turbo_stream do
render turbo_stream: turbo_flashes + turbo_stream.replace("x", ...)
end
end
end
This should do the trick for now
Thanks for turbo ! Happy to discover new ways of using it every day :)
from turbo-rails.
Pretty interesting idea. Would like to see what an implementation could look like without getting weird 👍
from turbo-rails.
We'd have to solve at least yield for this to be a tenable path. Feel free to open a PR if you manage that.
from turbo-rails.
Oh ha, that's even better. Just rely on the existing mechanics 👍
from turbo-rails.
It'll work if you do render layout: "x"
inline, though, right? I think that's sufficient.
from turbo-rails.
Related Issues (20)
- Mixed Content Error with Turbo Drive
- OT: Attackers might be trying to steal your information from discuss.hotwire.dev HOT 2
- Turbo refresh can hijack user navigation HOT 3
- import "@hotwired/turbo-rails" errors HOT 1
- Using data-turbo-confirm on plain (non-form) buttons HOT 3
- Adding stimulus breaks a test HOT 1
- how do you install turboframes into a rails 5 project that migrated to rails 7 using only sprockets (skipping all the webpack & 6 bs)? HOT 2
- What are the controls effected before injecting `text/vnd.turbo-stream.html`?
- turbo_stream.remove_all Not generating proper selector for targets.
- idiomorph merges attributes on `<turbo-cable-stream-source>` and `connectedCallback()` doesn't run HOT 2
- Add morph to StreamActions HOT 5
- how do I batch multiple turbo stream broadcasts?
- <turbo-cable-stream-source> tag can affect visual layout HOT 1
- broadcast_update_to partial should be smarter and extract the inner_html, it feels like a bug
- Turbo not loading on Safari in production HOT 1
- Any Security Constraints to turbo_method: 'post' functionality?
- using the view transitions API? HOT 1
- Unable to render layouts when using inline turbo stream responses
- 8.0.5: cdn (and thus importmap pins) has broken import paths "CbGBsmqA.js" instead of cable.js
- Turbo Frame + Nginx Cache + data-action-advance
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from turbo-rails.