Widget-like API for DOM objects?

D

Daniel Berger

Hi all,

Is there something out there that would let me create DOM objects in a more
Rubyesque manner? For example, say I want to create a 'select' object in a web
page. I'd like an API something along these lines:

select = Select.new
select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
select.selected_index = 2
select.title = "Three guys"
select.access_key = "A"

p select.html # Show html that would be generated using above options

You get the picture. Is there anything like this out on the RAA? Nothing
immediately jumped out at me.

Regards,

Dan
 
H

Hannes Wyss

Dan

I can give you this: http://scm.ywesee.com/?p=3Dhtmlgrid

However, this was one of my very first ruby-projects and basically needs
a complete rewrite.. Some of the reasons:
- Geared towards Table-Layout, hence the 'grid'
- tight coupling to http://scm.ywesee.com/?p=3Dsbsm which is a
state-based session-manager
- meta-programming by configuration-constants
- naming conventions changed in mid-project
- nonexistant documentation

That said, if you want to try it out and help me get the priorities for
a next version straight, you're more than welcome to do so and I hereby
pledge my support - which you'll probably need ;)

Cheers
Hannes
 
D

Daniel Berger

Hannes said:
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Precedence: bulk
Lines: 47
List-Id: ruby-talk.ruby-lang.org
List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
List-Post: <mailto:[email protected]>
List-Owner: <mailto:[email protected]>
List-Help: <mailto:[email protected]?body=help>
List-Unsubscribe: <mailto:[email protected]?body=unsubscribe>

Dan

I can give you this: http://scm.ywesee.com/?p=3Dhtmlgrid

However, this was one of my very first ruby-projects and basically needs
a complete rewrite.. Some of the reasons:
- Geared towards Table-Layout, hence the 'grid'
- tight coupling to http://scm.ywesee.com/?p=3Dsbsm which is a
state-based session-manager
- meta-programming by configuration-constants
- naming conventions changed in mid-project
- nonexistant documentation

That said, if you want to try it out and help me get the priorities for
a next version straight, you're more than welcome to do so and I hereby
pledge my support - which you'll probably need ;)

I tried to look at the link you provided but I get a 403 forbidden. :(

Dan
 
P

Paul Duncan

--zn4k3Q+N5puqXur4
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Daniel Berger ([email protected]) said:
Hi all,
=20
Is there something out there that would let me create DOM objects in a mo= re=20
Rubyesque manner? For example, say I want to create a 'select' object in= a=20
web page. I'd like an API something along these lines:
=20
select =3D Select.new
select.options =3D ["joe", "bob", "moe"] # or {1=3D>"joe", 2=3D>"bob", 3= =3D>"moe"}
select.selected_index =3D 2
select.title =3D "Three guys"
select.access_key =3D "A"
=20
p select.html # Show html that would be generated using above options
=20
You get the picture. Is there anything like this out on the RAA? Nothin= g=20
immediately jumped out at me.

REXML has a DOM-like API, and it comes pre-installed with Ruby:

# load REXML=20
require 'rexml/document'

#
# Create an HTML select element.
#=20
class HTMLSelect
attr_accessor :eek:ptions, :attrs, :index

def initialize(options =3D [], select_attrs =3D {}, selected_index =3D =
nil)
@options, @attrs, @index =3D options, select_attrs, selected_index
end

def to_s
# create return select element and append requested attributes
ret =3D REXML::Element.new('select')
@attrs.each { |key, val| ret.attributes[key] =3D val }
=20
# iterate over, create, and append each option
@options.each_with_index do |opt, i|
# create and append element
opt_elem =3D ret << REXML::Element.new('option')

# add option text, and option attributes
opt_elem << REXML::Text.new(opt)
opt_elem.attributes['value'] =3D i.to_s
opt_elem.attributes['selected'] =3D 'Y' if i =3D=3D @index
end

# convert select element to string and return
ret.to_s
end
end

# define attributes, option items, and initial selected index
select_attrs =3D {
'name' =3D> 'which_guy',
'title' =3D> 'Three Guys',
'access_key' =3D> 'A',
}
selected_index, options =3D 2, %w{joe bob moe}
=20
# build select box and print out result
puts HTMLSelect.new(options, select_attrs, selected_index).to_s

Hope this helps...

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--zn4k3Q+N5puqXur4
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFDyARRzdlT34LClWIRAlb8AKDauzyZODM7m7DTz/lJbbeneKRirwCdHOTR
wm+STZ2qSe+aSgh9/pmohmI=
=CY2J
-----END PGP SIGNATURE-----

--zn4k3Q+N5puqXur4--
 
D

Daniel Berger

Paul said:
* Daniel Berger ([email protected]) said:
Hi all,

Is there something out there that would let me create DOM objects in a more
Rubyesque manner? For example, say I want to create a 'select' object in a
web page. I'd like an API something along these lines:

select = Select.new
select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
select.selected_index = 2
select.title = "Three guys"
select.access_key = "A"

p select.html # Show html that would be generated using above options

You get the picture. Is there anything like this out on the RAA? Nothing
immediately jumped out at me.


REXML has a DOM-like API, and it comes pre-installed with Ruby:

# load REXML
require 'rexml/document'

#
# Create an HTML select element.
#
class HTMLSelect
attr_accessor :eek:ptions, :attrs, :index

def initialize(options = [], select_attrs = {}, selected_index = nil)
@options, @attrs, @index = options, select_attrs, selected_index
end

def to_s
# create return select element and append requested attributes
ret = REXML::Element.new('select')
@attrs.each { |key, val| ret.attributes[key] = val }

# iterate over, create, and append each option
@options.each_with_index do |opt, i|
# create and append element
opt_elem = ret << REXML::Element.new('option')

# add option text, and option attributes
opt_elem << REXML::Text.new(opt)
opt_elem.attributes['value'] = i.to_s
opt_elem.attributes['selected'] = 'Y' if i == @index
end

# convert select element to string and return
ret.to_s
end
end

# define attributes, option items, and initial selected index
select_attrs = {
'name' => 'which_guy',
'title' => 'Three Guys',
'access_key' => 'A',
}
selected_index, options = 2, %w{joe bob moe}

# build select box and print out result
puts HTMLSelect.new(options, select_attrs, selected_index).to_s

Hope this helps...

Ah, thanks Paul. I'd like something prebuilt, but it looks like REXML would
serve as a great way to build things behind the scenes.

Regards,

Dan
 
K

Kero

Is there something out there that would let me create DOM objects in a more
Rubyesque manner? For example, say I want to create a 'select' object in a web
page. I'd like an API something along these lines:

select = Select.new
select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
select.selected_index = 2
select.title = "Three guys"
select.access_key = "A"

p select.html # Show html that would be generated using above options

You get the picture. Is there anything like this out on the RAA? Nothing
immediately jumped out at me.

How about the stdlib cgi?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top