Sha256: 128db6f244ea37ca7a2e324206125bc87acd451f5ea71c8769564e6f6b425953

Contents?: true

Size: 941 Bytes

Versions: 1

Compression:

Stored size: 941 Bytes

Contents

# frozen_string_literal: true

# 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.
#
class Occams::Content::Tag::Snippet < Occams::Content::Tag

  attr_reader :identifier

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

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

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

  # Grabbing or initializing Occams::Cms::Snippet object
  def snippet
    context.site.snippets.detect { |s| s.identifier == identifier } ||
      context.site.snippets.build(identifier: identifier)
  end

end

Occams::Content::Renderer.register_tag(
  :snippet, Occams::Content::Tag::Snippet
)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
occams-1.0.0 lib/occams/content/tags/snippet.rb