Class: Occams::Content::Tag::Snippet

Inherits:
Occams::Content::Tag show all
Defined in:
lib/occams/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 Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Occams::Content::Tag

#allow_erb?, #nodes, #render

Constructor Details

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

Returns a new instance of Snippet.

Raises:



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

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

  return if @identifier.present?

  raise Error, 'Missing identifier for snippet tag'
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



8
9
10
# File 'lib/occams/content/tags/snippet.rb', line 8

def identifier
  @identifier
end

Instance Method Details

#contentObject



19
20
21
22
23
24
25
# File 'lib/occams/content/tags/snippet.rb', line 19

def content
  if snippet.markdown
    Kramdown::Document.new(snippet.content.to_s).to_html
  else
    snippet.content
  end
end

#snippetObject

Grabbing or initializing Occams::Cms::Snippet object



28
29
30
31
# File 'lib/occams/content/tags/snippet.rb', line 28

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