Module: ExpressTemplates::Components::Capabilities::Templating::ClassMethods

Defined in:
lib/express_templates/components/capabilities/templating.rb

Instance Method Summary collapse

Instance Method Details

#[](label) ⇒ Object



65
66
67
# File 'lib/express_templates/components/capabilities/templating.rb', line 65

def [](label)
  _lookup(label)
end

#emits(*args, &template_code) ⇒ Object

Store fragments of ExpressTemplate style markup for use in generating the HTML representation of a component.

For example in your class, simply place the following:

class MyComponent < ET::Components::Base
  emits {
    ul {
      li "one"
      li "two"
      li "three"
    }
  }

end

By default this template code is stored under the label :markup

You may specify several fragments with a hash containing lambdas:

emits body:     -> { li "item" },
      wrapper:  -> { ul { _yield } }

This method is aliased as fragments and has_markup



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/express_templates/components/capabilities/templating.rb', line 52

def emits(*args, &template_code)
  if args.first.respond_to?(:call) or template_code
    fragment = (args.first||template_code)
    raise "must use stabby lambda" unless fragment.lambda?
    _store :markup, fragment# default fragment is named :markup
  else
    args.first.to_a.each do |name, block|
      raise ArgumentError unless name.is_a?(Symbol) and block.is_a?(Proc)
      _store(name, block)
    end
  end
end

#helper(name, &block) ⇒ Object

Stores a block given for later evaluation in context.

Example:

class TitleComponent < ECB
  helper :title_helper do
    @resource.name
  end

  emits {
    h1 {
      title_helper
    }
  }

end

In this example @resource.name is evaluated in the provided context during page rendering and not during template expansion or compilation.

This is the recommended for encapsulation of “helper” type functionality which is of concern only to the component and used only in its own markup fragments.



93
94
95
96
# File 'lib/express_templates/components/capabilities/templating.rb', line 93

def helper(name, &block)
  _helpers[name] = block
  _define_helper_methods name
end

#special_handlersObject



98
99
100
# File 'lib/express_templates/components/capabilities/templating.rb', line 98

def special_handlers
  {insert: self, _yield: self}.merge(Hash[*(_helpers.keys.map {|k| [k, self] }.flatten)])
end