dyny 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardoc/checksums +5 -0
- data/.yardoc/complete +0 -0
- data/.yardoc/object_types +0 -0
- data/.yardoc/objects/root.dat +0 -0
- data/.yardoc/proxy_types +0 -0
- data/CHANGELOG.md +15 -0
- data/README.md +2 -0
- data/VERSION +1 -0
- data/doc/Dyny/Helper.html +321 -0
- data/doc/Dyny/Railtie.html +124 -0
- data/doc/Dyny/TemplateHandler.html +207 -0
- data/doc/Dyny/Version.html +121 -0
- data/doc/Dyny.html +119 -0
- data/doc/_index.html +167 -0
- data/doc/class_list.html +54 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +58 -0
- data/doc/css/style.css +503 -0
- data/doc/file.README.html +184 -0
- data/doc/file_list.html +59 -0
- data/doc/frames.html +22 -0
- data/doc/index.html +184 -0
- data/doc/js/app.js +344 -0
- data/doc/js/full_list.js +242 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +70 -0
- data/doc/top-level-namespace.html +110 -0
- data/dyny.gemspec +7 -13
- data/lib/dyny/helper.rb +0 -1
- data/lib/dyny/version.rb +1 -7
- data/logo.svg +192 -0
- metadata +30 -8
@@ -0,0 +1,184 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>
|
7
|
+
File: README
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.37
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
|
+
|
17
|
+
<script type="text/javascript">
|
18
|
+
pathId = "README";
|
19
|
+
relpath = '';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
+
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
+
|
27
|
+
|
28
|
+
</head>
|
29
|
+
<body>
|
30
|
+
<div class="nav_wrap">
|
31
|
+
<iframe id="nav" src="file_list.html?1"></iframe>
|
32
|
+
<div id="resizer"></div>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
<div id="main" tabindex="-1">
|
36
|
+
<div id="header">
|
37
|
+
<div id="menu">
|
38
|
+
|
39
|
+
<a href="_index.html">Index</a> »
|
40
|
+
<span class="title">File: README</span>
|
41
|
+
|
42
|
+
</div>
|
43
|
+
|
44
|
+
<div id="search">
|
45
|
+
|
46
|
+
<a class="full_list_link" id="class_list_link"
|
47
|
+
href="class_list.html">
|
48
|
+
|
49
|
+
<svg width="24" height="24">
|
50
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
51
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
52
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
+
</svg>
|
54
|
+
</a>
|
55
|
+
|
56
|
+
</div>
|
57
|
+
<div class="clear"></div>
|
58
|
+
</div>
|
59
|
+
|
60
|
+
<div id="content"><div id='filecontents'>
|
61
|
+
<h1 id="label-Introducing+Dyny">Introducing Dyny</h1>
|
62
|
+
|
63
|
+
<p><img src=“logo.svg” width=250 alt=“Dyny logo”/></p>
|
64
|
+
|
65
|
+
<p>Dyny is a tiny framework for writing dynamic Rails views directly in Ruby. Typically, Rails views, are written in <a href="https://guides.rubyonrails.org/layouts_and_rendering.html">ERB</a>, <a href="https://haml.info/">HAML</a> or <a href="https://github.com/slim-template/slim">Slim</a>. However there are situations where you’d prefer to write regular ruby code without having to cope with <a href="https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html">Tag Helpers</a> at every call. This is where Dyny comes in.</p>
|
66
|
+
|
67
|
+
<p>A quick and trivial example: Having <code>MessagesController</code> and an action <code>hello_world</code>, you can write a file <code>app/views/messages/hello_world.html.dyny</code> with the contents:</p>
|
68
|
+
|
69
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_h1'>h1</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Hello world</span><span class='tstring_end'>'</span></span>
|
70
|
+
<span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card mb-3</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
71
|
+
<span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card-body</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
72
|
+
<span class='id identifier rubyid_para'>para</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>I'm inside a Bootstrap card.</span><span class='tstring_end'>"</span></span>
|
73
|
+
<span class='kw'>end</span>
|
74
|
+
<span class='kw'>end</span>
|
75
|
+
</code></pre>
|
76
|
+
<ul><li>
|
77
|
+
<p>Use directory structures similar to ERB views</p>
|
78
|
+
</li><li>
|
79
|
+
<p>The file extension is <code>.html.dyny</code></p>
|
80
|
+
</li><li>
|
81
|
+
<p>Call HTML elements as methods and nest them using blocks. There are three special cases:</p>
|
82
|
+
</li><li>
|
83
|
+
<p>Since the method name <code>p</code> is already taken, use <code>para</code> to generate a <code><p></code> tag.</p>
|
84
|
+
</li><li>
|
85
|
+
<p>Since the method names <code>label</code> and <code>select</code> are taken by Rails, use <code>tag.label</code> and <code>tag.select</code> instead.</p>
|
86
|
+
</li><li>
|
87
|
+
<p>Arguments given to Dyny HTML helper methods are passed to the <code>tag</code> helper[https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag], for instance <code>class</code> in the example above.</p>
|
88
|
+
</li><li>
|
89
|
+
<p>If you don’t want a HTML tag, but output plain text, use <code>concat</code>. See examples below.</p>
|
90
|
+
</li></ul>
|
91
|
+
|
92
|
+
<p><strong>Dyny was tested with Rails 7.</strong></p>
|
93
|
+
|
94
|
+
<h1 id="label-But+dynamic+views+are+bad-3F">But dynamic views are bad?</h1>
|
95
|
+
|
96
|
+
<p>Rails views are meant to merely display data that was already pre-processed elsewhere, such as helpers or controller actions. However, as your application gets very complex, there may be situations where adhering to that principle leads to code scattered all over your application. Also, for rapid prototyping, you might wanna start writing your first proof-of-concept code directly into the view. This is where dyny jumps in to fill the gap and lets you write views directly in Ruby.</p>
|
97
|
+
|
98
|
+
<p>Dyny does not prevent you from writing most of your views in ERB or HAML. The file name ending of the view decides which processor is used. If your file ends with <code>.html.erb</code>, you write ERB HTML; if it ends with <code>.html.dyny</code>, you can write plain Ruby with the help of Dyny.</p>
|
99
|
+
|
100
|
+
<h1 id="label-Installation">Installation</h1>
|
101
|
+
<ol><li>
|
102
|
+
<p>Add the gem to your Gemfile: <code>gem 'dyny'</code></p>
|
103
|
+
</li><li>
|
104
|
+
<p>Run <code>bundle</code> to install it.</p>
|
105
|
+
</li></ol>
|
106
|
+
|
107
|
+
<h1 id="label-Usage">Usage</h1>
|
108
|
+
|
109
|
+
<h2 id="label-In+a+template">In a template</h2>
|
110
|
+
|
111
|
+
<p>Simply have the file name of your view end with <code>.html.dyny</code> and write plain ruby in it, as shown in the example above.</p>
|
112
|
+
|
113
|
+
<h2 id="label-In+render+or+render_to_string">In <code>render</code> or <code>render_to_string</code></h2>
|
114
|
+
|
115
|
+
<p>You may speficy <code>type: :dyny</code> in your render call to have Rails process your string with Dyny. Using <code>inline</code>, you may supply a string containing ruby.</p>
|
116
|
+
|
117
|
+
<p>As an example: in your controller, you may call:</p>
|
118
|
+
|
119
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_render_to_string'>render_to_string</span><span class='lparen'>(</span>
|
120
|
+
<span class='label'>type:</span> <span class='symbol'>:dyny</span><span class='comma'>,</span>
|
121
|
+
<span class='label'>locals:</span> <span class='lbrace'>{</span> <span class='label'>foo:</span> <span class='symbol'>:bar</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
122
|
+
<span class='label'>inline:</span> <span class='heredoc_beg'><<~RUBY</span>
|
123
|
+
<span class='tstring_content'> h1 'Hello world'
|
124
|
+
</span><span class='heredoc_end'> RUBY
|
125
|
+
</span><span class='rparen'>)</span>
|
126
|
+
</code></pre>
|
127
|
+
|
128
|
+
<h1 id="label-Examples">Examples</h1>
|
129
|
+
|
130
|
+
<h2 id="label-A+trivial+index+page">A trivial index page</h2>
|
131
|
+
|
132
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_h1'>h1</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>All users</span><span class='tstring_end'>'</span></span>
|
133
|
+
<span class='id identifier rubyid_ul'>ul</span> <span class='kw'>do</span>
|
134
|
+
<span class='ivar'>@users</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_current_user'>current_user</span><span class='op'>|</span>
|
135
|
+
<span class='id identifier rubyid_li'>li</span> <span class='id identifier rubyid_current_user'>current_user</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span>
|
136
|
+
<span class='kw'>end</span>
|
137
|
+
<span class='kw'>end</span>
|
138
|
+
</code></pre>
|
139
|
+
|
140
|
+
<h2 id="label-Working+with+simple_form+and+bootstrap">Working with simple_form and bootstrap</h2>
|
141
|
+
|
142
|
+
<p>This illustrates working with Bootstrap and the popular <code>[SimpleForm](https://github.com/heartcombo/simple_form)</code> gem. For a better overview, we first save the HTML generated by <code>simple_form_for</code> into the local variable <code>form_html</code> and use <code>concat</code> to output it later.</p>
|
143
|
+
|
144
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card card-body</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
145
|
+
<span class='id identifier rubyid_form_html'>form_html</span> <span class='op'>=</span> <span class='id identifier rubyid_simple_form_for'>simple_form_for</span> <span class='symbol'>:test</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
|
146
|
+
<span class='id identifier rubyid_concat'>concat</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_input'>input</span><span class='lparen'>(</span><span class='symbol'>:fun</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
147
|
+
<span class='id identifier rubyid_div'>div</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_input'>input</span><span class='lparen'>(</span><span class='symbol'>:stuff</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>mb-3</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
148
|
+
<span class='id identifier rubyid_concat'>concat</span> <span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_submit'>submit</span>
|
149
|
+
<span class='kw'>end</span>
|
150
|
+
<span class='id identifier rubyid_concat'>concat</span> <span class='id identifier rubyid_form_html'>form_html</span>
|
151
|
+
<span class='kw'>end</span>
|
152
|
+
</code></pre>
|
153
|
+
|
154
|
+
<p>The result is a form with two fields and a submit button.</p>
|
155
|
+
|
156
|
+
<h1 id="label-Differences+to+the+Arbre+gem">Differences to the Arbre gem</h1>
|
157
|
+
|
158
|
+
<p>This project aims to replace <a href="https://github.com/activeadmin/arbre">Arbre</a> which has similar intentions. The key differences to Arbre are:</p>
|
159
|
+
<ul><li>
|
160
|
+
<p>Dyny is much smaller and simpler than Arbre but achieves the same features.</p>
|
161
|
+
</li><li>
|
162
|
+
<p>Dyny supports using controller instance variable in views while Arbre does not</p>
|
163
|
+
</li><li>
|
164
|
+
<p>Dyny properly uses the <code>ActionView</code> buffer, supporting calls to <code>concat</code> or <code>capture</code>. This means that Dyny fully supports gems making use of these features, such as <a href="https://github.com/heartcombo/simple_form">SimpleForm</a>, while Arbre breaks such gems because it uses its own buffer.</p>
|
165
|
+
</li><li>
|
166
|
+
<p>Where Arbre uses <code>text_node</code>, Dyny lets you use the native Rails method <code>concat</code> to output raw text.</p>
|
167
|
+
</li><li>
|
168
|
+
<p>Arbre has its own <code>Context</code> concept while Dyny does not. To have Dyny generated HTML saved to a string variable, use Rails’ <code>render_to_string</code> method instead, see example above.</p>
|
169
|
+
</li></ul>
|
170
|
+
|
171
|
+
<h1 id="label-Contributing">Contributing</h1>
|
172
|
+
|
173
|
+
<p>You are welcome to contribute to this project via the regular fork and pull request procedure.</p>
|
174
|
+
</div></div>
|
175
|
+
|
176
|
+
<div id="footer">
|
177
|
+
Generated on Wed Jan 29 17:57:41 2025 by
|
178
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
179
|
+
0.9.37 (ruby-3.3.5).
|
180
|
+
</div>
|
181
|
+
|
182
|
+
</div>
|
183
|
+
</body>
|
184
|
+
</html>
|
data/doc/file_list.html
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html >
|
3
|
+
<head>
|
4
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
5
|
+
<meta charset="utf-8" />
|
6
|
+
|
7
|
+
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" />
|
8
|
+
|
9
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
14
|
+
|
15
|
+
<script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
|
16
|
+
|
17
|
+
|
18
|
+
<title>File List</title>
|
19
|
+
<base id="base_target" target="_parent" />
|
20
|
+
</head>
|
21
|
+
<body>
|
22
|
+
<div id="content">
|
23
|
+
<div class="fixed_header">
|
24
|
+
<h1 id="full_list_header">File List</h1>
|
25
|
+
<div id="full_list_nav">
|
26
|
+
|
27
|
+
<span><a target="_self" href="class_list.html">
|
28
|
+
Classes
|
29
|
+
</a></span>
|
30
|
+
|
31
|
+
<span><a target="_self" href="method_list.html">
|
32
|
+
Methods
|
33
|
+
</a></span>
|
34
|
+
|
35
|
+
<span><a target="_self" href="file_list.html">
|
36
|
+
Files
|
37
|
+
</a></span>
|
38
|
+
|
39
|
+
</div>
|
40
|
+
|
41
|
+
<div id="search">
|
42
|
+
<label for="search-class">Search:</label>
|
43
|
+
<input id="search-class" type="text" />
|
44
|
+
</div>
|
45
|
+
</div>
|
46
|
+
|
47
|
+
<ul id="full_list" class="file">
|
48
|
+
|
49
|
+
|
50
|
+
<li id="object_README" class="odd">
|
51
|
+
<div class="item"><span class="object_link"><a href="index.html" title="README">README</a></span></div>
|
52
|
+
</li>
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
</ul>
|
57
|
+
</div>
|
58
|
+
</body>
|
59
|
+
</html>
|
data/doc/frames.html
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<title>Documentation by YARD 0.9.37</title>
|
6
|
+
</head>
|
7
|
+
<script type="text/javascript">
|
8
|
+
var mainUrl = 'index.html';
|
9
|
+
try {
|
10
|
+
var match = decodeURIComponent(window.location.hash).match(/^#!(.+)/);
|
11
|
+
var name = match ? match[1] : mainUrl;
|
12
|
+
var url = new URL(name, location.href);
|
13
|
+
window.top.location.replace(url.origin === location.origin ? name : mainUrl);
|
14
|
+
} catch (e) {
|
15
|
+
window.top.location.replace(mainUrl);
|
16
|
+
}
|
17
|
+
</script>
|
18
|
+
<noscript>
|
19
|
+
<h1>Oops!</h1>
|
20
|
+
<h2>YARD requires JavaScript!</h2>
|
21
|
+
</noscript>
|
22
|
+
</html>
|
data/doc/index.html
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>
|
7
|
+
File: README
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.37
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
|
+
|
17
|
+
<script type="text/javascript">
|
18
|
+
pathId = "README";
|
19
|
+
relpath = '';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
+
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
+
|
27
|
+
|
28
|
+
</head>
|
29
|
+
<body>
|
30
|
+
<div class="nav_wrap">
|
31
|
+
<iframe id="nav" src="class_list.html?1"></iframe>
|
32
|
+
<div id="resizer"></div>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
<div id="main" tabindex="-1">
|
36
|
+
<div id="header">
|
37
|
+
<div id="menu">
|
38
|
+
|
39
|
+
<a href="_index.html">Index</a> »
|
40
|
+
<span class="title">File: README</span>
|
41
|
+
|
42
|
+
</div>
|
43
|
+
|
44
|
+
<div id="search">
|
45
|
+
|
46
|
+
<a class="full_list_link" id="class_list_link"
|
47
|
+
href="class_list.html">
|
48
|
+
|
49
|
+
<svg width="24" height="24">
|
50
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
51
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
52
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
+
</svg>
|
54
|
+
</a>
|
55
|
+
|
56
|
+
</div>
|
57
|
+
<div class="clear"></div>
|
58
|
+
</div>
|
59
|
+
|
60
|
+
<div id="content"><div id='filecontents'>
|
61
|
+
<h1 id="label-Introducing+Dyny">Introducing Dyny</h1>
|
62
|
+
|
63
|
+
<p><img src=“logo.svg” width=250 alt=“Dyny logo”/></p>
|
64
|
+
|
65
|
+
<p>Dyny is a tiny framework for writing dynamic Rails views directly in Ruby. Typically, Rails views, are written in <a href="https://guides.rubyonrails.org/layouts_and_rendering.html">ERB</a>, <a href="https://haml.info/">HAML</a> or <a href="https://github.com/slim-template/slim">Slim</a>. However there are situations where you’d prefer to write regular ruby code without having to cope with <a href="https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html">Tag Helpers</a> at every call. This is where Dyny comes in.</p>
|
66
|
+
|
67
|
+
<p>A quick and trivial example: Having <code>MessagesController</code> and an action <code>hello_world</code>, you can write a file <code>app/views/messages/hello_world.html.dyny</code> with the contents:</p>
|
68
|
+
|
69
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_h1'>h1</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Hello world</span><span class='tstring_end'>'</span></span>
|
70
|
+
<span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card mb-3</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
71
|
+
<span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card-body</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
72
|
+
<span class='id identifier rubyid_para'>para</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>I'm inside a Bootstrap card.</span><span class='tstring_end'>"</span></span>
|
73
|
+
<span class='kw'>end</span>
|
74
|
+
<span class='kw'>end</span>
|
75
|
+
</code></pre>
|
76
|
+
<ul><li>
|
77
|
+
<p>Use directory structures similar to ERB views</p>
|
78
|
+
</li><li>
|
79
|
+
<p>The file extension is <code>.html.dyny</code></p>
|
80
|
+
</li><li>
|
81
|
+
<p>Call HTML elements as methods and nest them using blocks. There are three special cases:</p>
|
82
|
+
</li><li>
|
83
|
+
<p>Since the method name <code>p</code> is already taken, use <code>para</code> to generate a <code><p></code> tag.</p>
|
84
|
+
</li><li>
|
85
|
+
<p>Since the method names <code>label</code> and <code>select</code> are taken by Rails, use <code>tag.label</code> and <code>tag.select</code> instead.</p>
|
86
|
+
</li><li>
|
87
|
+
<p>Arguments given to Dyny HTML helper methods are passed to the <code>tag</code> helper[https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag], for instance <code>class</code> in the example above.</p>
|
88
|
+
</li><li>
|
89
|
+
<p>If you don’t want a HTML tag, but output plain text, use <code>concat</code>. See examples below.</p>
|
90
|
+
</li></ul>
|
91
|
+
|
92
|
+
<p><strong>Dyny was tested with Rails 7.</strong></p>
|
93
|
+
|
94
|
+
<h1 id="label-But+dynamic+views+are+bad-3F">But dynamic views are bad?</h1>
|
95
|
+
|
96
|
+
<p>Rails views are meant to merely display data that was already pre-processed elsewhere, such as helpers or controller actions. However, as your application gets very complex, there may be situations where adhering to that principle leads to code scattered all over your application. Also, for rapid prototyping, you might wanna start writing your first proof-of-concept code directly into the view. This is where dyny jumps in to fill the gap and lets you write views directly in Ruby.</p>
|
97
|
+
|
98
|
+
<p>Dyny does not prevent you from writing most of your views in ERB or HAML. The file name ending of the view decides which processor is used. If your file ends with <code>.html.erb</code>, you write ERB HTML; if it ends with <code>.html.dyny</code>, you can write plain Ruby with the help of Dyny.</p>
|
99
|
+
|
100
|
+
<h1 id="label-Installation">Installation</h1>
|
101
|
+
<ol><li>
|
102
|
+
<p>Add the gem to your Gemfile: <code>gem 'dyny'</code></p>
|
103
|
+
</li><li>
|
104
|
+
<p>Run <code>bundle</code> to install it.</p>
|
105
|
+
</li></ol>
|
106
|
+
|
107
|
+
<h1 id="label-Usage">Usage</h1>
|
108
|
+
|
109
|
+
<h2 id="label-In+a+template">In a template</h2>
|
110
|
+
|
111
|
+
<p>Simply have the file name of your view end with <code>.html.dyny</code> and write plain ruby in it, as shown in the example above.</p>
|
112
|
+
|
113
|
+
<h2 id="label-In+render+or+render_to_string">In <code>render</code> or <code>render_to_string</code></h2>
|
114
|
+
|
115
|
+
<p>You may speficy <code>type: :dyny</code> in your render call to have Rails process your string with Dyny. Using <code>inline</code>, you may supply a string containing ruby.</p>
|
116
|
+
|
117
|
+
<p>As an example: in your controller, you may call:</p>
|
118
|
+
|
119
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_render_to_string'>render_to_string</span><span class='lparen'>(</span>
|
120
|
+
<span class='label'>type:</span> <span class='symbol'>:dyny</span><span class='comma'>,</span>
|
121
|
+
<span class='label'>locals:</span> <span class='lbrace'>{</span> <span class='label'>foo:</span> <span class='symbol'>:bar</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
122
|
+
<span class='label'>inline:</span> <span class='heredoc_beg'><<~RUBY</span>
|
123
|
+
<span class='tstring_content'> h1 'Hello world'
|
124
|
+
</span><span class='heredoc_end'> RUBY
|
125
|
+
</span><span class='rparen'>)</span>
|
126
|
+
</code></pre>
|
127
|
+
|
128
|
+
<h1 id="label-Examples">Examples</h1>
|
129
|
+
|
130
|
+
<h2 id="label-A+trivial+index+page">A trivial index page</h2>
|
131
|
+
|
132
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_h1'>h1</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>All users</span><span class='tstring_end'>'</span></span>
|
133
|
+
<span class='id identifier rubyid_ul'>ul</span> <span class='kw'>do</span>
|
134
|
+
<span class='ivar'>@users</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_current_user'>current_user</span><span class='op'>|</span>
|
135
|
+
<span class='id identifier rubyid_li'>li</span> <span class='id identifier rubyid_current_user'>current_user</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span>
|
136
|
+
<span class='kw'>end</span>
|
137
|
+
<span class='kw'>end</span>
|
138
|
+
</code></pre>
|
139
|
+
|
140
|
+
<h2 id="label-Working+with+simple_form+and+bootstrap">Working with simple_form and bootstrap</h2>
|
141
|
+
|
142
|
+
<p>This illustrates working with Bootstrap and the popular <code>[SimpleForm](https://github.com/heartcombo/simple_form)</code> gem. For a better overview, we first save the HTML generated by <code>simple_form_for</code> into the local variable <code>form_html</code> and use <code>concat</code> to output it later.</p>
|
143
|
+
|
144
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_div'>div</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>card card-body</span><span class='tstring_end'>'</span></span> <span class='kw'>do</span>
|
145
|
+
<span class='id identifier rubyid_form_html'>form_html</span> <span class='op'>=</span> <span class='id identifier rubyid_simple_form_for'>simple_form_for</span> <span class='symbol'>:test</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_f'>f</span><span class='op'>|</span>
|
146
|
+
<span class='id identifier rubyid_concat'>concat</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_input'>input</span><span class='lparen'>(</span><span class='symbol'>:fun</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
147
|
+
<span class='id identifier rubyid_div'>div</span><span class='lparen'>(</span><span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_input'>input</span><span class='lparen'>(</span><span class='symbol'>:stuff</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='label'>class:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>mb-3</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
148
|
+
<span class='id identifier rubyid_concat'>concat</span> <span class='id identifier rubyid_f'>f</span><span class='period'>.</span><span class='id identifier rubyid_submit'>submit</span>
|
149
|
+
<span class='kw'>end</span>
|
150
|
+
<span class='id identifier rubyid_concat'>concat</span> <span class='id identifier rubyid_form_html'>form_html</span>
|
151
|
+
<span class='kw'>end</span>
|
152
|
+
</code></pre>
|
153
|
+
|
154
|
+
<p>The result is a form with two fields and a submit button.</p>
|
155
|
+
|
156
|
+
<h1 id="label-Differences+to+the+Arbre+gem">Differences to the Arbre gem</h1>
|
157
|
+
|
158
|
+
<p>This project aims to replace <a href="https://github.com/activeadmin/arbre">Arbre</a> which has similar intentions. The key differences to Arbre are:</p>
|
159
|
+
<ul><li>
|
160
|
+
<p>Dyny is much smaller and simpler than Arbre but achieves the same features.</p>
|
161
|
+
</li><li>
|
162
|
+
<p>Dyny supports using controller instance variable in views while Arbre does not</p>
|
163
|
+
</li><li>
|
164
|
+
<p>Dyny properly uses the <code>ActionView</code> buffer, supporting calls to <code>concat</code> or <code>capture</code>. This means that Dyny fully supports gems making use of these features, such as <a href="https://github.com/heartcombo/simple_form">SimpleForm</a>, while Arbre breaks such gems because it uses its own buffer.</p>
|
165
|
+
</li><li>
|
166
|
+
<p>Where Arbre uses <code>text_node</code>, Dyny lets you use the native Rails method <code>concat</code> to output raw text.</p>
|
167
|
+
</li><li>
|
168
|
+
<p>Arbre has its own <code>Context</code> concept while Dyny does not. To have Dyny generated HTML saved to a string variable, use Rails’ <code>render_to_string</code> method instead, see example above.</p>
|
169
|
+
</li></ul>
|
170
|
+
|
171
|
+
<h1 id="label-Contributing">Contributing</h1>
|
172
|
+
|
173
|
+
<p>You are welcome to contribute to this project via the regular fork and pull request procedure.</p>
|
174
|
+
</div></div>
|
175
|
+
|
176
|
+
<div id="footer">
|
177
|
+
Generated on Wed Jan 29 17:57:41 2025 by
|
178
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
179
|
+
0.9.37 (ruby-3.3.5).
|
180
|
+
</div>
|
181
|
+
|
182
|
+
</div>
|
183
|
+
</body>
|
184
|
+
</html>
|