Can you stream HTML with Slim in a Sinatra application?

I have a Sinatra application that wraps a command line application. It has no users, so performance is not an issue.

I am using Sinatra streaming api to let me emit HTML code as the command runs. This means that the user starts to see progress as they run the command.

post "/reorder" do
  @project = params["project"]
  @id_or_range = params["id_or_range"]
  @output_log = "[OUTPUT]"

  before, after =  slim(:index).split(@output_log)

  stream do |out|
    out << before
    run(@project, @id_or_range, StreamOutput.new(out))
    out << after
  end
end

      

https://gist.github.com/NigelThorne/04775270abd46b78e262

Currently I am doing a hack where I draw the template (as if I had all the data) and then split the template text into which the data should be inserted. Then I render the beginning of the template and then render the data when I receive it (downstream) and then the end of the template.

Slim is supposed to support streaming ...

I would like to write.

post "/reorder" do
  ...
  stream do |out|
    out << slim(:index)
  end
end

      

or better

post "/reorder" do
  ...
  slim(:index, stream: true)
end

      

How do I get thin to succumb to the data stream when rendering, so I exit the template in one go?

+3


source to share


1 answer


Yes, you can overwrite the helper slim

in Sinatra. Cm:



+1


source







All Articles