Class: Dry::View Abstract
- Inherits:
-
Object
- Object
- Dry::View
- Extended by:
- Configurable, Core::Cache
- Defined in:
- lib/dry/view.rb,
lib/dry/view/part.rb,
lib/dry/view/path.rb,
lib/dry/view/tilt.rb,
lib/dry/view/scope.rb,
lib/dry/view/errors.rb,
lib/dry/view/context.rb,
lib/dry/view/version.rb,
lib/dry/view/exposure.rb,
lib/dry/view/rendered.rb,
lib/dry/view/renderer.rb,
lib/dry/view/tilt/erb.rb,
lib/dry/view/exposures.rb,
lib/dry/view/tilt/haml.rb,
lib/dry/view/tilt/erbse.rb,
lib/dry/view/part_builder.rb,
lib/dry/view/scope_builder.rb,
lib/dry/view/render_environment.rb,
lib/dry/view/decorated_attributes.rb,
lib/dry/view/render_environment_missing.rb
Overview
Subclass this and provide your own configuration and exposures to define your own view (along with a custom ‘#initialize` if you wish to inject dependencies into your subclass)
A standalone, template-based view rendering system that offers everything you need to write well-factored view code.
This represents a single view, holding the configuration and exposures necessary for rendering its template.
Defined Under Namespace
Modules: DecoratedAttributes, Tilt Classes: Context, Exposure, Exposures, Part, PartBuilder, Path, RenderEnvironment, RenderEnvironmentMissing, Rendered, Renderer, Scope, ScopeBuilder, TemplateNotFoundError, UndefinedConfigError
Constant Summary collapse
- DEFAULT_RENDERER_OPTIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{default_encoding: "utf-8"}.freeze
- VERSION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"0.8.0"
Instance Attribute Summary collapse
-
#exposures ⇒ Exposures
readonly
private
The view’s bound exposures.
Configuration collapse
-
.config.default_context=(context) ⇒ Object
Set the default context object to use when rendering.
-
.config.default_format=(format) ⇒ Object
Set the default format to use when rendering.
-
.config.inflector=(inflector) ⇒ Object
Set an inflector to provide to the part_builder and scope_builder.
-
.config.layout=(name) ⇒ Object
Set the name of the layout to render templates within.
-
.config.layouts_dir=(dir) ⇒ Object
Set the name of the directory (within the configured ‘paths`) holding the layouts.
-
.config.part_builder=(part_builder) ⇒ Object
Set a custom part builder class.
-
.config.scope_namespace=(namespace) ⇒ Object
Set a namespace that will be searched when building scope classes.
-
.config.paths=(paths) ⇒ Object
Set an array of directories that will be searched for all templates (templates, partials, and layouts).
-
.config.renderer_engine_mapping=(mapping) ⇒ Object
A hash specifying the (Tilt-compatible) template engine class to use for a given format.
-
.config.renderer_options=(options) ⇒ Object
A hash of options to pass to the template engine.
-
.config.scope=(scope_class) ⇒ Object
Set the scope class to use when rendering the view’s template.
-
.config.scope_builder=(scope_builder) ⇒ Object
Set a custom scope builder class.
-
.config.scope_namespace=(namespace) ⇒ Object
Set a namespace that will be searched when building scope classes.
-
.config.template=(name) ⇒ Object
Set the name of the template for rendering this view.
Exposures collapse
- .expose(*names, **options, &block) ⇒ Object
-
.exposures ⇒ Exposures
private
Returns the defined exposures.
- .private_expose(*names, **options, &block) ⇒ Object
Render environment collapse
-
.layout_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
Returns a render environment for the view and the given options, chdir’ed into the view’s layout directory.
- .layout_path ⇒ Object private
-
.render_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
Returns a render environment for the view and the given options.
-
.renderer(format) ⇒ Object
private
Returns renderer for the view and provided format.
-
.template_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
Returns a render environment for the view and the given options, chdir’ed into the view’s template directory.
Class Method Summary collapse
- .inherited(klass) ⇒ Object private
Instance Method Summary collapse
-
#call(format: config.default_format, context: config.default_context, **input) ⇒ Rendered
Render the view.
-
#config ⇒ Object
private
The view’s configuration.
-
#initialize ⇒ View
constructor
Returns an instance of the view.
Constructor Details
#initialize ⇒ View
Returns an instance of the view. This binds the defined exposures to the view instance.
Subclasses can define their own ‘#initialize` to accept injected dependencies, but must call `super()` to ensure the standard view initialization can proceed.
444 445 446 |
# File 'lib/dry/view.rb', line 444 def initialize @exposures = self.class.exposures.bind(self) end |
Instance Attribute Details
#exposures ⇒ Exposures (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The view’s bound exposures
434 435 436 |
# File 'lib/dry/view.rb', line 434 def exposures @exposures end |
Class Method Details
.config.default_context=(context) ⇒ Object
121 |
# File 'lib/dry/view.rb', line 121 setting :default_context, default: Context.new.freeze |
.config.default_format=(format) ⇒ Object
131 |
# File 'lib/dry/view.rb', line 131 setting :default_format, default: :html |
.expose(name, **options, &block) ⇒ Object .expose(name, **options) ⇒ Object .expose(name, **options) ⇒ Object .expose(*names, **options) ⇒ Object
338 339 340 341 342 343 344 345 346 |
# File 'lib/dry/view.rb', line 338 def self.expose(*names, **, &block) if names.length == 1 exposures.add(names.first, block, **) else names.each do |name| exposures.add(name, **) end end end |
.exposures ⇒ Exposures
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the defined exposures. These are unbound, since bound exposures are only created when initializing a View instance.
358 359 360 |
# File 'lib/dry/view.rb', line 358 def self.exposures @exposures ||= Exposures.new end |
.config.inflector=(inflector) ⇒ Object
183 |
# File 'lib/dry/view.rb', line 183 setting :inflector, default: Dry::Inflector.new |
.inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
221 222 223 224 225 226 |
# File 'lib/dry/view.rb', line 221 def self.inherited(klass) super exposures.each do |name, exposure| klass.exposures.import(name, exposure) end end |
.config.layout=(name) ⇒ Object
86 |
# File 'lib/dry/view.rb', line 86 setting :layout, default: false |
.layout_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
405 406 407 |
# File 'lib/dry/view.rb', line 405 def self.layout_env(**args) render_env(**args).chdir(layout_path) end |
.layout_path ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
424 425 426 |
# File 'lib/dry/view.rb', line 424 def self.layout_path File.join(config.layouts_dir, config.layout) end |
.config.layouts_dir=(dir) ⇒ Object
95 |
# File 'lib/dry/view.rb', line 95 setting :layouts_dir, default: "layouts" |
.config.part_builder=(part_builder) ⇒ Object
152 |
# File 'lib/dry/view.rb', line 152 setting :part_builder, default: PartBuilder |
.config.scope_namespace=(namespace) ⇒ Object
142 |
# File 'lib/dry/view.rb', line 142 setting :part_namespace |
.config.paths=(paths) ⇒ Object
61 62 63 |
# File 'lib/dry/view.rb', line 61 setting :paths, constructor: -> paths do Array(paths).map { |path| Path[path] } end |
.private_expose(*names, **options, &block) ⇒ Object
349 350 351 |
# File 'lib/dry/view.rb', line 349 def self.private_expose(*names, **, &block) expose(*names, **, private: true, &block) end |
.render_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
Returns a render environment for the view and the given options. This environment isn’t chdir’ed into any particular directory.
377 378 379 |
# File 'lib/dry/view.rb', line 377 def self.render_env(format: config.default_format, context: config.default_context) RenderEnvironment.prepare(renderer(format), config, context) end |
.renderer(format) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns renderer for the view and provided format
412 413 414 415 416 417 418 419 420 421 |
# File 'lib/dry/view.rb', line 412 def self.renderer(format) fetch_or_store(:renderer, config, format) { Renderer.new( config.paths, format: format, engine_mapping: config.renderer_engine_mapping, **config. ) } end |
.config.renderer_engine_mapping=(mapping) ⇒ Object
216 |
# File 'lib/dry/view.rb', line 216 setting :renderer_engine_mapping |
.config.renderer_options=(options) ⇒ Object
198 199 200 |
# File 'lib/dry/view.rb', line 198 setting :renderer_options, default: DEFAULT_RENDERER_OPTIONS, constructor: -> do DEFAULT_RENDERER_OPTIONS.merge(.to_h).freeze end |
.config.scope=(scope_class) ⇒ Object
108 |
# File 'lib/dry/view.rb', line 108 setting :scope |
.config.scope_builder=(scope_builder) ⇒ Object
173 |
# File 'lib/dry/view.rb', line 173 setting :scope_builder, default: ScopeBuilder |
.config.scope_namespace=(namespace) ⇒ Object
163 |
# File 'lib/dry/view.rb', line 163 setting :scope_namespace |
.config.template=(name) ⇒ Object
74 |
# File 'lib/dry/view.rb', line 74 setting :template |
.template_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
392 393 394 |
# File 'lib/dry/view.rb', line 392 def self.template_env(**args) render_env(**args).chdir(config.template) end |
Instance Method Details
#call(format: config.default_format, context: config.default_context, **input) ⇒ Rendered
Render the view
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/dry/view.rb', line 463 def call(format: config.default_format, context: config.default_context, **input) ensure_config env = self.class.render_env(format: format, context: context) template_env = self.class.template_env(format: format, context: context) locals = locals(template_env, input) output = env.template(config.template, template_env.scope(config.scope, locals)) if layout? layout_env = self.class.layout_env(format: format, context: context) output = env.template( self.class.layout_path, layout_env.scope(config.scope, layout_locals(locals)) ) { output } end Rendered.new(output: output, locals: locals) end |
#config ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The view’s configuration
451 452 453 |
# File 'lib/dry/view.rb', line 451 def config self.class.config end |