/
core.dryml
136 lines (93 loc) · 5.81 KB
/
core.dryml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!-- Core DRYML tags. These are included implicitly and are always available. Contains mainly control-flow tags. -->
<!-- Call the tag given by the `tag` attribute. This lets you call tags dynamically based on some runtime value.
It's the DRYML equivalent of Ruby's `send` method.
-->
<def tag="call-tag" attrs="tag">
<%= send(tag.gsub('-', '_'), attributes, parameters) %>
</def>
<!-- Wrap the body in the tag specified by the `tag` attribute, iff `when` is true.
Using regular DRYML conditional logic it is rather akward to conditionally wrap some tag in another tag. This tag makes it easy to do that.
### Usage
For example, you might want to wrap an `<img>` tag in an `<a>` tag but only under certain conditions. Say the current context has an `href` attribute that may or may not be nil. We want to wrap the img in `<a>` if `href` is not nil:
<wrap when="&this.href.present?" tag="a" href="&this.href"><img src="&this.img_filename"/></wrap>
{: .dryml}
-->
<def tag="wrap" attrs="tag, when, parameter">
<% parameter ||= :default %>
<%= when_ ? send(tag, attributes, { parameter.to_sym => parameters[:default] }) : parameters.default %>
</def>
<!-- DRYML version of `render(:partial => 'my_partial')`
### Usage
<partial name="my-partial" locals="&{:x => 10, :y => 20}"/>
-->
<def tag="partial" attrs="name, locals"><%=
locals ||= {}
render(:partial => name, :locals => locals.merge(:this => this))
%></def>
<!-- Repeat a section of mark-up. The context should be a collection (anything that responds to `each`). The content of the call to `<repeat>` will be repeated for each item in the collection, and the context will be set to each item in turn.
### Attributes
- join: The value of this attribute, if given, will be inserted between each of the items (e.g. `join=", "` is very common).
-->
<def tag="repeat" attrs="join"><if><%=
raise ArgumentError, "Cannot <repeat> on #{this.inspect}" unless this.respond_to? :each
context_map do
parameters.default
end.safe_join(join)
%></if></def>
<!-- The 'do nothing' tag. Used to add parameters or change context without adding any markup -->
<def tag="do"><%= parameters.default %></def>
<!-- Alias of `do` -->
<def tag="with" alias-of="do"/>
<!-- DRYML's 'if' test
### Usage
<if test="¤t_user.administrtator?">Logged in as administrator</if>
<else>Logged in as normal user</else>
**IMPORTANT NOTE**: `<if>` tests for non-blank vs. blank (as defined by ActiveSuport), not true vs. false.
If you do not give the `test` attribute, uses the current context instead. This allows a nice trick like this:
<if:comments>...</if>
This has the double effect of changing the context to the `this.comments`, and only evaluating the body if there are comments (because an empty
collection is considered blank)
-->
<def tag="if" attrs="test"><%=
test = all_attributes.fetch(:test, this)
res = (cond = !test.blank?) ? parameters.default : ""
Dryml.last_if = cond
res
%></def>
<!-- General purpose `else` clause.
`<else>` works with various tags such as `<if>` and `<repeat>` (the else clause will be output if the collection was empty). It simply outputs its content if `Dryml.last_if` is false. This is pretty much a crazy hack which violates many good principles of language design, but it's very useful : )
-->
<def tag="else"><%= parameters.default unless Dryml.last_if %></def>
<!-- Same behaviour as `<if>`, except the test is negated. -->
<def tag="unless" attrs="test"><%=
test = all_attributes.fetch(:test, this)
res = (cond = test.blank?) ? parameters.default : ""
Dryml.last_if = cond
res
%></def>
<!-- nodoc. -->
<def tag="fake-field-context" attrs="fake-field, context"><%=
res = ""
new_field_context(fake_field, context) { res << parameters.default }
raw res
%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="html"><%=raw "<html#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</html>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="table"><%=raw "<table#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</table>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="a"><%=raw "<a#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</a>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="section"><%=raw "<section#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</section>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="header"><%=raw "<header#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</header>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="footer"><%=raw "<footer#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</footer>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="form"><%=raw "<form#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</form>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="submit"><%=raw "<submit#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</submit>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="input"><%=raw "<input#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</input>" -%></def>
<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="link"><%=raw "<link#{tag_options(attributes, true)}>" -%><do param="default"/><%= "</link>" -%></def>