Import csv to html table

Hello,

for a project I must convert csv files to html table. Is there a way I can do this with middleman? At moment I use jquery addon but when work with middleman goes maybe without jquery.

This were a start but it were awesome if can use middleman to export all *csv to html table and I can linked in the menus to the file.

Thanks for help
Silvio

Hello,

with middleman v4 it seem not want work the extension.

The output:
NameError: undefined local variable or method `data_dir’ for #<#Class:0x000000035fb768:0x0000000327afd0>

In config.rb is data folder set with standard configuration:
set :data_dir, ‘data’

Have someone a idea?

Thank you / Nice day
Silvio

You might want to try the following “tutorial”: http://ict4g.net/adolfo/notes/2015/05/30/csv_data_in_middleman.html.

It works with Middleman 4.

I hope it helps!

  • adolfo
1 Like

Hi,

that I have try but I ever become message

undefined local variable or method `data_dir' for #<#<Class:0x000000028010e0>:0x00000004687b40>

The complete error message can see > http://pastebin.com/ZunZftdC

Silvio

Hi,
sorry for taking so long to get respond.

I have checked and, of course, you are right. To make it work in Middleman 4 data_dir in the function csv_data must be replaced by app.config[:data_dir].

The correct code, therefore, is:

class CSV_Helpers < Middleman::Extension
  def initialize(app, options_hash={}, &block)
    super
  end
  helpers do
    def csv_data(file)
      csv_data = File.read(File.join(data_dir, file))
      hash = CSV.new(csv_data, :headers => true, :header_converters => :symbol)
      return hash.to_a.map { |row| row.to_hash }
    end
  end
end
::Middleman::Extensions.register(:csv_helpers, CSV_Helpers)
1 Like