renshi 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +23 -48
- data/lib/renshi/frameworks/sinatra.rb +3 -11
- data/lib/renshi/frameworks.rb +1 -1
- data/lib/renshi.rb +1 -1
- metadata +3 -3
data/README
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
Renshi is a templating language for Ruby which is versatile and cooperative with HTML. It has a concise syntax and 'attribute expressions', which let you build and combine sets of inline Ruby instructions upon HTML elements.
|
2
2
|
|
3
|
-
Renshi
|
3
|
+
Renshi gives the Ruby dev. flexibility when templating; 'attribute expressions' create well-formed XHTML, $ interpretations can be used as a conciser ERB notation.
|
4
4
|
|
5
|
-
Renshi
|
5
|
+
Renshi integrates with Rails and Sinatra out of the box. Simply 'require renshi'! In Sinatra the helper method simply use 'ren' where you would have used 'erb'/'haml'.
|
6
|
+
|
7
|
+
Renshi templates are appended with the suffix of .ren, e.g. index.html.ren or index.ren.
|
6
8
|
|
7
9
|
$ Ruby Interpretation
|
8
10
|
=====================
|
9
|
-
|
11
|
+
Like ERB, but less typing.
|
10
12
|
|
11
13
|
Use $ or ${..} where you would use <%= ... %> . The single $ is used for one word phrases.
|
12
14
|
|
@@ -21,34 +23,28 @@ $[if foo]
|
|
21
23
|
$[end]
|
22
24
|
|
23
25
|
Attribute Expressions
|
24
|
-
|
25
|
-
|
26
|
+
=====================
|
27
|
+
Attr. expressions are insertable into HTML elements as attributes.
|
26
28
|
|
27
|
-
|
29
|
+
<p r:if="user.known?">Welcome $user.name</p>
|
28
30
|
|
29
|
-
|
30
|
-
<ul>
|
31
|
-
<li r:for="section in @section.sections" r:if="authorized?(current_user, section)" id="section_$section.name">$section.name
|
32
|
-
<ul>
|
33
|
-
<li r:for="page in section.pages">${link_to page.name, "#{page.path}"}</li>
|
34
|
-
</ul>
|
35
|
-
</li>
|
36
|
-
</ul>
|
31
|
+
They can be combined on elements and are interpreted in the order of appearance and are cumulative, allowing you to program inline on the HTML structure: if a variable is created by an attr. expression it can be used within its scope.
|
37
32
|
|
38
|
-
Another e.g.
|
39
33
|
<li r:each="@sphinx.results[:words], |k,v|" r:if="v[:hits].to_i > 0">
|
40
34
|
$k - Hits $v[:hits] in $v[:docs] documents
|
41
35
|
</li>
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
If you use a variable within an attr. expression, the $ notation is not needed. It's only for insertion in regular HTML.
|
38
|
+
|
39
|
+
Renshi will automatically guess the natural ending of boolean structures.
|
40
|
+
|
41
|
+
<td r:if="dataset.published?">...</td>
|
42
|
+
<td r:else>...</td>
|
46
43
|
|
47
44
|
You don't need to supply an 'end' element; it can be guessed by the structure of the HTML document (see r:if below in the library).
|
48
45
|
|
49
|
-
|
46
|
+
None of the r:foo attr.expressions are rendered in the HTML source.
|
50
47
|
|
51
|
-
$ symbols inside of attribute values are interpreted for regular HTML attributes (they're unneeded for renshi attributes).
|
52
48
|
|
53
49
|
Attribute Expression Library
|
54
50
|
============================
|
@@ -144,7 +140,7 @@ hello99
|
|
144
140
|
Further elements will appear as I have time to make them or others want to contribute them.
|
145
141
|
There isn't much to making them. If there's one you'd especially like, let me know on lighthouse and I'll see about adding it.
|
146
142
|
|
147
|
-
The one restriction is that I want to limit
|
143
|
+
The one restriction is that I want to limit attr. expressions to one key and value combination on the element (everything fits into r:foo="expression").
|
148
144
|
I want to avoid complexity like "<span r:foo='..' r:params="k,v" />" for example - see r:each for the workaround.
|
149
145
|
|
150
146
|
See renshi/lib/renshi/attribute_expressions for how they work.
|
@@ -153,47 +149,26 @@ Other Rules
|
|
153
149
|
===========
|
154
150
|
To print a $ use $$, e.g. $$10.95 comes out as $10.95
|
155
151
|
|
156
|
-
Because the $foo evaluation can take all sorts of symbols to interpret a
|
152
|
+
Because the $foo evaluation can take all sorts of symbols to interpret a Ruby statement, if you are using a special symbol *immediately* before it, you'll have to use ${foo} instead. For example, because $greetings[0].upcase is interpretable, if you want to output "[HELLO]" where the brackets surround the string, you'd use [${greetings[0].upcase}]
|
157
153
|
|
158
|
-
The $
|
154
|
+
The $ parser *delimits* (splits) on any character which is not in this list - . " ' { } ( ) + = * / \ - @ [ ] : ? ! \w
|
159
155
|
|
160
156
|
Installation
|
161
157
|
============
|
162
158
|
Renshi is hosted on Rubyforge so 'sudo gem install renshi'. Alternatively, use github to do what you want with it.
|
163
159
|
|
164
160
|
|
165
|
-
Framework Integration
|
166
|
-
=====================
|
167
|
-
|
168
|
-
* Rails
|
169
|
-
Simply require the renshi gem in your code, and Renshi will make itself available.
|
170
|
-
|
171
|
-
There is a Rails 2.3.2 example app in the examples directory. Go there, start up the application, and visit http://localhost:3000/hello
|
172
|
-
|
173
|
-
I'd welcome integrations for Sinatra or Merb or XYZ framework. See renshi/lib/renshi/frameworks (see below - How Fast Is It?)
|
174
|
-
|
175
|
-
|
176
161
|
How Fast Is It? - Compilable Templates
|
177
162
|
======================================
|
178
|
-
I don't know - I've not benchmarked it yet. It's built solely on Nokogiri, which is very fast
|
163
|
+
I don't know - I've not benchmarked it yet. It's built solely on Nokogiri, which is very fast, and allows Renshi to be lightweight.
|
164
|
+
|
165
|
+
To a certain extent it's an irrelevant question, given that Renshi compiles .ren documents into chunks of ruby code, which are kept in memory and reused. This was written specifically for the Rails notion of compilable templates (see ActionView::TemplateHandlers::Compilable and Renshi::Frameworks::Rails).
|
179
166
|
|
180
167
|
ERB and HAML also do this (in fact I worked from the source code of both). It means, essentially, that a foo.html.ren document is compiled once into ruby, and each presentation of the template is then a matter of using the generated code (not reading the template).
|
181
168
|
|
182
169
|
Thus, n requests for the template mean 1 call to Renshi to generate the necessary code, which is then reused. At some stage I might benchmark this raw ruby - but I've written it with an eye on efficiency as it's generated and so far it seems very fast. If someone enjoys benchmarking then please jump in.
|
183
170
|
|
184
|
-
When integrating this feature with other frameworks, simply use a delegate object to hold the compiled code and evaluate it against the binding you want to use
|
185
|
-
|
186
|
-
E.g. some helper methods in the spec_helper.rb might get you started. For the integration you'd keep memory of which files had been compiled (only call compile_file once).
|
187
|
-
|
188
|
-
def compile_file(file)
|
189
|
-
compiled = Renshi::Parser.parse(read_file(file))
|
190
|
-
end
|
191
|
-
|
192
|
-
def interpret(file, context)
|
193
|
-
eval(compile_file(file), context)
|
194
|
-
end
|
195
|
-
|
196
|
-
rendered_template = interpret("index.html.ren", binding)
|
171
|
+
When integrating this feature with other frameworks, simply use a delegate object to hold the compiled code and evaluate it against the binding you want to use. See the Rails and Sinatra bridges to get you started.
|
197
172
|
|
198
173
|
Development
|
199
174
|
===========
|
@@ -1,20 +1,12 @@
|
|
1
|
-
|
2
|
-
# module Renshi
|
3
|
-
# module Frameworks
|
4
|
-
# module Sinatra
|
5
|
-
#
|
6
|
-
|
7
1
|
module Sinatra
|
8
2
|
module Templates
|
9
3
|
def ren(template, options={}, locals={})
|
10
|
-
|
4
|
+
render :ren, template, options, locals
|
11
5
|
end
|
12
6
|
|
13
7
|
private
|
14
|
-
def render_ren(data, options, locals, &block)
|
15
|
-
|
16
|
-
#
|
17
|
-
out = Renshi::Parser.parse(source)
|
8
|
+
def render_ren(template, data, options, locals, &block)
|
9
|
+
out = Renshi::Parser.parse(data)
|
18
10
|
return eval(out, binding)
|
19
11
|
end
|
20
12
|
end
|
data/lib/renshi/frameworks.rb
CHANGED
@@ -3,7 +3,7 @@ module Renshi
|
|
3
3
|
#framework integrations can be loaded within notice by checking the environment
|
4
4
|
def self.notice
|
5
5
|
require 'renshi/frameworks/rails' if defined? ENV['RAILS_ENV']
|
6
|
-
|
6
|
+
require 'renshi/frameworks/sinatra' if defined? Sinatra
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/lib/renshi.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renshi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Faiz
|
@@ -9,7 +9,7 @@ autorequire: renshi
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-10 00:00:00 +10:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -75,7 +75,7 @@ rubyforge_project:
|
|
75
75
|
rubygems_version: 1.3.5
|
76
76
|
signing_key:
|
77
77
|
specification_version: 3
|
78
|
-
summary: Renshi is a
|
78
|
+
summary: Renshi is a templating language for Ruby which is versatile and cooperative with HTML.
|
79
79
|
test_files:
|
80
80
|
- spec/data/attribute_values_parsed.ren
|
81
81
|
- spec/data/example1.ren
|