Module: Tilt
- Defined in:
- lib/tilt.rb,
lib/tilt/erb.rb,
lib/tilt/css.rb,
lib/tilt/haml.rb,
lib/tilt/yajl.rb,
lib/tilt/rdoc.rb,
lib/tilt/wiki.rb,
lib/tilt/radius.rb,
lib/tilt/coffee.rb,
lib/tilt/string.rb,
lib/tilt/liquid.rb,
lib/tilt/textile.rb,
lib/tilt/builder.rb,
lib/tilt/markaby.rb,
lib/tilt/template.rb,
lib/tilt/markdown.rb,
lib/tilt/nokogiri.rb
Defined Under Namespace
Modules: CompileSite Classes: BlueClothTemplate, BuilderTemplate, Cache, CoffeeScriptTemplate, CreoleTemplate, ERBTemplate, ErubisTemplate, HamlTemplate, KramdownTemplate, LessTemplate, LiquidTemplate, MarkabyTemplate, MarukuTemplate, NokogiriTemplate, RDiscountTemplate, RDocTemplate, RadiusTemplate, RedClothTemplate, RedcarpetTemplate, SassTemplate, ScssTemplate, StringTemplate, Template, WikiClothTemplate, YajlTemplate
Constant Summary
- VERSION =
'1.3.3'- TOPOBJECT =
defined?(BasicObject) ? BasicObject : Object
Class Method Summary (collapse)
-
+ (Object) [](file)
Lookup a template class for the given filename or file extension.
-
+ (Object) mappings
Hash of template path pattern => template implementation class mappings.
-
+ (Object) new(file, line = nil, options = {}, &block)
Create a new template for the given file using the file's extension to determine the the template mapping.
- + (Object) normalize(ext)
-
+ (Object) prefer(template_class, *extensions)
Makes a template class preferred for the given file extensions.
-
+ (Object) register(template_class, *extensions)
Register a template implementation by file extension.
-
+ (Boolean) registered?(ext)
Returns true when a template exists on an exact match of the provided file extension.
Class Method Details
+ (Object) [](file)
Lookup a template class for the given filename or file extension. Return nil when no implementation is found.
69 70 71 72 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 101 102 103 104 105 106 107 108 |
# File 'lib/tilt.rb', line 69 def self.[](file) pattern = file.to_s.downcase until pattern.empty? || registered?(pattern) pattern = File.basename(pattern) pattern.sub!(/^[^.]*\.?/, '') end # Try to find a preferred engine. preferred_klass = @preferred_mappings[pattern] return preferred_klass if preferred_klass # Fall back to the general list of mappings. klasses = @template_mappings[pattern] # Try to find an engine which is already loaded. template = klasses.detect do |klass| if klass.respond_to?(:engine_initialized?) klass.engine_initialized? end end return template if template # Try each of the classes until one succeeds. If all of them fails, # we'll raise the error of the first class. first_failure = nil klasses.each do |klass| begin klass.new { '' } rescue Exception => ex first_failure ||= ex next else return klass end end raise first_failure if first_failure end |
+ (Object) mappings
Hash of template path pattern => template implementation class mappings.
8 9 10 |
# File 'lib/tilt.rb', line 8 def self.mappings @template_mappings end |
+ (Object) new(file, line = nil, options = {}, &block)
Create a new template for the given file using the file's extension to determine the the template mapping.
59 60 61 62 63 64 65 |
# File 'lib/tilt.rb', line 59 def self.new(file, line=nil, ={}, &block) if template_class = self[file] template_class.new(file, line, , &block) else fail "No template engine registered for #{File.basename(file)}" end end |
+ (Object) normalize(ext)
12 13 14 |
# File 'lib/tilt.rb', line 12 def self.normalize(ext) ext.to_s.downcase.sub(/^\./, '') end |
+ (Object) prefer(template_class, *extensions)
Makes a template class preferred for the given file extensions. If you don't provide any extensions, it will be preferred for all its already registered extensions:
# Prefer RDiscount for its registered file extensions:
Tilt.prefer(Tilt::RDiscountTemplate)
# Prefer RDiscount only for the .md extensions:
Tilt.prefer(Tilt::RDiscountTemplate, '.md')
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/tilt.rb', line 38 def self.prefer(template_class, *extensions) if extensions.empty? mappings.each do |ext, klasses| @preferred_mappings[ext] = template_class if klasses.include? template_class end else extensions.each do |ext| ext = normalize(ext) register(template_class, ext) @preferred_mappings[ext] = template_class end end end |
+ (Object) register(template_class, *extensions)
Register a template implementation by file extension.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tilt.rb', line 17 def self.register(template_class, *extensions) if template_class.respond_to?(:to_str) # Support register(ext, template_class) too extensions, template_class = [template_class], extensions[0] end extensions.each do |ext| ext = normalize(ext) mappings[ext].unshift(template_class).uniq! end end |
+ (Boolean) registered?(ext)
Returns true when a template exists on an exact match of the provided file extension
53 54 55 |
# File 'lib/tilt.rb', line 53 def self.registered?(ext) mappings.key?(ext.downcase) && !mappings[ext.downcase].empty? end |