Class: Temple::Filters::Escapable

Inherits:
Temple::Filter show all
Defined in:
lib/temple/filters/escapable.rb

Overview

Escape dynamic or static expressions. This filter must be used after Temple::HTML::* and before the generators. It can be enclosed with Temple::Filters::DynamicInliner filters to reduce calls to Temple::Utils#escape_html.

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods included from Mixins::Options

included

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::CoreDispatcher

#on_capture, #on_multi

Methods included from Mixins::CompiledDispatcher

#call, #compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Constructor Details

#initialize(opts = {}) ⇒ Escapable

Returns a new instance of Escapable.



16
17
18
19
20
21
22
# File 'lib/temple/filters/escapable.rb', line 16

def initialize(opts = {})
  super
  @escape_code = options[:escape_code] ||
    "::Temple::Utils.escape_html#{options[:use_html_safe] ? '_safe' : ''}((%s))"
  @escaper = eval("proc {|v| #{@escape_code % 'v'} }")
  @escape = false
end

Instance Method Details

#on_dynamic(value) ⇒ Object



36
37
38
# File 'lib/temple/filters/escapable.rb', line 36

def on_dynamic(value)
  [:dynamic, @escape ? @escape_code % value : value]
end

#on_escape(flag, exp) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/temple/filters/escapable.rb', line 24

def on_escape(flag, exp)
  old = @escape
  @escape = flag && !options[:disable_escape]
  compile(exp)
ensure
  @escape = old
end

#on_static(value) ⇒ Object



32
33
34
# File 'lib/temple/filters/escapable.rb', line 32

def on_static(value)
  [:static, @escape ? @escaper[value] : value]
end