Class: Jekyll::UJPowertools::IfFileTag

Inherits:
Liquid::Block
  • Object
show all
Includes:
VariableResolver
Defined in:
lib/tags/iffile.rb

Instance Method Summary collapse

Methods included from VariableResolver

#is_quoted?, #parse_arguments, #parse_options, #resolve_input, #resolve_variable

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ IfFileTag

Returns a new instance of IfFileTag.



10
11
12
13
# File 'lib/tags/iffile.rb', line 10

def initialize(tag_name, markup, tokens)
  super
  @path = markup.strip
end

Instance Method Details

#render(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tags/iffile.rb', line 15

def render(context)
  # Get the site object
  site = context.registers[:site]

  path = resolve_path(context)

  # Return empty if path couldn't be resolved
  return "" if path.nil? || path.to_s.empty?

  # Ensure path starts with /
  path = "/#{path}" unless path.to_s.start_with?('/')

  # Check if file exists in static_files
  file_exists = site.static_files.any? { |file|
    # Compare both with and without leading slash
    file.relative_path == path ||
    file.relative_path == path[1..-1] ||
    "/#{file.relative_path}" == path
  }

  if file_exists
    super
  else
    ""
  end
end