Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:10:59+00:00 2026-05-26T06:10:59+00:00

I have a html file has the general design (some div’s) and I need

  • 0

I have a html file has the general design (some div’s) and I need to fill this div’s with some html code Using ruby script.
any suggests?

example

I have page.html

<html>
<title>html Page</title>
<body>

<div id="main">
</div>

<div id="side">
</div>

</body>
</html>

and a ruby script inside it i collect some data and doing some kind of processing on it and i want to present it in a nice format**
so I want to set the div which it’s id=main with some html code to be like this

<html>
<title>html Page</title>
<body>

<div id="main">
<h1>you have 30 files in games folder</h1>
</div>

<div id="side">
</div>

</body>
</html>

** why i don’t use ROR? because I don’t want to build a web site I just need to build a desktop tool but it’s presentation layer is html code interpreted by browser to avoid working with graphics libraries


my problem isn’t “how can I write to this html file” I can handle it.
my problem that If I want to create a table in the html file inside main div
I will wrote the whole html code inside the ruby script to print it to the html file, is there any lib or gem that i can tell it that I want a table with 3 rows and 2 columns and it generates the html code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:11:00+00:00Added an answer on May 26, 2026 at 6:11 am

    I historically have used ERB and REXML for things like this, since they both ship with Ruby (removing gem dependencies). You can combine one XML file (content) with one .erb file (for layout) and get simple merging. Here’s a script I wrote for this (most of which is argument handling and extending REXML with some convenience methods):

    USAGE = <<ENDUSAGE
    Usage:
       rubygen source_xml [-t template_file] [-o output_file]
    
       -t,--template  The ERB template file to merge (default: xml_name.erb)
    
       -o,--output    The output file name to write  (default: template.txt)
                      If the template_file is named "somefile_XXX.yyy",
                      the output_file will default instead to "somefile.XXX"
    ENDUSAGE
    
    ARGS = {}
    UNFLAGGED_ARGS = [ :source_xml ]
    next_arg = UNFLAGGED_ARGS.first
    ARGV.each{ |arg|
       case arg
         when '-t','--template'
           next_arg = :template_file
         when '-o','--output'
           next_arg = :output_file
         else
           if next_arg
             ARGS[next_arg] = arg
             UNFLAGGED_ARGS.delete( next_arg )
           end
           next_arg = UNFLAGGED_ARGS.first
       end
    }
    
    if !ARGS[:source_xml]
       puts USAGE
       exit
    end
    
    extension_match = /\.[^.]+$/
    template_match  = /_([^._]+)\.[^.]+$/
    xml_file      = ARGS[ :source_xml   ]
    template_file = ARGS[ :template_file] || xml_file.sub( extension_match, '.erb' )
    output_file   = ARGS[ :output_file  ] || ( ( template_file =~ template_match ) ? template_file.sub( template_match, '.\\1' ) : template_file.sub( extension_match, '.txt' ) )
    
    require 'rexml/document'
    include REXML
    
    class REXML::Element
      # Find all descendant nodes with a specified tag name and/or attributes
      def find_all( tag_name='*', attributes_to_match={} )
        self.each_element( ".//#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
      end
      # Find all child nodes with a specified tag name and/or attributes
      def kids( tag_name='*', attributes_to_match={} )
        self.each_element( "./#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
      end
      def self.xpathfor( tag_name='*', attributes_to_match={} )
        out = "#{tag_name}"
        unless attributes_to_match.empty?
          out << "["
          out << attributes_to_match.map{ |key,val|
            if val == :not_empty
              "@#{key}"
            else
              "@#{key}='#{val}'"
            end
          }.join( ' and ' )
          out << "]"
        end
        out
      end
    
      # A hash to tag extra data onto a node during processing
      def _mydata
        @_mydata ||= {}
      end
    end
    
    start_time = Time.new
      @xmldoc = Document.new( IO.read( xml_file ), :ignore_whitespace_nodes => :all )
      @root = @xmldoc.root
      @root = @root.first if @root.is_a?( Array )
    end_time = Time.new
    puts "%.2fs to parse XML file (#{xml_file})" % ( end_time - start_time )
    
    require 'erb'
    File.open( output_file, 'w' ){ |o|
      start_time = Time.new
      output_code = ERB.new( IO.read( template_file ), nil, '>', 'output' ).result( binding )
      end_time = Time.new
      puts "%.2fs to run template   (#{template_file})" % ( end_time - start_time )
    
      start_time = Time.new
      o << output_code
    }
    end_time = Time.new
    puts "%.2fs to write output   (#{output_file})" % ( end_time - start_time )
    puts " "
    

    This can be used for HTML or automated source code generation alike.

    However, these days I would advocate using Haml and Nokogiri (if you want structured XML markup) or YAML (if you want simple-to-edit content), as these will make your markup cleaner and your template logic simpler.

    Edit: Here’s a simpler file that merges YAML with Haml. The last four lines do all the work:

    #!/usr/bin/env ruby
    require 'yaml'; require 'haml'; require 'trollop'
    EXTENSION = /\.[^.]+$/
    
    opts = Trollop.options do
      banner "Usage:\nyamlhaml [opts] <sourcefile.yaml>"
      opt :haml,   "The Haml file to use (default: sourcefile.haml)", type:String
      opt :output, "The file to create   (default: sourcefile.html)", type:String
    end
    opts[:source] = ARGV.shift
    Trollop.die "Please specify an input Yaml file" unless opts[:source]
    Trollop.die "Could not find #{opts[:source]}"   unless File.exist?(opts[:source])
    
    opts[:haml]   ||= opts[:source].sub( EXTENSION, '.haml' )
    opts[:output] ||= opts[:source].sub( EXTENSION, '.html' )
    Trollop.die "Could not find #{opts[:haml]}" unless File.exist?(opts[:haml])
    
    @data = YAML.load(IO.read(opts[:source]))
    File.open( opts[:output], 'w' ) do |output|
      output << Haml::Engine.new(IO.read(opts[:haml])).render(self)
    end
    

    Here’s a sample YAML file:

    title: Hello World
    main: "<h1>you have 30 files in games folder</h1>"
    side: "I dunno, something goes here."
    

    …and a sample Haml file:

    !!! 5
    %html
      %head
        %title= @data['title']
      %body
        #main= @data['main']
        #side= @data['side']
    

    …and finally the HTML they produce:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Hello World</title>
      </head>
      <body>
        <div id='main'><h1>you have 30 files in games folder</h1></div>
        <div id='side'>I dunno, something goes here.</div>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html file which has to be parsed using selenium. I need
I have a HTML file that has code similar to the following. <table> <tr>
I have an HTML page containing a flash file, I need to write code
Say i have a .html file that contains a web design and has variables
1: I have a .html file that has some markup with some placeholder tags.
I have a html file that has invoice details I would like to know
I have html-file. I have to replace all text between this: [%anytext%]. As I
I have a generated HTML file which has large blocks of text with span's
This is the beginning -- I have a file on disk which is HTML
I have a big HTML file that has lots of markup that looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.