Class: ComfortableMexicanSofa::Content::Tag::Snippet

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

Overview

Tag for reusable snippets within context’s site scope. Looks like this:

{{cms:snippet identifier}}

Snippets may have more tags in them like fragments, so they may be expanded too.

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from ComfortableMexicanSofa::Content::Tag

#allow_erb?, #nodes, #render

Constructor Details

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

Returns a new instance of Snippet.



11
12
13
14
15
16
17
18
# File 'lib/comfortable_mexican_sofa/content/tags/snippet.rb', line 11

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

  unless @identifier.present?
    raise Error, "Missing identifier for snippet tag"
  end
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/comfortable_mexican_sofa/content/tags/snippet.rb', line 9

def identifier
  @identifier
end

Instance Method Details

#contentObject



20
21
22
# File 'lib/comfortable_mexican_sofa/content/tags/snippet.rb', line 20

def content
  snippet.content
end

#snippetObject

Grabbing or initializing Comfy::Cms::Snippet object



25
26
27
28
# File 'lib/comfortable_mexican_sofa/content/tags/snippet.rb', line 25

def snippet
  context.site.snippets.detect { |s| s.identifier == identifier } ||
    context.site.snippets.build(identifier: identifier)
end