Module: Alongslide::Templates
- Defined in:
- lib/alongslide/templates.rb
Constant Summary collapse
- @@TEMPLATE_DIR =
Template paths.
A minimum of templates are bundled with the gem.
The rest are provided by the user, and stored elsewhere.
File.("../../app/views", File.dirname(__FILE__))
- @@user_template_dir =
nil- @@template_names =
Names of user templates, sorted by type.
{}
- @@locals =
{}
Class Method Summary collapse
- .configure {|_self| ... } ⇒ Object
-
.load(name, is_user_template = false) ⇒ Object
Load template, which consists of HAML + Middleman-style frontmatter.
-
.locals=(locals) ⇒ Object
Set config value, then reset cache.
-
.render_template(name, is_user_template, render_params) ⇒ Object
Do HAML render.
-
.renderer ⇒ Object
Return an ActionView instance with helpers monkeypatched in.
-
.scan_user_templates ⇒ Object
Scan user templates and populate list of names to be used by Treetop grammar to validate Alongslide directives.
-
.split_frontmatter(text) ⇒ Object
HAML templates use Middleman-style YAML frontmatter.
-
.template_path(name, is_user_template) ⇒ Object
Filesystem utility.
-
.user_template_dir=(user_template_dir) ⇒ Object
Set config value, then reset cache.
Class Method Details
.configure {|_self| ... } ⇒ Object
34 35 36 |
# File 'lib/alongslide/templates.rb', line 34 def configure yield self end |
.load(name, is_user_template = false) ⇒ Object
Load template, which consists of HAML + Middleman-style frontmatter.
61 62 63 64 65 66 |
# File 'lib/alongslide/templates.rb', line 61 def load(name, is_user_template = false) template = IO.read(template_path(name, is_user_template)) yaml, haml = split_frontmatter(template) params = YAML.load yaml return params, haml end |
.locals=(locals) ⇒ Object
Set config value, then reset cache.
102 103 104 |
# File 'lib/alongslide/templates.rb', line 102 def locals=(locals) @@locals = locals end |
.render_template(name, is_user_template, render_params) ⇒ Object
Do HAML render.
Use ActionView instead of Haml::Engine because we need our Rails helpers.
72 73 74 75 76 77 78 |
# File 'lib/alongslide/templates.rb', line 72 def render_template(name, is_user_template, render_params) template_params, haml = load(name, is_user_template) renderer.render( inline: haml, type: :haml, locals: render_params.merge(@@locals)) end |
.renderer ⇒ Object
Return an ActionView instance with helpers monkeypatched in.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/alongslide/templates.rb', line 110 def renderer action_view = ActionView::Base.new(Rails.configuration.paths["app/views"]) action_view.class_eval do include Rails.application.routes.url_helpers if defined? Rails.application.routes include ApplicationHelper if defined? ApplicationHelper def protect_against_forgery? false end end return action_view end |
.scan_user_templates ⇒ Object
Scan user templates and populate list of names to be used by Treetop grammar to validate Alongslide directives.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/alongslide/templates.rb', line 41 def scan_user_templates if @@user_template_dir @@template_names = {greedy: [], nongreedy: [], independent: []} Dir.glob(File.join @@user_template_dir, "*.haml") do |filename| name = File.basename filename, ".haml" params, template = load name, true greediness = params["greedy"] ? :greedy : :nongreedy @@template_names[greediness] << name @@template_names[:independent] << name if params["independent"] end end end |
.split_frontmatter(text) ⇒ Object
HAML templates use Middleman-style YAML frontmatter. Separate it out.
89 90 91 |
# File 'lib/alongslide/templates.rb', line 89 def split_frontmatter(text) text.split("---\n").reject(&:empty?) end |
.template_path(name, is_user_template) ⇒ Object
Filesystem utility
82 83 84 85 |
# File 'lib/alongslide/templates.rb', line 82 def template_path(name, is_user_template) template_dir = is_user_template ? @@user_template_dir : @@TEMPLATE_DIR File.join template_dir, "#{name}.haml" end |
.user_template_dir=(user_template_dir) ⇒ Object
Set config value, then reset cache.
95 96 97 98 |
# File 'lib/alongslide/templates.rb', line 95 def user_template_dir=(user_template_dir) @@user_template_dir = user_template_dir scan_user_templates end |