Problem with block rendering #1

Open
Titinux opened this Issue Jan 24, 2016 · 24 comments

Projects

None yet

5 participants

@Titinux
Titinux commented Jan 24, 2016

Maybe I misunderstood something but when I add

gem 'hamlit-block'

to my Gemfile the following template stop working and does not output anything.

- 10.times do
  %p= "Hey !"
@k0kubun
Contributor
k0kubun commented Jan 25, 2016

Thank you for trying hamlit and hamlit-block!

the following template stop working and does not output anything.

Yes. This is intentional in hamlit-block because @apotonick required the block to stop changing buffer to be rendered and allow capturing via not rails' way but just calling yield in k0kubun/hamlit#53. This means that you can render contents inside block only when yield is called like this way.

I've guessed the design of cells and trailblazer is making helper calling yield and using them for all blocks you want to render.
@apotonick What do you do for the case in this issue's description?

@Titinux
Titinux commented Jan 25, 2016

If I want to use cells with hamlit (require hamlit-block) I can't use rails standards templates anymore ? It's a bit problematic, no ?

@apotonick Do I need to convert all the templates to cells ?

@apotonick

No, you should be able to simply replace Haml with Hamlit, you don't have to convert everything. Does that work for you?

@Titinux
Titinux commented Jan 27, 2016

I've set up a repository with a basic example. https://github.com/Titinux/hamlittest

This should render the words list with the standard template and with a cell. Simple tags (h1 / h2 / ...) works but as soon as I want to render a list there is no output

- @words.each do |word|
  %p= word
@k0kubun
Contributor
k0kubun commented Jan 27, 2016

you should be able to simply replace Haml with Hamlit, you don't have to convert everything

As I said in k0kubun/hamlit#53 (comment), original Haml does not have a behavior like Slim's disable_capture: false and hamlit-block is disable_capture: false for Hamlit and breaking compatibility against Haml.

I think cells-slim's capture actually uses not yield's result but with_output_buffer's result because slim uses disable_capture: true for rails. We may need the same thing as https://github.com/trailblazer/cells-slim/blob/v0.0.4/lib/cell/slim/rails.rb#L9-L23.

@Titinux By the way, did you try cells-hamlit v0.1.0? It doesn't require hamlit-block and uses rails' capture. While I'll try to fix hamlit-block and cells-hamlit to solve this issue, it's possible that cells-hamlit v0.1.0 suits your use case for now.

@Titinux
Titinux commented Jan 27, 2016

If I pin cells-hamlit gem to version 0.1.0 everything is working. 👍
If I can help to solve problem with 0.1.1 version tell me.

Thanks for taking the time to investigate this problem and btw hamlit is amazing.

@apotonick

@Titinux Are you using Formular?

@Titinux
Titinux commented Jan 29, 2016

Not yet, I'm still with simple_form.

@apotonick

I think I know what we should do now.

The current behavior in hamlit-block is what I find the best way to implement blocks in any template language. This is also evidenced in other languages, such as Slim. It makes it very simple for library authors to have a reliable block semantic without any weird output_buffer redirection mess.

I remember getting Haml to work with my stuff in Cells was a nightmare.

What if hamlit_block would have a method iterate or something which gives the original behavior?

- iterate|capture do
  - 10.times do
    = "Hey!"

Is that possible, @k0kubun ?

I strongly believe that the current capture == yield semantic is correct. Output buffers are horrible to work with, and this must be handled in the template engine itself.

In ERBse, I could implement the same behaviour. Many Cells users won't mind this, because they use collection cells anyway to render lists.

@k0kubun
Contributor
k0kubun commented Feb 1, 2016

What if hamlit_block would have a method iterate or something which gives the original behavior?
Is that possible, @k0kubun ?

It's possible but it's still a breaking change. I don't want users to change all templates like #1 (comment) to use iterate. It'll be too hard to migrate to cells.

This is also evidenced in other languages, such as Slim. It makes it very simple for library authors to have a reliable block semantic without any weird output_buffer redirection mess.

I think cells-slim also uses output_buffer redirection https://github.com/trailblazer/cells-slim/blob/v0.0.4/lib/cell/slim/rails.rb#L19. If you think cells-slim's design is ok, cells-hamlit can use the same design to keep backward compatibility. Maybe https://github.com/trailblazer/cells-slim/blob/v0.0.4/lib/cell/slim/rails.rb#L9-L23 will work in Hamlit too.

