Class: Sourcerer::Jekyll::Tags::EmbedTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/sourcerer/jekyll/liquid/tags.rb

Overview

A Liquid tag for embedding and rendering a file within a template. It searches for the file in the configured include paths.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ EmbedTag

Returns a new instance of EmbedTag.

Parameters:

  • tag_name (String)
    The name of the tag ('embed').
  • markup (String)
    The name of the partial to embed.
  • tokens (Array<String>)
    The list of tokens.


13
14
15
16
# File 'lib/sourcerer/jekyll/liquid/tags.rb', line 13

def initialize tag_name, markup, tokens
  super
  @partial_name = markup.strip
end

Instance Method Details

#render(context) ⇒ String

Renders the embedded file.

Parameters:

  • context (Liquid::Context)
    The Liquid context.

Returns:

  • (String)
    The rendered content of the embedded file.

Raises:

  • (IOError)
    if the embed file is not found.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sourcerer/jekyll/liquid/tags.rb', line 23

def render context
  includes_paths = context.registers[:includes_load_paths] || []

  found_path = includes_paths.find do |base|
    candidate = File.expand_path(@partial_name, base)
    File.exist?(candidate)
  end

  raise "Embed file not found: #{@partial_name}" unless found_path

  full_path = File.expand_path(@partial_name, found_path)
  source = File.read(full_path)

  partial = ::Liquid::Template.parse(source)
  partial.render!(context)
end