Sha256: 611b1ae7d24d46bf4c60050403df64cec17ab363f4be6a950e4ef79d62aea749

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 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 ComfortableMexicanSofa::Content::Tag::Partial < ComfortableMexicanSofa::Content::Tag

  attr_reader :path, :locals

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

    unless @path.present?
      raise Error, "Missing path for partial tag"
    end
  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 = ComfortableMexicanSofa.config.allowed_partials
    if whitelist.is_a?(Array)
      whitelist.member?(@path) ? content : ""
    else
      content
    end
  end

end

ComfortableMexicanSofa::Content::Renderer.register_tag(
  :partial, ComfortableMexicanSofa::Content::Tag::Partial
)

Version data entries

14 entries across 14 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/content/tags/partial.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/content/tags/partial.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/content/tags/partial.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/partial.rb
kcy1-1.0 lib/comfortable_mexican_sofa/content/tags/partial.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/partial.rb
comfortable_mexican_sofa-2.0.17 lib/comfortable_mexican_sofa/content/tags/partial.rb
comfortable_mexican_sofa-2.0.16 lib/comfortable_mexican_sofa/content/tags/partial.rb