Class: ComfortableMexicanSofa::Content::Tag::Template

Inherits:
ComfortableMexicanSofa::Content::Tag show all
Defined in:
lib/comfortable_mexican_sofa/content/tags/template.rb

Overview

Tag for injecting template rendering. Example tag:

{{cms:template template/path}}

This expands into something like this:

<%= render template: "template/path" %>

Whitelist is can be used to control what templates are available.

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from ComfortableMexicanSofa::Content::Tag

#nodes

Constructor Details

#initialize(context:, params: [], source: nil) ⇒ Template

Returns a new instance of Template.



13
14
15
16
17
18
19
20
# File 'lib/comfortable_mexican_sofa/content/tags/template.rb', line 13

def initialize(context:, params: [], source: nil)
  super
  @path = params[0]

  unless @path.present?
    raise Error, "Missing template path for template tag"
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/comfortable_mexican_sofa/content/tags/template.rb', line 11

def path
  @path
end

Instance Method Details

#allow_erb?Boolean

we output erb into rest of the content

Returns:

  • (Boolean)


23
24
25
# File 'lib/comfortable_mexican_sofa/content/tags/template.rb', line 23

def allow_erb?
  true
end

#contentObject



27
28
29
# File 'lib/comfortable_mexican_sofa/content/tags/template.rb', line 27

def content
  format("<%%= render template: %<path>p %%>", path: path)
end

#renderObject



31
32
33
34
35
36
37
38
# File 'lib/comfortable_mexican_sofa/content/tags/template.rb', line 31

def render
  whitelist = ComfortableMexicanSofa.config.allowed_templates
  if whitelist.is_a?(Array)
    whitelist.member?(@path) ? content : ""
  else
    content
  end
end