Beginner here - unwanted empty line in template

What I’m trying to do

Hey everyone,

I’m still very much of a noob and trying to find my way around everything, but I am happy with a daily notes template I built using advice from here that adds a kanji depending on the work days. It works very well, but it does create an empty line at the very top that I’m unable to get rid of. I’m a bit stumped what to do and was hoping someone would have an idea so I don’t have to manually get rid of it every day.

Here’s my template:

<%* if (tp.date.now("dddd") == "Saturday") {%>  
# **土** <% tp.date.now("YYYY-MM-DD dddd") %>
<%*} else if (tp.date.now("dddd") == "Sunday") {%>  
# **日** <% tp.date.now("YYYY-MM-DD dddd") %>
<%*} 
if (tp.date.now("dddd") == "Monday") {%>  
# **月** <% tp.date.now("YYYY-MM-DD dddd") %>
<%*} 
if (tp.date.now("dddd") == "Tuesday") {%>  
# **火** <% tp.date.now("YYYY-MM-DD dddd") %>
<%*} 
if (tp.date.now("dddd") == "Wednesday") {%>  
# **水** <% tp.date.now("YYYY-MM-DD dddd") %> 
<%*}
if (tp.date.now("dddd") == "Thursday") {%>  
# **木** <% tp.date.now("YYYY-MM-DD dddd") %> 
<%*} 
if (tp.date.now("dddd") == "Friday") {%>  
# **金** <% tp.date.now("YYYY-MM-DD dddd") %> 
<%*} %>
<< **先** [[<% tp.date.now("YYYY-MM-DD dddd", -1) %>]] | **月** [[<% tp.date.now("YYYY-MM MMMM") %>]] **月** | [[<% tp.date.now("YYYY-MM-DD dddd", 1) %>]] **翌** >>

- 

Things I have tried

This is the code I use. I don’t understand why it creates an empty line since it just starts with an if clause that then calls to insert a header, but if I remove the line break, the header doesn’t get formatted as a header when the template is applied, so I’m guessing it’s necessary.

Any advice?

You would probably need to look at white space control within Templater :blush: :

https://silentvoid13.github.io/Templater/commands/whitespace-control.html

… as this is probably where the unwanted blank line is coming from.

I never write templates in this way but the following doesn’t seem to produce an unwanted blank line before the header :blush: :

<%-* if (tp.date.now("dddd") == "Saturday") { %>
# **土** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else if (tp.date.now("dddd") == "Sunday") { -%>
# **日** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else if (tp.date.now("dddd") == "Monday") { -%>
# **月** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else if (tp.date.now("dddd") == "Tuesday") { -%>
# **火** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else if (tp.date.now("dddd") == "Wednesday") { -%>
# **水** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else if (tp.date.now("dddd") == "Thursday") { -%>
# **木** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } else { -%>
# **金** <% tp.date.now("YYYY-MM-DD dddd") %>
<%-* } %>
<< **先** [[<% tp.date.now("YYYY-MM-DD dddd", -1) %>]] | **月** [[<% tp.date.now("YYYY-MM MMMM") %>]] **月** | [[<% tp.date.now("YYYY-MM-DD dddd", 1) %>]] **翌** >>

-
1 Like

You are an absolute, Gilgeam-certified GENIUS! No way I’d have figured that out. Thank you so much! You rock!

1 Like

As @Pch said it’s matter of white space handling, but I would also like to show a different approach to the same which I tend to use in similar cases.

<%* 
const dayAlternatives = [
  "日",
  "月",
  "火",
  "水",
  "木",
  "金",
  "土",
  ]

const dayText = "**" + dayAlternatives[tp.date.now("d")] + "** " + tp.date.now("YYYY-MM-DD dddd")

_%>
# <% dayText %>

<< **先** [[<% tp.date.now("YYYY-MM-DD dddd", -1) %>]] | **月** [[<% tp.date.now("YYYY-MM MMMM") %>]] **月** | [[<% tp.date.now("YYYY-MM-DD dddd", 1) %>]] **翌** >>

The trick in this variant is that we declare the different characters(?)/texts for the various days in a table, and then do one lookup into that table depending on which day of week it is, and use that to build up a intermediate text, dayText, for later usage.

I believe this gives a cleaner solution, where it’s easier to see what your template actually produces. It also simplifies the logic, and reduce on the complexitiy and number of calls to tp.date.now().

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.