Class: RSX::Template
- Inherits:
-
Object
- Object
- RSX::Template
- Defined in:
- lib/rsx/template.rb
Overview
A .rsx file rendered as markup rather than as a named component.
The file body becomes a method on an anonymous Component subclass, so a template gets the whole component toolkit (children, caching, context, helpers) and is compiled once no matter how often it is rendered.
Instance Attribute Summary collapse
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #component ⇒ Object
-
#initialize(source, path: nil) ⇒ Template
constructor
A new instance of Template.
- #render(props = nil, context: nil) ⇒ Object
Constructor Details
#initialize(source, path: nil) ⇒ Template
Returns a new instance of Template.
23 24 25 26 27 28 29 |
# File 'lib/rsx/template.rb', line 23 def initialize(source, path: nil) digest = RSX.config.compile_cache.digest(source.to_s) ruby = RSX.config.compile_cache.fetch(path || "template", source.to_s) do Transformer.transform(source.to_s, path: path) end build(ruby, path, digest) end |
Instance Attribute Details
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
10 11 12 |
# File 'lib/rsx/template.rb', line 10 def digest @digest end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/rsx/template.rb', line 10 def path @path end |
Class Method Details
.from_ruby(ruby, path: nil, digest: nil) ⇒ Object
19 20 21 |
# File 'lib/rsx/template.rb', line 19 def self.from_ruby(ruby, path: nil, digest: nil) allocate.tap { |template| template.send(:build, ruby, path, digest) } end |
.load(path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/rsx/template.rb', line 12 def self.load(path) absolute = File.(path) raise FileNotFoundError, "no such RSX file: #{absolute}" unless File.file?(absolute) new(File.read(absolute), path: absolute) end |
Instance Method Details
#component ⇒ Object
43 44 45 |
# File 'lib/rsx/template.rb', line 43 def component @component end |
#render(props = nil, context: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rsx/template.rb', line 31 def render(props = nil, context: nil) value = @component.new(props, context).rsx_render(props) # A file whose last statement is a component definition (or an # `export default`) renders that component with the props it was given. if value.is_a?(Class) && value < Component RSX.safe(value.rsx_call(props, context)) else RSX.safe(RSX.child(value)) end end |