Class: Tilt::StaticTemplate

Inherits:
Template show all
Defined in:
lib/tilt/template.rb

Overview

Static templates are templates that return the same output for every render

Instead of inheriting from the StaticTemplate class, you will use the .subclass method with a block which processes @data and returns the transformed value.

Basic example which transforms the template to uppercase:

UppercaseTemplate = Tilt::StaticTemplate.subclass do
  @data.upcase
end

Direct Known Subclasses

CoffeeScriptTemplate, SassTemplate

Instance Attribute Summary

Attributes inherited from Template

#compiled_path, #data, #file, #line, #options

For template implementations collapse

Methods inherited from Template

#basename, default_mime_type, default_mime_type=, #eval_file, #initialize, metadata, #metadata, #name

Constructor Details

This class inherits a constructor from Tilt::Template

Class Method Details

.subclass(mime_type: 'text/html', &block) ⇒ Object



441
442
443
444
445
446
447
448
449
# File 'lib/tilt/template.rb', line 441

def self.subclass(mime_type: 'text/html', &block)
  Class.new(self) do
    self.default_mime_type = mime_type

    private

    define_method(:_prepare_output, &block)
  end
end

Instance Method Details

#allows_script?Boolean

Static templates never allow script.

Returns:

  • (Boolean)


463
464
465
# File 'lib/tilt/template.rb', line 463

def allows_script?
  false
end

#compiled_method(locals_keys, scope_class = nil) ⇒ Object

Raise NotImplementedError, since static templates do not support compiled methods.

Raises:

  • (NotImplementedError)


458
459
460
# File 'lib/tilt/template.rb', line 458

def compiled_method(locals_keys, scope_class=nil)
  raise NotImplementedError
end

#render(scope = nil, locals = nil) ⇒ Object

Static templates always return the prepared output.



452
453
454
# File 'lib/tilt/template.rb', line 452

def render(scope=nil, locals=nil)
  @output
end