0
我有一个文本文件位于我的项目目录中,其中包含站点链接以及徽标的位置。例如:在ruby中循环散列文件
img1 = {name => "logo1", link => "http://example.com", imgpath => "img/logo1.png"}
img2 = {name => "logo2", link => "http://foobar.com", imgpath => "img/foobar.png"}
img3 = {name => "logo3", link => "http://foobar.com", imgpath => "img/foobar2.png"}
我想要做的是循环浏览该文本文件以显示每个图像及其链接。我已经搜遍了,我似乎无法找到一个很好的例子来说明如何去做。这里的for循环,我在我的网页这么远:
- File.open("img/logos.txt").readlines.each do |name,link,imgpath|
.col-sm-6.col-md-4.col-lg-3
.item-work
.hover
%img{:alt => "Image", :src => "img/#{imgpath}"}/
%a.lightbox-image{:href => "#{link}", :title => "Image"}/
.overlay
.info
%a{:href => "#{name}"} #{name}
我有点接近,页面加载的所有标志,除了图像不存在,并链接与它们相关联。我知道这是因为我没有在我的%img
链接和%a
标签中正确调用它们,但我似乎无法弄清楚。你会如何建议去做这件事?
readlines将把这些行中的每一行变成一个字符串。你是否可以控制'img/logos.txt'文件,比如数据如何存储在那里? – Anthony 2014-12-13 20:00:01
是的,以不同的方式存储它有意义吗? – niharvey 2014-12-13 20:01:45