Class: Tilt::Template
- Defined in:
- lib/vendor/tilt-1.4.1/lib/tilt/template.rb
Overview
Base class for template implementations. Subclasses must implement the #prepare method and one of the #evaluate or #precompiled_template methods.
Direct Known Subclasses
AsciidoctorTemplate, BlueClothTemplate, BuilderTemplate, CSVTemplate, CoffeeScriptTemplate, CreoleTemplate, ERBTemplate, EtanniTemplate, HamlTemplate, KramdownTemplate, LessTemplate, LiquidTemplate, MarkabyTemplate, MarukuTemplate, NokogiriTemplate, PlainTemplate, RDiscountTemplate, RDocTemplate, RadiusTemplate, RedClothTemplate, RedcarpetTemplate, RedcarpetTemplate::Redcarpet2, SassTemplate, StringTemplate, WikiClothTemplate, YajlTemplate
Class Attribute Summary collapse
-
.default_mime_type ⇒ Object
Returns the value of attribute default_mime_type.
-
.engine_initialized ⇒ Object
(also: engine_initialized?)
Returns the value of attribute engine_initialized.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Template source; loaded from a file or given directly.
-
#file ⇒ Object
readonly
The name of the file where the template data was loaded from.
-
#line ⇒ Object
readonly
The line number in #file where template data was loaded from.
-
#options ⇒ Object
readonly
A Hash of template engine specific options.
Instance Method Summary collapse
-
#allows_script? ⇒ Boolean
Whether or not this template engine allows executing Ruby script within the template.
-
#basename(suffix = '') ⇒ Object
The basename of the template file.
-
#default_encoding ⇒ Object
The encoding of the source data.
-
#eval_file ⇒ Object
The filename used in backtraces to describe the template.
-
#initialize(file = nil, line = 1, options = {}, &block) ⇒ Template
constructor
Create a new template with the file, line, and options specified.
-
#name ⇒ Object
The template file’s basename with all extensions chomped off.
- #read_template_file ⇒ Object
-
#render(scope = Object.new, locals = {}, &block) ⇒ Object
Render the template in the given scope with the locals specified.
Constructor Details
#initialize(file = nil, line = 1, options = {}, &block) ⇒ Template
Create a new template with the file, line, and options specified. By default, template data is read from the file. When a block is given, it should read template data and return as a String. When file is nil, a block is required.
All arguments are optional.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 38 def initialize(file=nil, line=1, ={}, &block) @file, @line, = nil, 1, {} [, line, file].compact.each do |arg| case when arg.respond_to?(:to_str) ; @file = arg.to_str when arg.respond_to?(:to_int) ; @line = arg.to_int when arg.respond_to?(:to_hash) ; = arg.to_hash.dup when arg.respond_to?(:path) ; @file = arg.path else raise TypeError end end raise ArgumentError, "file or block required" if (@file || block).nil? # call the initialize_engine method if this is the very first time # an instance of this class has been created. if !self.class.engine_initialized? initialize_engine self.class.engine_initialized = true end # used to hold compiled template methods @compiled_method = {} # used on 1.9 to set the encoding if it is not set elsewhere (like a magic comment) # currently only used if template compiles to ruby @default_encoding = .delete :default_encoding # load template data and prepare (uses binread to avoid encoding issues) @reader = block || lambda { |t| read_template_file } @data = @reader.call(self) if @data.respond_to?(:force_encoding) @data.force_encoding(default_encoding) if default_encoding if !@data.valid_encoding? raise Encoding::InvalidByteSequenceError, "#{eval_file} is not valid #{@data.encoding}" end end prepare end |
Class Attribute Details
.default_mime_type ⇒ Object
Returns the value of attribute default_mime_type.
29 30 31 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 29 def default_mime_type @default_mime_type end |
.engine_initialized ⇒ Object Also known as: engine_initialized?
Returns the value of attribute engine_initialized.
26 27 28 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 26 def engine_initialized @engine_initialized end |
Instance Attribute Details
#data ⇒ Object (readonly)
Template source; loaded from a file or given directly.
9 10 11 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 9 def data @data end |
#file ⇒ Object (readonly)
The name of the file where the template data was loaded from.
12 13 14 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 12 def file @file end |
#line ⇒ Object (readonly)
The line number in #file where template data was loaded from.
15 16 17 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 15 def line @line end |
#options ⇒ Object (readonly)
A Hash of template engine specific options. This is passed directly to the underlying engine and is not used by the generic template interface.
20 21 22 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 20 def end |
Instance Method Details
#allows_script? ⇒ Boolean
Whether or not this template engine allows executing Ruby script within the template. If this is false, scope
and locals
will generally not be used, nor will the provided block be avaiable via yield
. This should be overridden by template subclasses.
126 127 128 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 126 def allows_script? true end |
#basename(suffix = '') ⇒ Object
The basename of the template file.
107 108 109 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 107 def basename(suffix='') File.basename(file, suffix) if file end |
#default_encoding ⇒ Object
The encoding of the source data. Defaults to the default_encoding-option if present. You may override this method in your template class if you have a better hint of the data’s encoding.
86 87 88 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 86 def default_encoding @default_encoding end |
#eval_file ⇒ Object
The filename used in backtraces to describe the template.
117 118 119 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 117 def eval_file file || '(__TEMPLATE__)' end |
#name ⇒ Object
The template file’s basename with all extensions chomped off.
112 113 114 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 112 def name basename.split('.', 2).first if basename end |
#read_template_file ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 90 def read_template_file data = File.open(file, 'rb') { |io| io.read } if data.respond_to?(:force_encoding) # Set it to the default external (without verifying) data.force_encoding(Encoding.default_external) if Encoding.default_external end data end |
#render(scope = Object.new, locals = {}, &block) ⇒ Object
Render the template in the given scope with the locals specified. If a block is given, it is typically available within the template via yield
.
102 103 104 |
# File 'lib/vendor/tilt-1.4.1/lib/tilt/template.rb', line 102 def render(scope=Object.new, locals={}, &block) evaluate scope, locals || {}, &block end |