Skip to content

Commit

Permalink
Remove deferred render entirely (#805)
Browse files Browse the repository at this point in the history
This abstraction is too confusing. If you understand the concept, you
can still use it by yielding early.

```ruby
def view_template
  vanish { yield }

  # ...
end
```

This PR also changes the callbacks so `before_template` is given the
block and can yield it.

```ruby
def before_template
  vanish { yield }
end
```

You can put this in a module called `DeferredRender` if you want. It’s
up to you.

---------

Co-authored-by: Will Cosgrove <[email protected]>
  • Loading branch information
joeldrapper and willcosgrove authored Oct 3, 2024
1 parent 7f166d7 commit 42228c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
1 change: 0 additions & 1 deletion lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Phlex
autoload :CSV, "phlex/csv"
autoload :Callable, "phlex/callable"
autoload :Context, "phlex/context"
autoload :DeferredRender, "phlex/deferred_render"
autoload :DoubleRenderError, "phlex/errors/double_render_error"
autoload :Elements, "phlex/elements"
autoload :Error, "phlex/error"
Expand Down
5 changes: 0 additions & 5 deletions lib/phlex/deferred_render.rb

This file was deleted.

28 changes: 14 additions & 14 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,23 @@ def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: n
end

@_context.around_render do
before_template(&block)

around_template do
if block
if Phlex::DeferredRender === self
vanish(self, &block)
view_template
else
view_template do |*args|
if args.length > 0
__yield_content_with_args__(*args, &block)
else
__yield_content__(&block)
end
view_template do |*args|
if args.length > 0
__yield_content_with_args__(*args, &block)
else
__yield_content__(&block)
end
end
else
view_template
end
end

after_template(&block)
end

unless parent
Expand Down Expand Up @@ -235,7 +234,11 @@ def render(renderable = nil, &)
def vanish(*args)
return unless block_given?

@_context.capturing_into(Phlex::BlackHole) { yield(*args) }
if args.length > 0
@_context.capturing_into(Phlex::BlackHole) { yield(*args) }
else
@_context.capturing_into(Phlex::BlackHole) { yield(self) }
end

nil
end
Expand All @@ -252,10 +255,7 @@ def format_object(object)
end

def around_template
before_template
yield
after_template

nil
end

Expand Down
14 changes: 7 additions & 7 deletions quickdraw/sgml/callbacks.test.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

class Example < Phlex::HTML
def around_template
def before_template
i { "1" }
end

def around_template
i { "2" }

super do
i { "3" }
yield
i { "5" }
end

i { "7" }
end

def before_template
i { "2" }
i { "6" }
end

def after_template
i { "6" }
i { "7" }
end

def view_template
Expand Down

0 comments on commit 42228c2

Please sign in to comment.