Sha256: fb6e30cd32144d6aae7900967ed917156b6f2370c7f667fd444b17bfd8373172

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

# 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.
#
class ComfortableMediaSurfer::Content::Tags::Partial < ComfortableMediaSurfer::Content::Tag
  attr_reader :path, :locals

  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

  # we output erb into rest of the content
  def allow_erb?
    true
  end

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

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

ComfortableMediaSurfer::Content::Renderer.register_tag(
  :partial, ComfortableMediaSurfer::Content::Tags::Partial
)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comfortable_media_surfer-3.0.0 lib/comfortable_media_surfer/content/tags/partial.rb