简体   繁体   English

如何制作新行或在数组中中断?

[英]How can I make new line or break in array?

I have a popover and I want to have break between each items in content- data. 我有一个弹出窗口,我想在内容数据中的每个项目之间切换。

 %i.fa.fa-folder.item{"data-content" => "#{folder.doc.map{ |file_id| File.find(file_id).name}.join{"<br />"}.html_safe}",rel: "popover", title: "title:"}

I have added "\\n" to the the join but its not working. 我已将“ \\ n”添加到联接中,但无法正常工作。 Do you have any idea where should I put the "\\n" in the code? 您是否知道我应该在代码的哪里放置“ \\ n”?

\\n is not rendered as a newline in HTML. \\n不会在HTML中显示为换行符。 Use <br /> tag instead: 使用<br />标签代替:

 #{folder.doc.map{ |file_id| File.find(file_id).name}.join("<br />").html_safe}",rel: "popover", title: "title: 

If this is being displayed in a browser then a \\n will only put a newline character in the code . 如果在浏览器中显示该字符\\n只会在代码中放入换行符。 The HTML newline tag is <br /> . HTML换行标记<br /> However, without knowing more about the general context you are using this in it is hard to give you good advice. 但是,如果不了解您所使用的一般上下文,那么很难为您提供良好的建议。

Normally if I am making a list of things then I would wrap the whole list in, say, a <ul> tag, and each list item in an <li> ... Or possibly each one should be wrapped in a <div> . 通常,如果我要列出事物,那么我会将整个列表包装在一个<ul>标记中,并将每个列表项目包装在一个<div> <li> ……或者可能每个包装在一个<div>

The thing about HTML, like most programming languages, is that there are many ways to do an arbitrary thing, but there are better ones or worse ones depending on the bigger picture 与大多数编程语言一样,关于HTML的事情是有很多方法可以执行任意操作,但是取决于总体情况,有更好的方法也可以有更差的方法

Adding a newline will not work, as HTML will ignore any whitespace/newlines to best format string as contiguous as possible. 添加换行符将不起作用,因为HTML将忽略任何空格/换行符,以使最佳格式的字符串尽可能连续。 So you have to use <br/> to add an explicit line-break in HTML. 因此,您必须使用<br/>在HTML中添加显式的换行符。 However, now, in rails for safety any string will be html-escaped unless you explicitly instruct rails to not do that. 但是,现在,为了安全起见, 除非您明确指示Rails 要这样做, 否则为了安全起见,任何字符串都将被html转义。

So in your case you want to build the list of files, claim it is safe, and then set as content. 因此,在您的情况下,您想要构建文件列表,声称它是安全的,然后将其设置为内容。 Something like 就像是

- file_list = folder.doc.map{|file_id| File.find(file_id).name}.join('<br/>').html_safe
%i.fa.fa-folder.item{data: {content: file_list, html: "true", rel: "popover", title: "title:"}

Secondly, because you are using bootstrap to render the popover, you will also have to instruct the bootstrap popover that the data-content is interpreted as html and should not be escaped (by setting the data-html attribute). 其次,因为您正在使用引导程序来渲染弹出窗口,所以您还必须指示引导程序弹出窗口将data-content解释为html,并且不应对其进行转义(通过设置data-html属性)。

For that case I would recommend using a helper 对于这种情况,我建议使用一个助手

Helper 帮手

module MyHelper
  def render_data_content(folder)
    folder.doc.map do |file_id| 
      File.find(file_id).name
    end.join('<br/>').html_safe
  end
end

View 视图

%i.fa.fa-folder.item{"data-content" => render_data_content(folder) ,rel: "popover", title: "title:"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM