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/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
Constant Summary collapse
- UndefinedTemplateError =
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.
Class.new(StandardError)
- 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.6.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.
442 443 444 |
# File 'lib/dry/view.rb', line 442 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
432 433 434 |
# File 'lib/dry/view.rb', line 432 def exposures @exposures end |
Class Method Details
.config.default_context=(context) ⇒ Object
123 |
# File 'lib/dry/view.rb', line 123 setting :default_context, Context.new.freeze |
.config.default_format=(format) ⇒ Object
133 |
# File 'lib/dry/view.rb', line 133 setting :default_format, :html |
.expose(name, **options, &block) ⇒ Object .expose(name, **options) ⇒ Object .expose(name, **options) ⇒ Object .expose(*names, **options) ⇒ Object
336 337 338 339 340 341 342 343 344 |
# File 'lib/dry/view.rb', line 336 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.
356 357 358 |
# File 'lib/dry/view.rb', line 356 def self.exposures @exposures ||= Exposures.new end |
.config.inflector=(inflector) ⇒ Object
185 |
# File 'lib/dry/view.rb', line 185 setting :inflector, 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.
223 224 225 226 227 228 |
# File 'lib/dry/view.rb', line 223 def self.inherited(klass) super exposures.each do |name, exposure| klass.exposures.import(name, exposure) end end |
.config.layout=(name) ⇒ Object
88 |
# File 'lib/dry/view.rb', line 88 setting :layout, false |
.layout_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
403 404 405 |
# File 'lib/dry/view.rb', line 403 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.
422 423 424 |
# File 'lib/dry/view.rb', line 422 def self.layout_path File.join(config.layouts_dir, config.layout) end |
.config.layouts_dir=(dir) ⇒ Object
97 |
# File 'lib/dry/view.rb', line 97 setting :layouts_dir, "layouts" |
.config.part_builder=(part_builder) ⇒ Object
154 |
# File 'lib/dry/view.rb', line 154 setting :part_builder, PartBuilder |
.config.scope_namespace=(namespace) ⇒ Object
144 |
# File 'lib/dry/view.rb', line 144 setting :part_namespace |
.config.paths=(paths) ⇒ Object
63 64 65 |
# File 'lib/dry/view.rb', line 63 setting :paths do |paths| Array(paths).map { |path| Path[path] } end |
.private_expose(*names, **options, &block) ⇒ Object
347 348 349 |
# File 'lib/dry/view.rb', line 347 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.
375 376 377 |
# File 'lib/dry/view.rb', line 375 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
410 411 412 413 414 415 416 417 418 419 |
# File 'lib/dry/view.rb', line 410 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
218 |
# File 'lib/dry/view.rb', line 218 setting :renderer_engine_mapping |
.config.renderer_options=(options) ⇒ Object
200 201 202 |
# File 'lib/dry/view.rb', line 200 setting :renderer_options, DEFAULT_RENDERER_OPTIONS do || DEFAULT_RENDERER_OPTIONS.merge(.to_h).freeze end |
.config.scope=(scope_class) ⇒ Object
110 |
# File 'lib/dry/view.rb', line 110 setting :scope |
.config.scope_builder=(scope_builder) ⇒ Object
175 |
# File 'lib/dry/view.rb', line 175 setting :scope_builder, ScopeBuilder |
.config.scope_namespace=(namespace) ⇒ Object
165 |
# File 'lib/dry/view.rb', line 165 setting :scope_namespace |
.config.template=(name) ⇒ Object
76 |
# File 'lib/dry/view.rb', line 76 setting :template |
.template_env(format: config.default_format, context: config.default_context) ⇒ RenderEnvironment
390 391 392 |
# File 'lib/dry/view.rb', line 390 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
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/dry/view.rb', line 461 def call(format: config.default_format, context: config.default_context, **input) raise UndefinedTemplateError, "no +template+ configured" unless config.template 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 = layout_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
449 450 451 |
# File 'lib/dry/view.rb', line 449 def config self.class.config end |