Class: Docco::Theme
- Inherits:
-
Object
- Object
- Docco::Theme
- Defined in:
- lib/docco/theme.rb
Overview
Theme is a templating system that uses ERB templates with named slots. It allows you to define a base template and then create specialized versions by filling in slots with different content.
Direct Known Subclasses
Defined Under Namespace
Classes: Context, Slots, Static
Class Method Summary collapse
- .call(node) ⇒ Object
-
.define(str) ⇒ Theme
Creates a new Theme from an ERB template string.
Instance Method Summary collapse
-
#call(node) ⇒ String
Renders the theme with the given node by evaluating all slot templates and then the main template.
-
#define(str = nil) {|Slots| ... } ⇒ Theme
Creates a new theme by defining slots for the template.
-
#initialize(tpl, slots: {}) ⇒ Theme
constructor
Initializes a new Theme instance.
Constructor Details
#initialize(tpl, slots: {}) ⇒ Theme
Initializes a new Theme instance.
70 71 72 73 |
# File 'lib/docco/theme.rb', line 70 def initialize(tpl, slots: {}) @tpl = tpl @slots = slots end |
Class Method Details
.call(node) ⇒ Object
56 57 58 |
# File 'lib/docco/theme.rb', line 56 def self.call(node) raise NotImplementedError, "define #{self}.call(node) to delegate .call(node) to the right root template (usually the homepage)" end |
.define(str) ⇒ Theme
Creates a new Theme from an ERB template string.
Template can also be a #read() => String interface for example a File or Pathname to read templates from disk
50 51 52 53 54 |
# File 'lib/docco/theme.rb', line 50 def self.define(str) str = str.read if str.respond_to?(:read) tmpl = ERB.new(str) new(tmpl) end |
Instance Method Details
#call(node) ⇒ String
Renders the theme with the given node by evaluating all slot templates and then the main template.
125 126 127 128 129 130 131 132 |
# File 'lib/docco/theme.rb', line 125 def call(node) ctx = Context.new(node:, slots: {}) slots = @slots.transform_values do |tpl| tpl.result(ctx.get_binding) end ctx = Context.new(node:, slots:) @tpl.result(ctx.get_binding) end |
#define(str = nil) {|Slots| ... } ⇒ Theme
Creates a new theme by defining slots for the template. Can accept either a string (which becomes the :main slot) or a block that yields a Slots object for defining multiple slots.
101 102 103 104 105 106 107 108 109 |
# File 'lib/docco/theme.rb', line 101 def define(str = nil, &) slots = Slots.new if str slots.slot(:main, str) elsif block_given? yield slots end self.class.new(@tpl, slots: slots.to_h) end |