Class: Luoma::ImportTag

Inherits:
Markup
  • Object
show all
Defined in:
lib/luoma/tags/import.rb,
sig/luoma/tags/import.rbs

Instance Attribute Summary

Attributes inherited from Markup

#blank, #tag_name, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Markup

#block_scope, #children, #expressions, #template_scope

Constructor Details

#initialize(token, tag_name, template_name, namespace) ⇒ ImportTag

(t_token, String, Name?) -> void

Parameters:

  • token (t_token)
  • tag_name (String)
  • template_name (Name)
  • namespace (Name, nil)


32
33
34
35
36
37
# File 'lib/luoma/tags/import.rb', line 32

def initialize(token, tag_name, template_name, namespace)
  super(token)
  @tag_name = tag_name
  @template_name = template_name
  @namespace = namespace
end

Class Method Details

.parse(token, tag_name, parser) ⇒ Markup

(t_token, String, Parser) -> Markup

Parameters:

  • token (t_token)
  • tag_name (String)
  • parser (Parser)

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/luoma/tags/import.rb', line 6

def self.parse(token, tag_name, parser)
  name_expr = parser.parse_string_literal
  name_value = name_expr.value

  unless name_value
    raise TemplateSyntaxError.new(
      "expected a string literal",
      name_expr.span,
      parser.source,
      parser.template_name
    )
  end

  name = Name.new(name_expr.token, name_value)

  namespace = if parser.current_value == "as"
                parser.next
                parser.parse_ident
              end

  parser.carry_whitespace_control
  parser.eat(:token_tag_end)
  new(token, tag_name, name, namespace)
end

Instance Method Details

#partial(static_context) ⇒ Object

Signature:

  • (RenderContext) -> Partial?



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/luoma/tags/import.rb', line 74

def partial(static_context)
  name = static_context.env.to_string(@template_name.evaluate(static_context), static_context)

  template = static_context.env.get_template(
    name,
    context: static_context,
    tag: "import"
  )

  Partial.new(
    template,
    :shared,
    {}, #: untyped
    Luoma.fnv1a32("im[ort-#{name}")
  )
end

#render(context, buffer) ⇒ Object

Signature:

  • (RenderContext, String) -> void



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/luoma/tags/import.rb', line 40

def render(context, buffer)
  begin
    template = context.env.get_template(
      @template_name.value,
      context: context,
      tag: "import"
    )
  rescue TemplateNotFoundError => e
    raise NoSuchTemplateError.new(
      e.message,
      @template_name.span,
      context.template.source,
      context.template.name
    )
  end

  ctx = context.copy(
    {},
    block_scope: false,
    template: template
  )

  template.render_with_context(ctx, +"") # Discard output.
  context.render_score_cumulative += ctx.render_score

  # Update locals only.
  if @namespace
    context.assign(@namespace.value, ctx.locals) # steep:ignore
  else
    context.locals.update(ctx.locals)
  end
end