@mrGrazy
mrGrazy commented Mar 13, 2016

We've been trying to use cells-hamlit and hamlit-block too, and wondering what to do with each loops in our views. This issue is that each doesn't return anything useful, so hamlit-blocks semantics don't produce any output. map/collect are better, but they return an array which needs to be joined into a string.

So we did this hackery:

Enumerable.class_eval do
  def map_join(&block)
    map(&block).join
  end
end

Which allows us to change this:

- results.each do |row|
  %tr
    - columns.each do |field|
      = concept 'filter_table/value', row, field: field

to:

= results.map_join do |row|
  %tr
    = columns.map_join do |field|
      = concept 'filter_table/value', row, field: field

This is the most painless way I've figured out how to use cells-hamlit with our existing templates

@k0kubun
Contributor
k0kubun commented Mar 14, 2016

Yes, that's true. The problem will not happen in normal (non-cells, no hamlit-block opt-in) templates if cells-hamlit changes hamlit-block to be removed for non-cells templates as I commented in trailblazer/cells-hamlit#2 (comment). I'll try it later.

But I don't know how to write such template properly for cells, which can't be separated with hamlit-block feature. Is there a helper or plugin to write it easily in cells? @apotonick

@apotonick

The thing is, we don't necessarily have to include hamlit-block in Cells-hamlit, but then I have to find another way how to make block capturing work properly across a non-monolithic environment (which Cells is). No matter how you turn it, it will cause problems for me.

I would much more prefer an explicit approach where each loops capture their content on the Hamlit level and per default, blocks return their content (as discussed in trailblazer/cells-hamlit#2).

@k0kubun
Contributor
k0kubun commented Mar 15, 2016

If we can find another way how to make block capturing work properly across a non-monolithic environment (which will don't require users to rewrite all their templates like #1 (comment)), I want to make the behavior by default too :)

@samstickland

@k0kubun Any further thoughts on this? Is the only option to have both of these:

  • passings blocks to helpers
  • a working enumerable method (ala each)

the Enumerable map_join monkey patch above?

@k0kubun
Contributor
k0kubun commented May 23, 2016

@samstickland I guess you're using cells-hamlit on rails. In that case, have you tried cells-hamlit v0.1.0?
See: #1 (comment) and #1 (comment).

@samstickland

@k0kubun Hi, thanks for the response.

I tried installing cells-hamlit 0.1.0 but that ends up resolving to a requirement for cells ~> 4.0, and I'm currently running 4.1.1

@apotonick

We can remove hamlit-block from cells-hamlit.

@k0kubun
Contributor
k0kubun commented May 23, 2016

I tried installing cells-hamlit 0.1.0 but that ends up resolving to a requirement for cells ~> 4.0, and I'm currently running 4.1.1

Ah, I confirmed that cells-hamlit depends on cells ~> 4.0.0.

We can remove hamlit-block from cells-hamlit.

I've forgotten the context until now. The "further thoughts" was trailblazer/cells-hamlit#2 (comment). It will work well but not it is implemented for now.

@samstickland

Removing hamlit-block from cells-hamlit doesn't help with this issue though?

  • Without hamlit-block (explicitly in my Gemfile) passing blocks to link_to doesn't work
  • With hamlit-block (explicitly in my Gemfile) each in haml doesn't output anything
@apotonick

Hello my friend @k0kubun - I saw that hamlit-block doesn't play with hamlit > 2.4. What's the idea behind that, I wonder?

@k0kubun
Contributor
k0kubun commented Jun 19, 2016

I saw that hamlit-block doesn't play with hamlit > 2.4. What's the idea behind that, I wonder?

Hi. In spite of hamlit-block's dependency, hamlit-block works well with hamlit > 2.4. So I just relaxed the hamlit-block's dependency. Could you try hamlit-block v0.3.1?

@apotonick

Thanks @k0kubun! <3 Hamlit-block 0.3.1 works fine with 2.5 in my Hanami/TRB app, but for some strange reason, the same does not work in a Sinatra/TRB app. Here, when I capture content it simply disappears and is not printed. Any idea where that comes from?

@apotonick

Stop, I take it back! Everything works like a charm in both applications. Cool, thanks so much! ❤️

@Titinux Titinux referenced this issue in trailblazer/cells Jun 26, 2016
Open

form_tag and submit_tag broken with Hamlit #419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment