Module: JekyllHighlightCards::TemplateRenderer
- Included in:
- LinkcardTag, PolaroidTag
- Defined in:
- lib/jekyll-highlight-cards/template_renderer.rb
Overview
Module for rendering Liquid templates with user override support
Instance Method Summary collapse
-
#find_template_path(site, template_name) ⇒ String?
Find template file path, checking user override first, then gem default.
-
#render_template(site, template_name, variables) ⇒ String
Render a template with given variables.
-
#safe_template_path(allowed_dir, template_path) ⇒ String?
Verify template path is safe (within allowed directory).
Instance Method Details
#find_template_path(site, template_name) ⇒ String?
Find template file path, checking user override first, then gem default
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 47 def find_template_path(site, template_name) validate_template_name!(template_name) template_filename = "#{template_name}.html" relative_path = File.join("highlight-cards", template_filename) # Check for user override first user_path = find_user_template(site, relative_path) return user_path if user_path # Fall back to gem bundled template find_gem_template(relative_path) end |
#render_template(site, template_name, variables) ⇒ String
Render a template with given variables
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 20 def render_template(site, template_name, variables) template_path = find_template_path(site, template_name) raise TemplateNotFoundError, "Template not found: #{template_name}" if template_path.nil? begin template_content = File.read(template_path) template = Liquid::Template.parse(template_content) template.render(variables) rescue Errno::ENOENT, Errno::EACCES => e raise TemplateRenderError, "Failed to read template '#{template_name}' at #{template_path}: #{e.class}: #{e}" rescue Encoding::InvalidByteSequenceError => e raise TemplateRenderError, "Invalid encoding in template '#{template_name}' at #{template_path}: #{e.class}: #{e}" rescue Liquid::SyntaxError, Liquid::Error => e raise TemplateRenderError, "Liquid error in template '#{template_name}': #{e.class}: #{e}" rescue StandardError => e raise TemplateRenderError, "Unexpected error rendering template '#{template_name}': #{e.class}: #{e}" end end |
#safe_template_path(allowed_dir, template_path) ⇒ String?
Verify template path is safe (within allowed directory)
66 67 68 69 70 71 72 |
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 66 def safe_template_path(allowed_dir, template_path) return nil unless File.exist?(template_path) = File.(allowed_dir) = File.(template_path) template_path if .start_with?() end |