Question

suppose in your controller, you had something like this:

@out = `git log --name-status -n1 ${@hash}`

(which returns a multi-line output to the terminal)

And you wanted to show it in a view like so:

<%= content_tag(:div, @out.html_safe, class: "well") %>

How do you get that output with preserved whitespace?

Was it helpful?

Solution

Either use the good old <pre> tag or, in your CSS, apply white-space: pre; or white-space: pre-wrap; to the surrounding element. Personally I think <pre> is preferable, since it's semantic.

P.S. I would avoid using html_safe in this instance, as the unescaped output from your shell command could easily break your markup.

OTHER TIPS

Typically a <pre> tag is used to markup pre-formatted text:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top