Module: Middleman::Patterns::Helpers
- Defined in:
- lib/middleman-patterns/helpers.rb
Overview
These helpers are available in source template files.
Helpers are defined dynamically to match the pattern types in the PATTERNS constant.
These pattern calls all work identically except for the path the extension checks for the source file (based on pattern type).
Variables are available in patterns and can come from one of three places:
-
Front matter in the pattern file itself
-
locals passed to the pattern
-
locals that were passed into the currently-rendering pattern
Given the following two patterns for a button:
# patterns/molecules/buttons.html.erb
---
div_class: 'button'
---
<div class="<%= div_class %>">
<%= atom 'button', button_text: 'molecule button' %>
</div>
--------------------------
# patterns/atoms/button.html.erb
---
button_text: I'm a button
---
<button type="button"><%= button_text %></button>
Variable precendence flows from highest->lowest pattern in terms of nesting, so a molecule with a local passed which is the same name as application data variable or front-matter variable will be overridden (div_class above).
# index.html.erb
<%= molecule 'buttons', div_class: 'big-button' %>
<%= molecule 'buttons', button_text: "Send 1000 emails" %>
Should render:
<div class="big-button">
<button type="button">molecule button</button>
</div>
<div class="button">
<button type="button">Send 1000 emails</button>
</div>
Constant Summary collapse
- PATTERNS =
%w(template organism molecule atom)
Instance Method Summary collapse
-
#context_variables ⇒ Hash
Getter for locals passed into pattern calls.
-
#resolve_pattern_path(pattern_type, pattern_name) ⇒ String
Get the path to the specified pattern on disk.
Instance Method Details
#context_variables ⇒ Hash
Getter for locals passed into pattern calls. Allows scoping for variables for nested pattern rendering
Example: Rendering a molecule template that renders a “Label” atom:
Calling as ‘molecule ’labeled-input’, label_class: ‘optional’‘ will override the atom’s label_class local, even though in the molecule’s template the label atom will be called without explicitly setting the label_class.
72 73 74 75 |
# File 'lib/middleman-patterns/helpers.rb', line 72 def context_variables @context_variables ||= {} @context_variables end |
#resolve_pattern_path(pattern_type, pattern_name) ⇒ String
Get the path to the specified pattern on disk. This respects the config setting patterns_directory to determine where to look.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/middleman-patterns/helpers.rb', line 105 def resolve_pattern_path(pattern_type, pattern_name) path = File.join(config.patterns_directory, pattern_type.pluralize, pattern_name) absolute_path = resolve_template(path) unless absolute_path = "#{pattern_type.capitalize} #{pattern_name} not found: #{path}" raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, end absolute_path end |