Supress Newlines in custom export?

I’m exporting using a template similar to the following example. I get a newline even if the field does not exist. Is there a way to suppress the blank lines in the exported text?

Export Template:

[<$view field=title/>]
id=<$view field=id/>
desc=<$view field=desc/>
<%if [<currentTiddler>has:field[exit-north]] %>north={{!!exit-north}}<%endif%>
<%if [<currentTiddler>has:field[exit-south]] %>south={{!!exit-south}}<%endif%>
<%if [<currentTiddler>has:field[exit-east]] %>east={{!!exit-east}}<%endif%>
<%if [<currentTiddler>has:field[exit-west]] %>west={{!!exit-west}}<%endif%>

Export Text:

[1]
id=center-room
desc=Center Room
north=2
south=3
east=4
west=5
[2]
id=north-room
desc=North Room

south=1


[3]
id=south-room
desc=South Room
north=1



[4]
id=east-room
desc=East Room



west=1
[5]
id=west-room
desc=West Room


east=1

There are better ways to write this, but for now the unwanted lines are coming from the end of the if statement lines however if you remove them you will have to add a line feed back when a value is available eg <br>

You could ensure that the newlines in your text come only after your content, and not between the <% if %> .. <% endif %> blocks. Or, as @TW_Tones says, you could use <br> tags (in conjunction with a \whitespace trim pragma.

These might look like this:

title: RoomTemplate with specific whitespace
tags: $:/tags/ViewTemplate

<% if [<currentTiddler>tag[Room]] %>
<pre>[<$view field=title/>]
id=<$view field=id/>
desc=<$view field=desc/><%if [<currentTiddler>has:field[exit-north]] %>
north={{!!exit-north}}<%endif%><%if [<currentTiddler>has:field[exit-south]] %>
south={{!!exit-south}}<%endif%><%if [<currentTiddler>has:field[exit-east]] %>
east={{!!exit-east}}<%endif%><%if [<currentTiddler>has:field[exit-west]] %>
west={{!!exit-west}}<%endif%></pre>
<% endif %>

or

title: RoomTemplate with trim and <br>
tags: $:/tags/ViewTemplate

\whitespace trim

<% if [<currentTiddler>tag[Room]] %>
<pre>[<$view field=title/>]<br/>
id=<$view field=id/><br/>
desc=<$view field=desc/><br/>
<%if [<currentTiddler>has:field[exit-north]] %>north={{!!exit-north}}<br/><%endif%>
<%if [<currentTiddler>has:field[exit-south]] %>south={{!!exit-south}}<br/><%endif%>
<%if [<currentTiddler>has:field[exit-east]] %>east={{!!exit-east}}<br/><%endif%>
<%if [<currentTiddler>has:field[exit-west]] %>west={{!!exit-west}}<br/><%endif%>
</pre>
<% endif %>

I use <pre> tags here to simulate the environment you would generate with a text import, but still be able to simple view them inline, like this:

RoomTemplates.json (2.6 KB)

The tiddlers 1, 2, 3, 4, and 5 each get the original template, the whitespace shifting one, and the <br> one.

Thanks guys - it’s not a viewtemplate. It’s a template used in a custom exporter to a text file, which gets downloaded by the browser. It’s all initiated using a button and the tm-download-file message. Sorry I wasn’t clear.

I want to manage rooms and items for loading into a game engine. Although my game parser can handle the spaces, I would really prefer the only newlines to be between records.

Primeval demo here.

While I cannot test on my mobile device, I would expect the same principles to apply.

Oh I think I get it - push all those lines together, and add the newline in the inside the if/endif. Yep, that works. Thanks.

2 Likes