Skip to content

Commit 740e901

Browse files
committed
use new api for rendering templates
1 parent cb0ca95 commit 740e901

8 files changed

+45
-32
lines changed

examples/slim_properties.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
3+
4+
include UI::Template
45

56
template =<<EOF
67
main_dialog
@@ -10,5 +11,5 @@
1011
push_button HStretch=false Ok
1112
EOF
1213

13-
dialog = UI.slim(template, self)
14+
dialog = render text: template
1415
dialog.wait_for_event

examples/slim_template.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
3+
require 'ui/template'
44

5+
include UI::Template
56
template =<<EOF
67
main_dialog
78
vbox
@@ -10,5 +11,5 @@
1011
push_button Ok
1112
EOF
1213

13-
dialog = UI.slim(template, self)
14+
dialog = render text: template
1415
dialog.wait_for_event

examples/slim_template_from_file.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
3+
4+
include UI::Template
45

56
TEMPLATE_FILE = File.expand_path("../wizard1.uit",__FILE__)
67
TEMPLATE_FILE2 = File.expand_path("../wizard2.uit",__FILE__)
78

8-
dialog = UI.slim(File.read(TEMPLATE_FILE), self)
9+
dialog = render file: TEMPLATE_FILE
910
puts dialog.id
1011
puts dialog[:Enabled]
1112
puts dialog.properties.inspect
@@ -15,6 +16,6 @@
1516
map[:first] = dialog.find_widget(:i1)[:Value]
1617
map[:second] = dialog.find_widget(:i2)[:Value]
1718
dialog.destroy!
18-
dialog2 = UI.slim(File.read(TEMPLATE_FILE2),:context => map)
19+
dialog2 = render file: TEMPLATE_FILE2, context: map
1920
dialog2.wait_for_event
2021
puts dialog2.id

examples/slim_template_test.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
3+
4+
include UI::Template
45

56
template =<<EOF
67
main_dialog
@@ -17,7 +18,7 @@
1718
push_button id=1 activated=Proc.new{|event,dialog| event.widget[:Label] = event.widget[:Label]+"!";false} Ok
1819
EOF
1920

20-
dialog = UI.slim(template, binding)
21+
dialog = render text: template
2122
event = dialog.wait_for_event
2223
puts event.inspect
2324
event.widget[:Label] = "No longer OK"

examples/slim_template_with_loops.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
3+
4+
include UI::Template
45

56
template =<<EOF
67
main_dialog
@@ -14,5 +15,5 @@
1415
push_button Ok
1516
EOF
1617

17-
dialog = UI.slim(template, self)
18+
dialog = render text: template
1819
dialog.wait_for_event

examples/user.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
22
require 'ui'
3-
require 'ui/builder/slim'
43

5-
EDIT_DIALOG_TEMPLATE = File.read(File.expand_path("../user_edit.yui",__FILE__))
6-
SHOW_DIALOG_TEMPLATE = File.read(File.expand_path("../users.yui",__FILE__))
4+
EDIT_DIALOG_TEMPLATE = File.expand_path("../user_edit.yui",__FILE__)
5+
SHOW_DIALOG_TEMPLATE = File.expand_path("../users.yui",__FILE__)
76

87
class User
98
ATTRS = [ :first_name, :surname, :hair, :skill, :beer ]
@@ -15,8 +14,10 @@ def initialize
1514
end
1615
end
1716

17+
include UI::Template
18+
1819
def edit_user user
19-
dialog = UI.slim(EDIT_DIALOG_TEMPLATE, user)
20+
dialog = render file: EDIT_DIALOG_TEMPLATE, context: user
2021
dialog.find(:clear).activated do |event,dialog|
2122
User::ATTRS.each { |a| dialog.find(a)[:Value] = "" }
2223
false
@@ -51,7 +52,7 @@ def add_user_to_dialog user,parent
5152

5253
def show_users users
5354
@users = users
54-
dialog = UI.slim(SHOW_DIALOG_TEMPLATE, self)
55+
dialog = render file: SHOW_DIALOG_TEMPLATE
5556
dialog.find(:save).activated do |event,dialog|
5657
path = UI.ask_for_existing_file Dir.pwd,"target File"
5758
puts users.to_json

lib/ui.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'ui/widget'
77
require 'ui/item'
88
require 'ui/selection_widget'
9+
require 'ui/template'
910

1011
module UI
1112

@@ -82,4 +83,4 @@ def self.replace(id, &block)
8283
current_dialog.replace(id, &block)
8384
end
8485

85-
end
86+
end

lib/ui/builder/slim.rb lib/ui/template.rb

+21-15
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
require 'pp'
1010

1111
module UI
12-
module Builder
13-
12+
module Template
1413
# This module allows to build user interfaces
1514
# using slim templates (http://www.slim-lang.org)
1615
#
@@ -160,7 +159,7 @@ def parse_attributes attrs
160159

161160
def on_slim_tag(name, attrs, body)
162161
attrs_str = parse_attributes attrs
163-
if LEAF_ELEMENTS.include?(name.to_sym)
162+
if Builder::LEAF_ELEMENTS.include?(name.to_sym)
164163
attrs_str.prepend ", " unless attrs_str.empty?
165164
body = compile(body)
166165
if body.empty?
@@ -171,7 +170,7 @@ def on_slim_tag(name, attrs, body)
171170
body,
172171
[:dynamic, attrs_str + "\n"]
173172
]
174-
elsif (CONTAINER_ELEMENTS+TOPLEVEL_ELEMENTS).include?(name.to_sym)
173+
elsif (Builder::CONTAINER_ELEMENTS+Builder::TOPLEVEL_ELEMENTS).include?(name.to_sym)
175174
attrs_str = "("+attrs_str+")" unless attrs_str.empty?
176175

177176
[ :multi,
@@ -200,18 +199,25 @@ class Engine < Temple::Engine
200199
end
201200

202201
end
203-
end
204-
end
205202

206-
module UI
203+
# Builds a widget/dialog using a slim template
204+
# mandatory part of options is either :file or :text key that
205+
# specify template
206+
#
207+
# {include:file:examples/slim_template.rb}
208+
def render(options={})
209+
raise "specify template to render by file: or text: key" if !options[:file] && !options[:text]
210+
io = options.delete(:text) || File.read(options.delete(:file))
211+
212+
options[:context] ||= self
213+
214+
code = UI::Template::Slim::Engine.new(options).call(io)
215+
File.write("/tmp/ui.log", code)
216+
puts options.inspect
217+
options[:context].extend UI::Builder
218+
options[:context].instance_eval code
219+
end
207220

208-
# Builds a widget/dialog using a slim template
209-
#
210-
# {include:file:examples/slim_template.rb}
211-
def self.slim(io, context, options={})
212-
code = UI::Builder::Slim::Engine.new(options).call(io)
213-
File.write("/tmp/ui.log", code)
214-
context.extend UI::Builder
215-
context.instance_eval code
216221
end
217222
end
223+

0 commit comments

Comments
 (0)