Class: Landable::Liquid::TemplateTag

Inherits:
Tag
  • Object
show all
Defined in:
lib/landable/liquid/tags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_tag, param, _tokens) ⇒ TemplateTag

Returns a new instance of TemplateTag.



67
68
69
70
71
# File 'lib/landable/liquid/tags.rb', line 67

def initialize(_tag, param, _tokens)
  param_tokens = param.split(/\s+/)
  @template_slug = param_tokens.shift
  @variables = Hash[param_tokens.join(' ').scan(/([\w_]+):\s+"([^"]*)"/)]
end

Instance Attribute Details

#template_slugObject

Returns the value of attribute template_slug.



65
66
67
# File 'lib/landable/liquid/tags.rb', line 65

def template_slug
  @template_slug
end

Instance Method Details

#render(context) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/landable/liquid/tags.rb', line 73

def render(context)
  template = Landable::Template.find_by_slug @template_slug

  # Handle Templates that are deleted (don't render anything)
  return if template && template.deleted_at?

  # Handle Templates that are Partials
  if template && template.partial?
    responder = context.registers[:responder]
    # the controller we need for rendering. the request we need to dodge a bug in rails 4.1
    # (fixed in https://github.com/rails/rails/commit/f6d9b689977c1dca1ed7f149f704d1b4344cd691).
    # if we need to render pages offline (i.e. without a request) we'll need to rethink this.
    if responder.try(:controller).try(:request).present?
      responder.controller.render_to_string(partial: template.file)
    else
      "<!-- render error: unable to render \"#{@template_slug}\", no controller/responder present -->"
    end

  # Handle Published Templates
  elsif template && template.revisions.where(is_published: true).present?
    template = template.revisions.where(is_published: true).first
    ::Liquid::Template.parse(template.body).render @variables

  # Handle Template Errors
  else
    "<!-- render error: missing published template \"#{@template_slug}\" -->"
  end
end