Sha256: c6ab7c1ed1b70e833e8386f1537c622102fd1dcfcdc156471721c382eedb12af

Contents?: true

Size: 1.85 KB

Versions: 59

Compression:

Stored size: 1.85 KB

Contents

module SnippetTags
  include TrustyCms::Taggable
  
  class TagError < StandardError; end
  
  desc %{
    Renders the snippet specified in the @name@ attribute within the context of a page.

    *Usage:*

    <pre><code><r:snippet name="snippet_name" /></code></pre>

    When used as a double tag, the part in between both tags may be used within the
    snippet itself, being substituted in place of @<r:yield/>@.

    *Usage:*

    <pre><code><r:snippet name="snippet_name">Lorem ipsum dolor...</r:snippet></code></pre>
  }
  tag 'snippet' do |tag|
    required_attr(tag, 'name')
    name = tag['name']

    snippet = snippet_cache(name.strip)
    
    if snippet
      tag.locals.yield = tag.expand if tag.double?
      tag.globals.page.render_snippet(snippet)
    else
      raise TagError.new("snippet '#{name}' not found")
    end
  end

  def snippet_cache(name)
    @snippet_cache ||= {}

    snippet = @snippet_cache[name]
    unless snippet
      snippet = SnippetFinder.find_by_name(name)
      @snippet_cache[name] = snippet
    end
    snippet
  end
  private :snippet_cache

  desc %{
    Used within a snippet as a placeholder for substitution of child content, when
    the snippet is called as a double tag.

    *Usage (within a snippet):*
    
    <pre><code>
    <div id="outer">
      <p>before</p>
      <r:yield/>
      <p>after</p>
    </div>
    </code></pre>

    If the above snippet was named "yielding", you could call it from any Page,
    Layout or Snippet as follows:

    <pre><code><r:snippet name="yielding">Content within</r:snippet></code></pre>

    Which would output the following:

    <pre><code>
    <div id="outer">
      <p>before</p>
      Content within
      <p>after</p>
    </div>
    </code></pre>

    When called in the context of a Page or a Layout, @<r:yield/>@ outputs nothing.
  }
  tag 'yield' do |tag|
    tag.locals.yield
  end
end

Version data entries

59 entries across 59 versions & 2 rubygems

Version Path
trusty-cms-4.1.2 app/models/snippet_tags.rb
trusty-cms-4.1.1 app/models/snippet_tags.rb
trusty-cms-4.1.0 app/models/snippet_tags.rb
trusty-cms-4.0.2 app/models/snippet_tags.rb
trusty-cms-3.9.7 app/models/snippet_tags.rb
trusty-cms-3.9.6 app/models/snippet_tags.rb
trusty-cms-3.9.5 app/models/snippet_tags.rb
trusty-cms-4.0.1 app/models/snippet_tags.rb
trusty-cms-3.9.4 app/models/snippet_tags.rb
trusty-cms-3.9.3 app/models/snippet_tags.rb
trusty-cms-3.9.2 app/models/snippet_tags.rb
trusty-cms-4.0.0 app/models/snippet_tags.rb
trusty-cms-3.9.1 app/models/snippet_tags.rb
trusty-cms-3.9.0 app/models/snippet_tags.rb
trusty-cms-3.8.4 app/models/snippet_tags.rb
trusty-cms-3.8.3 app/models/snippet_tags.rb
trusty-cms-3.8.2 app/models/snippet_tags.rb
trusty-cms-3.8.1 app/models/snippet_tags.rb
trusty-cms-3.8.0 app/models/snippet_tags.rb
trusty-cms-3.7.1 app/models/snippet_tags.rb