inject yaml data into liquid templates. closes #111

This commit is contained in:
Thomas Reynolds 2011-09-06 11:12:20 -07:00
parent d79eb3fc0f
commit 6eadb0e888
8 changed files with 47 additions and 8 deletions

View file

@ -3,8 +3,8 @@
- Support accessing variables and data objects in ERb Sprockets files (library.js.coffee.erb)
- Make :markdown_engine support simple symbol names (:maruku instead of ::Tilt::MarkukuTemplate)
- Update Padrino deps to 0.10.2
- Update EventMachine to pre for Windows users
- Include therubyracer on *nix
- Enable frontmatter for Liquid templates
2.0.7
=====

View file

@ -4,4 +4,9 @@ Feature: Local Data API
Scenario: Rendering html
Given the Server is running at "test-app"
When I go to "/data.html"
Then I should see "One:Two"
Then I should see "One:Two"
Scenario: Rendering liquid
Given the Server is running at "test-app"
When I go to "/data2.html"
Then I should see "OneTwo"

View file

@ -19,6 +19,7 @@ with_layout false do
page "/spaces in file.html"
page "/slim.html"
page "/data.html"
page "/data2.html"
page "/page-classes.html"
page "/sub1/page-classes.html"
page "/sub1/sub2/page-classes.html"

View file

@ -0,0 +1 @@
{% for item in data.test %}{{item.title}}{% endfor %}

View file

@ -172,7 +172,12 @@ module Middleman::Base
render_options = { :layout => layout }
render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine
request_path = request.path_info.gsub("%20", " ")
result = render(request_path, render_options)
path, engine = resolve_template(request_path)
locals = {}
locals[:data] = data.to_h if engine == :liquid
result = render(engine, path, render_options, locals)
settings.set :layout, old_layout
if result

View file

@ -22,7 +22,7 @@ module Middleman::CoreExtensions::Data
@app = app
end
def method_missing(path)
def data_for_path(path)
response = nil
@@local_sources ||= {}
@ -38,11 +38,37 @@ module Middleman::CoreExtensions::Data
response = YAML.load_file(file_path)
end
end
end
def method_missing(path)
result = data_for_path(path)
if response
recursively_enhance(response)
if result
recursively_enhance(result)
else
super
end
end
def to_h
data = {}
(@@local_sources || {}).each do |k, v|
data[k] = data_for_path(k)
end
(@@callback_sources || {}).each do |k, v|
data[k] = data_for_path(k)
end
yaml_path = File.join(@app.root, @app.data_dir, "*.yml")
Dir[yaml_path].each do |f|
p = f.split("/").last.gsub(".yml", "")
data[p] = data_for_path(p)
end
data
end
def self.data_content(name, content)
@@local_sources ||= {}

View file

@ -80,7 +80,7 @@ module Middleman::CoreExtensions::FrontMatter
module YamlAware
def prepare
options, @data = Middleman::CoreExtensions::FrontMatter.parse_front_matter(@data)
@frontmatter, @data = Middleman::CoreExtensions::FrontMatter.parse_front_matter(@data)
super
end
end

View file

@ -38,7 +38,7 @@ Gem::Specification.new do |s|
eos
s.add_dependency("rack", ["~> 1.3.0"])
s.add_dependency("eventmachine", ["1.0.0.beta.3"])
# s.add_dependency("eventmachine", ["1.0.0.beta.3"])
s.add_dependency("thin", ["~> 1.2.11"])
s.add_dependency("thor", ["~> 0.14.0"])
s.add_dependency("tilt", ["~> 1.3.1"])
@ -74,6 +74,7 @@ eos
# Development and test
# s.add_development_dependency("jquery-rails")
s.add_development_dependency("coffee-filter", ["~> 0.1.1"])
s.add_development_dependency("liquid", ["~> 2.2.0"])
s.add_development_dependency("cucumber", ["~> 1.0.2"])
s.add_development_dependency("rake", ["0.8.7"])
s.add_development_dependency("rspec", ["~> 2.6.0"])