express_templates 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/express_templates/components/capabilities/parenting.rb +13 -6
- data/lib/express_templates/components/content_for.rb +36 -0
- data/lib/express_templates/components.rb +1 -0
- data/lib/express_templates/version.rb +1 -1
- data/test/components/content_for_test.rb +60 -0
- data/test/dummy/log/test.log +11862 -0
- data/test/test_helper.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07e430ee29720a9e7a29b668bdafdb09eaa997cc
|
4
|
+
data.tar.gz: 1002363331e515438fc97bebf78918d4beaca39d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d80b5b553bb5f14693789a7d6d129c7930ecd585af21ffadb5bf5793db6a2f82e5d9f6c0f8dc7da1a6ceb87df0d6e747d879287da1e3a5c2050105ed105169b
|
7
|
+
data.tar.gz: 81021e19f25c8b01bf185f3a92e39d1f89c50a001c0e37c7fcade904a862038dbce3803d5d169a990abf9001111c34b47bf27e06ab51f2786608311ee6e72ccb
|
@@ -55,18 +55,25 @@ module ExpressTemplates
|
|
55
55
|
|
56
56
|
def compile
|
57
57
|
locals = (expand_locals rescue nil).inspect
|
58
|
-
compiled_children = nil
|
59
58
|
args = %w(self)
|
60
59
|
args << locals
|
61
|
-
|
62
|
-
|
63
|
-
compiled_children.gsub!('"+"', '') # avoid unnecessary string concatenation
|
64
|
-
args << compiled_children unless compiled_children.empty?
|
65
|
-
end
|
60
|
+
compiled_children = compile_children
|
61
|
+
args << compiled_children unless compiled_children.empty?
|
66
62
|
closing_paren = compiled_children.empty? ? ')' : "\n#{Indenter.for(:compile)})"
|
67
63
|
"#{self.class.to_s}.render_with_children(#{args.join(', ')}#{closing_paren}"
|
68
64
|
end
|
69
65
|
|
66
|
+
def compile_children
|
67
|
+
compiled_children = nil
|
68
|
+
Indenter.for(:compile) do |indent, indent_with_newline|
|
69
|
+
compiled_children = children.map do |child|
|
70
|
+
indent_with_newline +
|
71
|
+
(child.compile rescue %Q("#{child}")) # Bare strings may be children
|
72
|
+
end.join("+")
|
73
|
+
compiled_children.gsub!('"+"', '') # avoid unnecessary string concatenation
|
74
|
+
end
|
75
|
+
return compiled_children
|
76
|
+
end
|
70
77
|
end
|
71
78
|
end
|
72
79
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ExpressTemplates
|
2
|
+
module Components
|
3
|
+
# Provide a wrapper for the content_for helper which
|
4
|
+
# accepts a block of express template code.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# ```ruby
|
9
|
+
# content_for(:header) {
|
10
|
+
# h1 "Title"
|
11
|
+
# }
|
12
|
+
# ```
|
13
|
+
class ContentFor < Container
|
14
|
+
include Capabilities::Configurable
|
15
|
+
def compile
|
16
|
+
children_markup = compile_children
|
17
|
+
content_label = @args[0]
|
18
|
+
result = %Q|content_for(:#{content_label}|
|
19
|
+
if children_markup.empty?
|
20
|
+
if @args[1].kind_of?(String)
|
21
|
+
children_markup = @args[1]
|
22
|
+
# append children as argument
|
23
|
+
result << %Q|, "#{children_markup}".html_safe)|
|
24
|
+
else
|
25
|
+
# no markup specified - must be referencing the content
|
26
|
+
result << ")"
|
27
|
+
end
|
28
|
+
else
|
29
|
+
# append children in block form
|
30
|
+
result << %Q|) { (#{children_markup.gsub(/^\s+/, '')}).html_safe }|
|
31
|
+
end
|
32
|
+
%Q("\#\{#{result}\}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContentForTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
class Context
|
6
|
+
def content_for(label, markup=nil, &block)
|
7
|
+
@blocks ||= {}
|
8
|
+
if block || markup
|
9
|
+
@blocks[label] = block ? block.call : markup
|
10
|
+
nil
|
11
|
+
else
|
12
|
+
@blocks[label]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
test "content_for accepts a block of express template" do
|
18
|
+
fragment = -> {
|
19
|
+
content_for(:whatever) { h1 'hello' }
|
20
|
+
}
|
21
|
+
context = Context.new
|
22
|
+
markup = ExpressTemplates.render(context, &fragment)
|
23
|
+
assert_equal %Q(<h1>hello</h1>), context.content_for(:whatever)
|
24
|
+
end
|
25
|
+
|
26
|
+
test "content_for accepts a second argument which contains markup" do
|
27
|
+
fragment = -> {
|
28
|
+
content_for :title, "Foo"
|
29
|
+
}
|
30
|
+
context = Context.new
|
31
|
+
markup = ExpressTemplates.render(context, &fragment)
|
32
|
+
assert_equal 'Foo', context.content_for(:title)
|
33
|
+
end
|
34
|
+
|
35
|
+
test "content_for without a body returns the markup" do
|
36
|
+
fragment = -> {
|
37
|
+
content_for :title
|
38
|
+
}
|
39
|
+
context = Context.new
|
40
|
+
context.content_for :title, "Foo"
|
41
|
+
markup = ExpressTemplates.render(context, &fragment)
|
42
|
+
assert_equal 'Foo', markup
|
43
|
+
end
|
44
|
+
|
45
|
+
test "content_for body is html_safe" do
|
46
|
+
arg_frag = -> {
|
47
|
+
content_for :title, "<h1>Foo</h1>"
|
48
|
+
}
|
49
|
+
context = Context.new
|
50
|
+
markup = ExpressTemplates.render(context, &arg_frag)
|
51
|
+
assert context.content_for(:title).html_safe?
|
52
|
+
|
53
|
+
block_frag = -> {
|
54
|
+
content_for(:title) { h1 "Foo" }
|
55
|
+
}
|
56
|
+
markup = ExpressTemplates.render(context, &block_frag)
|
57
|
+
assert context.content_for(:title).html_safe?
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|