Class: Occams::Content::Tag::Partial

Inherits:
Occams::Content::Tag show all
Defined in:
lib/occams/content/tags/partial.rb

Overview

Tag for injecting partials. Example tag:

{{cms:partial path/to/partial, foo: bar, zip: zoop}}

This expands into a familiar:

<%= render partial: "path/to/partial", locals: {foo: bar, zip: zoop} %>

Whitelist is can be used to control what partials are renderable.

Instance Attribute Summary collapse

Attributes inherited from Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Occams::Content::Tag

#nodes

Constructor Details

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

Returns a new instance of Partial.

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/occams/content/tags/partial.rb', line 12

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

  return if @path.present?

  raise Error, 'Missing path for partial tag'
end

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals.



10
11
12
# File 'lib/occams/content/tags/partial.rb', line 10

def locals
  @locals
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/occams/content/tags/partial.rb', line 10

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/occams/content/tags/partial.rb', line 23

def allow_erb?
  true
end

#contentObject



27
28
29
30
31
32
33
# File 'lib/occams/content/tags/partial.rb', line 27

def content
  format(
    '<%%= render partial: %<path>p, locals: %<locals>s %%>',
    path: @path,
    locals: @locals
  )
end

#renderObject



35
36
37
38
39
40
41
42
# File 'lib/occams/content/tags/partial.rb', line 35

def render
  whitelist = Occams.config.allowed_partials
  if whitelist.is_a?(Array)
    whitelist.member?(@path) ? content : ''
  else
    content
  end
end