Alas, white-space:pre;
and piping can only preserve the formatting if there are no nested tags (i.e. your span
s). To get the output to print exactly as you might like with piping and inline tags, you have to get a little hacky and use some manner of escaped character string interpolation (#{}
) that renders inline JavaScript.
The basic interpolation expression you'll need somewhere is #{'\n\t\t'}
to ensure it lines up with the tag. As with regular JavaScript, \n
will return the string to the beginning of the next line, hence the need for \t
s.
There are a variety of ways you can implement this interpolation, however.
Inline
p(style='white-space:pre;')
|#{'\n\t\t'} One: #[span.A]
|#{'\n\t\t'} Two: #[span.B]
|#{'\n\t\t'} Three: #[span.C]
|#{'\n\t\t'} Four: #[span.D]
|#{'\n\t\t'} blah blah blah
In your example, all of the colons prefacing the span
are indented 8 characters, so you can line them up using some simple math and .repeat
without having to figure out the particulars of how many spaces to have.
Function
Note the -
in front of the function, which is necessary to run a line of JavaScript.
-function whiteSpaceFunc(str) { return '\n\t' + ' '.repeat(8-str.length) + str + ': ';}
p(style='white-space:pre;')
|#{whiteSpaceFunc('One')}#[span.A]
|#{whiteSpaceFunc('Two')}#[span.B]
|#{whiteSpaceFunc('Three')}#[span.C]
|#{whiteSpaceFunc('Four')}#[span.D]
|#{'\n\t\t'} blah blah blah
Mixin
A more native approach of functions. Note the use of className
rather than class
as a parameter; class
seems to be reserved and throws an error.
p(style='white-space:pre;')
mixin whiteSpacedLine(str,className)
| #{'\n\t\t' + ' '.repeat(8-str.length)}#{str}: #[span(class=className)]
+whiteSpaceLine('One','A')
+whiteSpaceLine('Two','B')
+whiteSpaceLine('Three','C')
+whiteSpaceLine('Four','D')
Mixin + Iteration
Perhaps the most simple way to do this. If you spread the vals
array across multiple lines as in the code below, make sure to wrap them inside a code block indented under a -
, and ensure the -
does not have any space afterward. (You could, of course, contain it to one line.)
p(style='white-space:pre;')
mixin whiteSpacedLine(str,className)
| #{'\n\t\t' + ' '.repeat(8-str.length)}#{str}: #[span(class=className)]
-
var vals = [
{char: 'A', num: 'One'},
{char: 'B', num: 'Two'},
{char: 'C', num: 'Three'},
{char: 'D', num: 'Four'}
];
- each item in vals
+whiteSpacedLine(item.num,item.char)
|#{'\n\t\t'} blah blah blah