Class: Hanami::View::Renderer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/view/renderer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Resolves a template by name and renders it through Tilt.

Template lookup combines two pieces of state:

  • **‘config.paths`** — the configured view paths, immutable for the lifetime of the renderer. When multiple view paths are configured, earlier ones override later ones.

  • **‘@prefixes`** — a stack of subdirectories within each view path to search, mutated during rendering. It starts at `[CURRENT_PATH_PREFIX]` (the root itself). When a template is rendered, its parent directory (e.g. `“users”` for `“users/index”`) is pushed onto the stack so that a partial referenced by its bare name (e.g. `render(“form”)` from inside `users/index.html.erb`) can be found alongside the template that renders it. The stack is

    snapshot-and-restored around each render via `ensure`.
    

‘#lookup` tries every combination of a path and a prefix, joining each pair with the requested name to find a matching file. `paths` are checked in configured order; an earlier entry overrides a later one. `prefixes` are checked oldest-first: a partial at the root wins over a same-named partial in a directory pushed onto the stack mid-render. First match wins.

Since:

  • 2.1.0

Constant Summary collapse

PARTIAL_PREFIX =

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.

Since:

  • 2.1.0

"_"
PATH_DELIMITER =

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.

Since:

  • 2.1.0

"/"
CURRENT_PATH_PREFIX =

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.

Since:

  • 2.1.0

"."
EXTENSIONS_REGEXP =

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.

Matches the ‘.format.engine` extensions on a template path (e.g. `.html.erb`).

Since:

  • 2.1.0

/\.[^.\/]+\.[^.\/]+\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_data) ⇒ Renderer

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 a new instance of Renderer.

Since:

  • 2.1.0



43
44
45
46
47
# File 'lib/hanami/view/renderer.rb', line 43

def initialize(config_data)
  @config_data = config_data
  @prefixes = [CURRENT_PATH_PREFIX]
  @current_template_names = []
end

Instance Attribute Details

#current_template_namesArray<String> (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.

Stack of resolved names for the templates and partials currently being rendered. The top of the stack is the innermost render in progress.

Returns:

Since:

  • 2.1.0



40
41
42
# File 'lib/hanami/view/renderer.rb', line 40

def current_template_names
  @current_template_names
end

Instance Method Details

#current_template_nameString?

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 resolved name of the template or partial currently being rendered, or nil if no render is in progress.

The name is the file’s path relative to the matching view path, with format/engine extensions stripped.

Returns:

Since:

  • 2.1.0



78
79
80
# File 'lib/hanami/view/renderer.rb', line 78

def current_template_name
  @current_template_names.last
end

#partial(name, format, scope, &block) ⇒ 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.

Since:

  • 2.1.0



67
68
69
# File 'lib/hanami/view/renderer.rb', line 67

def partial(name, format, scope, &block)
  template(name_for_partial(name), format, scope, &block)
end

#template(name, format, scope, &block) ⇒ 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.

Since:

  • 2.1.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hanami/view/renderer.rb', line 49

def template(name, format, scope, &block)
  old_prefixes = @prefixes.dup

  result = lookup(name, format)
  raise TemplateNotFoundError.new(name, format, config_data.paths) unless result

  template_path, relative_path = result

  new_prefix = File.dirname(name)
  @prefixes << new_prefix unless @prefixes.include?(new_prefix)
  @current_template_names << resolve_template_name(relative_path)

  render(template_path, scope, &block)
ensure
  @prefixes = old_prefixes
  @current_template_names.pop if result
end