Giter Site home page Giter Site logo

Comments (9)

seanpdoyle avatar seanpdoyle commented on August 10, 2024 4

@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.

alextakitani avatar alextakitani commented on August 10, 2024 2

@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.

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.

Intrepidd avatar Intrepidd commented on August 10, 2024 1

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.

Intrepidd avatar Intrepidd commented on August 10, 2024 1

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.

Intrepidd avatar Intrepidd commented on August 10, 2024 1

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.

dhh avatar dhh commented on August 10, 2024

Pretty interesting idea. Would like to see what an implementation could look like without getting weird 👍

from turbo-rails.

dhh avatar dhh commented on August 10, 2024

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.

dhh avatar dhh commented on August 10, 2024

Oh ha, that's even better. Just rely on the existing mechanics 👍

from turbo-rails.

dhh avatar dhh commented on August 10, 2024

It'll work if you do render layout: "x" inline, though, right? I think that's sufficient.

from turbo-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.