Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow injecting request context to the dry-view view #4

Merged
merged 1 commit into from
Jul 3, 2019

Conversation

waiting-for-dev
Copy link
Owner

This adds a view_context setting to WebPipe::Conn which value is a
lambda taking the instance as argument. Unless an explicit context:
argument is given to the #view method, it is used as injected
arguments for Dry::View::Context#with.

For example:

class MyContext < Dry::View::Context
  attr_reader :current_path

  def initialize(current_path: nil, **options)
    @current_path = current_path
    super
  end
end

class SayHelloView < Dry::View
  config.paths = [File.join(__dir__, '..', 'templates')]
  config.template = 'say_hello'
  config.default_context = MyContext.new

  expose :name
end

\#...
  def render(conn)
    conn.view(SayHelloView.new, name: 'Joe') # `current_path`
    # will be available in the view scope
  end
\#...

This adds a `view_context` setting to `WebPipe::Conn` which value is a
lambda taking the instance as argument. Unless an explicit `context:`
argument is given to the `#view` method, it is used as injected
arguments for `Dry::View::Context#with`.

For example:

```ruby
class MyContext < Dry::View::Context
  attr_reader :current_path

  def initialize(current_path: nil, **options)
    @current_path = current_path
    super
  end
end

class SayHelloView < Dry::View
  config.paths = [File.join(__dir__, '..', 'templates')]
  config.template = 'say_hello'
  config.default_context = MyContext.new

  expose :name
end

\#...
  def render(conn)
    conn.view(SayHelloView.new, name: 'Joe') # `current_path`
    # will be available in the view scope
  end
\#...
```
@waiting-for-dev waiting-for-dev merged commit 3f739d3 into master Jul 3, 2019
@waiting-for-dev waiting-for-dev deleted the dry_view_context branch July 3, 2019 15:24
@waiting-for-dev waiting-for-dev added the enhancement New feature or request label Jul 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant