我正在使用这个主题 http://twbs.github.io/bootstrap/examples/justified-nav/
我想在 jade View 中实例化我的集合的文档并(稍后创建分页),但我的问题是关于有一个 HTML 或 Jade 示例只是为了弄清楚进一步的过程。
这是我要循环的 div:
<div class="row">
<div class="col-lg-4">
<h2>Safari bug warning!</h2>
<p class="text-danger">As of v7.0.1, Safari exhibits a bug in which resizing your browser horizontally causes rendering errors in the justified nav that are cleared upon refreshing.</p>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-primary" href="#" role="button">View details »</a></p>
</div>
<div class="col-lg-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-primary" href="#" role="button">View details »</a></p>
</div>
<div class="col-lg-4">
<h2>Heading</h2>
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa.</p>
<p><a class="btn btn-primary" href="#" role="button">View details »</a></p>
</div>
</div>
假设我的集合中只有 12 个文档,我如何遍历我的集合来创建
4 <div class="row">和每一个<div class="row">有 3 <div class="col-lg-4">和
每个<div class="col-lg-4">有一个:
<h2>= product.title
<p> = product.imgurl
<p> = product.description
<p> = prodcuct.url
我知道如何路由、访问数据库、在 jade 引擎上显示...我做了简单的测试 一切顺利,但要进入下一个阶段,我会很感激一些解释或一个小例子。
我下定决心:
each product, i in products
div
div
h1 = product.title
p = product.imgurl
p = product.description
p = prodcuct.url
最佳答案
我只是根据您提供的信息做了一个小的、快速的、粗略的例子,我希望它能帮助您完成最后几个步骤以熟悉 Jade 和 co。
首先是你的集合,只是一个小例子(只有 8 个例子):
{
products: [{title: 'xyz', imgurl: 'http://www.example.com/img.jpeg',
description: 'more text',
url: 'http://www.example.com/'},
{title: 'abc', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/another'},
{title: 'fgh', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/another'},
{title: 'tgb', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/another'},
{title: 'qwe', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/anotherz'},
{title: 'asd', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/anothery'},
{title: 'hjk', imgurl: 'http://www.example.com/anotherimg.jpeg',
description: 'a lot more text',
url: 'http://www.example.com/anotherx'}]
}
这是你通过的res.render在您的 Node 应用程序中。
接下来我们有您的 Jade 标记,您的示例并没有错,我在这里只更改了几处:
- for (var i = 0; i < products.length; i += 4)
div.row
- for (var j = i; j < 3+i; j++)
div(class="col-lg-4")
h1= products[j].title
p= products[j].imgurl
p= products[j].description
p= products[j].url
如你所见,我把你的 <div class="row">在两个循环之间(我在这里使用 for 循环,如果你以其他方式传递数据会更容易。我想说的是:这个例子是不故障安全的,例如,如果你不有 3 列的数据你会得到一个像 Cannot read property 'title' of undefined 这样的错误 - 所以在这里你必须设置异常(exception)),所以我们每 3 次迭代得到的数据不会超过一个,你的例子会产生一个 div每次迭代。
一个类可以通过一个点来添加。例如div.yourClass或者只用 div(class="yourClass" style="..." whatever="...") .
接下来我们有循环,在给定 <div class="col-lg-4 "> 之后为您的收集数据。您的数据可以通过 tag= var 显示或 tag #{var} .
其余的应该很容易猜到。
这将产生以下 HTML 代码:
<div class="row">
<div class="col-lg-4">
<h1>xyz</h1>
<p>http://www.example.com/img.jpeg</p>
<p>more text</p>
<p>http://www.example.com/</p>
</div>
<div class="col-lg-4">
<h1>abc</h1>
<p>http://www.example.com/anotherimg.jpeg</p>
<p>a lot more text</p>
<p>http://www.example.com/another</p>
</div>
<div class="col-lg-4">
<h1>fgh</h1>
<p>http://www.example.com/anotherimg.jpeg</p>
<p>a lot more text</p>
<p>http://www.example.com/another</p>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<h1>qwe</h1>
<p>http://www.example.com/anotherimg.jpeg</p>
<p>a lot more text</p>
<p>http://www.example.com/anotherz</p>
</div>
<div class="col-lg-4">
<h1>asd</h1>
<p>http://www.example.com/anotherimg.jpeg</p>
<p>a lot more text</p>
<p>http://www.example.com/anothery</p>
</div>
<div class="col-lg-4">
<h1>hjk</h1>
<p>http://www.example.com/anotherimg.jpeg</p>
<p>a lot more text</p>
<p>http://www.example.com/anotherx</p>
</div>
</div>
你看到 <p>链接标签在这里没有多大意义,但您可以轻松地将它们更改为 <a>标记,它始终是相同的程序。
重要的是,在您的 Jade 标记中,缩进是正确的,移动的所有内容都在开始标记和结束标记之间。
所以循环在 div 行中,数据在你的列中。
关于node.js - Jade 循环通过 db 构建 <div>,每个使用 bootstrap 有 3 个内容 block ?提供的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21998925/
在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi
我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec
我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e
我从用户Hirolau那里找到了这段代码:defsum_to_n?(a,n)a.combination(2).find{|x,y|x+y==n}enda=[1,2,3,4,5]sum_to_n?(a,9)#=>[4,5]sum_to_n?(a,11)#=>nil我如何知道何时可以将两个参数发送到预定义方法(如find)?我不清楚,因为有时它不起作用。这是重新定义的东西吗? 最佳答案 如果您查看Enumerable#find的文档,您会发现它只接受一个block参数。您可以将它发送两次的原因是因为Ruby可以方便地让您根据它的“并行赋
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption