Templater Whitespace

What I’m trying to do

I’m trying to insert a template and everything is ok but templater creates whitespaces that break the yaml frontmatter at the beginning

Things I have tried

I’ve tried adding the “-” or “_” to the template but no luck

Here’s the code I modified from @Komma:

<%* let filetype = await tp.system.suggester(["!Generic","Zettel", "Hub", "Hub Autore", "Hub Opere"], ["!Generic","Zettel", "Hub", "Hub Autore", "Hub Opere"], false, "Which template do you want to use?") -%> 
<%-* if (filetype === "Zettel") { %>  <% tp.file.include("[[Zettel Template (new note suggester template)]]") %> 
<% tp.file.move("/30 - SlipBox/" + tp.file.title) %>
<%-* } else if (filetype === "Hub Autore") { %> 
<% tp.file.include("[[Hub Template (Author)]]") %> <% tp.file.move("/40 - Hubs/Autori/" + tp.file.title) %> 
<%-* } else if (filetype === "Hub Opere") { %> 
<% tp.file.include("[[Hub Template (Opere)]]") %> <% tp.file.move("/40 - Hubs/Opere/" + tp.file.title) %>
<%-* } else if (filetype === "Hub") { %>
<% tp.file.include("[[Hub Template]]") %>
<% tp.file.move("/40 - Hubs/" + tp.file.title) %>
<%-* } else if (filetype === "!Generic") { %> 
<% tp.file.include("[[New General Note template]]") %> 
<%-* } else { %> 

What I’m doing now is create my note and remove manually but it’s not that good!
Dunno if it’s a useful information but the extra space I have to remove are: one line and one space (I mean I have to press delete to times: the first takes me one line up, the second remove one space)
Any help appreciated!

You could try this:

Seems you’ve tried these, but just to doublecheck:

A specific syntax exists for whitespace control:

  • An underscore _ at the beginning of a tag (<%_) will trim all > > whitespace before the command
  • An underscore _ at the end of a tag (_%>) will trim all >whitespace after the command
  • A dash - at the beginning of a tag (<%-) will trim one newline >before the command
  • A dash - at the end of a tag (-%>) will trim one newline after >the command.

https://silentvoid13.github.io/Templater/commands/whitespace-control.html?highlight=whitespace#whitespace-control

Just add - or _ to all starting Templater tags and you remove all the whitespace.

An alternative approach is:

---
# TEMPLATE YAML
---

<%_* tR = ""
let filetype = await tp.system.suggester(["!Generic","Zettel", "Hub", "Hub Autore", "Hub Opere"], ["!Generic","Zettel", "Hub", "Hub Autore", "Hub Opere"], false, "Which template do you want to use?")

if (filetype === "Zettel") {
    tp.file.include("[[Zettel Template (new note suggester template)]]")
    tp.file.move("/30 - SlipBox/" + tp.file.title)
} else if (filetype === "Hub Autore") {
    tp.file.include("[[Hub Template (Author)]]")
    tp.file.move("/40 - Hubs/Autori/" + tp.file.title)
} else if (filetype === "Hub Opere") {
    tp.file.include("[[Hub Template (Opere)]]")
    tp.file.move("/40 - Hubs/Opere/" + tp.file.title)
} else if (filetype === "Hub") {
    tp.file.include("[[Hub Template]]")
    tp.file.move("/40 - Hubs/" + tp.file.title)
else if (filetype === "!Generic") {
    tp.file.include("[[New General Note template]]")
} else {
_%>

The tR = "" clears the “template frontmatter”.

2 Likes

Thanks @stef.rausch, it worked by adding underscores to every Templater tag, I guess I had done a lot of attempts but not this :face_with_open_eyes_and_hand_over_mouth:

BTW: in one case I’d like that the template inserted by tp.file.include could 1) prompt for a name, 2) get the clipboard content (<%tp.system.clipboard ()%>) and 3) get the link of the note from were I’m copying to clipboard.
I could manage 2) but don’t know how to get points 1 and 3, any idea?
How can I put at the beginning a prompt to
The line that I can’t understand how to make it works is Source:[[<% tp.config.active_file% >]](below)
The template for that note is:

\\?? Here should go the code for 
\\<% tp.system.prompt("Title: (Id - Description)") and to set the name of the note?
---
aliases: 
tags:
title: <% tp.file.title %>
type: zettel
created: 
modified:
status: pending
priority:

---

See also: [[]]
HUB: [[]] 
Source:[[<% tp.config.active_file %>]]
Commento a: [[]]


---
<%tp.system.clipboard ()%>

---

THX!

  1. To rename the file see tp.file - Templater (silentvoid13.github.io)

    <%_*
    const newName = await tp.system.prompt(YOUR_TEXT)
    
    if (newName !== null) {
        tp.file.rename(newName)
    }
    _%>
    

    In the frontmatter:

    title: <% newName %>
    

Regarding point 3, I will get back to you later.

1 Like

Wow, thanks a lot @stef.rausch,
but unfortunately I’m at the very beginning with templater, I studied the documentation but I can only use very basic commands…I copied your code at the very beginning (and put the corresponding templater tag in the title field) of the note template but I get a parsing error…the note is created, moved to the right folder but it’s empty and untitled (no prompt appears)

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