Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Code executes twice

Open DeutscheGabanna opened this issue 2 years ago • 5 comments

Plugin informations (please complete the following information):

  • OS: [e.g. Windows 10] win10
  • Templater version: [e.g. 0.3.4] 1.9.9
  • Obsidian version: [e.g. 0.9.11] 0.12.15
  • Templater settings: [e.g. Template folder location: "Templates/", Timeout: "5" (add the templates not working with their associated command)]

Describe the bug Code executes twice.

Expected behavior Code executes once.

Additional context Code:

<%*
// =========================================================
// DATA COLLECTION
// =========================================================
// Crucial var inits
var name, team, position, location, head, category

/* - TYPE: picking feature or person sends you through a different  route than picking team */
const type = await tp.system.suggester(["feature", "team", "person"], ["feature", "team", "person"], false, "this note is about a:")

console.log("type:" + type + "\nfilename:" + tp.file.name)

// - PERSON? TELL ME MORE ABOUT THEIR POSITION
if (type == "person") {
	position = await tp.system.suggester(["PM", "TL", "deweloper", "director", "frontline", "Product Communication Specialist", "Communication Specialist"], ["PM", "TL", "deweloper", "director", "frontline", "Product Communication Specialist", "Communication Specialist"], false, "this person works as a:")
}
// - PERSON OR A TEAM? TELL ME MORE ABOUT THEIR ORG LOCATION
if (type != "feature") {
	location = await tp.system.suggester(["tech", "commerce", "CX"], ["tech", "commerce", "CX"], false, "this " + type + " works in:")
	if (type == "person") {
		if (position == "Communication Specialist") location = "CX"
		else if (position == "Product Communication Specialist") location = "tech"
	}
}
// - TEAM? TELL ME MORE ABOUT THEIR PM
if (type == "team") head = await tp.system.prompt(type + " is lead by:", "", false)

console.log("position:" + position + "\nlocation:" + location + "\nhead:" + head) 

/* - CATEGORY: just a plural version of the type used to move the file into an appropriate category later on */
if (type != "person") category = "/" + type + "s/"
else category = "/people/"

/* - NAME: If the file already has a non-default name then let this be its name */
if (tp.file.title != "Untitled" && tp.file.name !== undefined) name = tp.file.name
else name = await tp.system.prompt(type + " name:", "Untitled", false)

/* - TEAM: Team name will be different from people's and features' names but be identical with name if it's a team */
if (type == "team") team = name
else team = await tp.system.prompt("belongs to team:", "", false)

console.log("category:" + category + "\nname:" + name + "\nteam:" + team)

// =========================================================
// DATA PROCESSING
// =========================================================
/* Make possible to slugify the team name into a taggable string (from: https://stackoverflow.com/a/5782563) */
var slug = function(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.toLowerCase();

  // remove accents, swap ñ for n, etc
	var from = "ĄąĆćĘꣳŃńÓ󌜏źŻż·/_,:;";
	var to   = "aacceellnnoosszzzz------";
  for (var i = 0, l = from.length; i < l; i++) {
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
           .replace(/\s+/g, '-') // collapse whitespace and replace by -
           .replace(/-+/g, '-'); // collapse dashes

  return str;
};

console.log("slugged_team:" + slug(team))

// Move to an appropriate folder based on the type 
await tp.file.move(category + name)

// =========================================================
// START OF FRONTMATTER
// =========================================================
tR += "---\ntags: " + type
if (type == "team") tR += "/" + slug(team) + "\n"
else if (team != "" && team !== undefined) tR += ", team/" + slug(team) + "\n"
else tR += "\n"

tR += "type: " + type + "\naliases: \n"
if (type == "person") tR += "person_position: " + position + "\n"
else if (type == "team") tR += "team_head: " + head + "\nteam_communication: \n"

tR += "org_location: " + location + "\nteam_name: " + team + "\n---\n%%\n"

// =========================================================
// END OF FRONTMATTER
// CUSTOM LINKABLE FRONTMATTER
// =========================================================
if (type != "team") {
	tR += "\n_Team: "
	if (team != "") tR += "[[" + team + "]]"
}
tR += "_Related:\n%%"
// =========================================================
// END OF CUSTOM LINKABLE FRONTMATTER
// =========================================================
%>

---
# <% name.charAt(0).toUpperCase() + name.slice(1) %>

// I escape the three asterisks to format the code on github properly
\```dataview
TABLE WITHOUT ID file.link AS "People", person_position AS "Position"  
FROM "people"
WHERE (contains(team_name, this.file.name) OR contains(team_communication, this.file.name)) AND type = "person"
SORT file.link desc
\```

\```dataview
TABLE WITHOUT ID file.link as "Feature"
FROM "features"
WHERE contains(team_name, this.file.name) AND type = "feature"
SORT file.link desc
\```

DeutscheGabanna avatar Oct 13 '21 21:10 DeutscheGabanna

Started getting this as well. I have the following as a default blank page that runs when I create a new file in root:

<%*
  let title = tp.file.title
  var output;
  if (title.startsWith('📹 ')) {
    output = await tp.file.include("[[Input - Youtube]]")
    await tp.file.move("/Consumption/" + tp.file.title)
} else {
    output = await tp.file.include("[[Template - Blank Page 1]]")
  }%>
  <% tR +=`${output}` %>

It will create the file, move it and when I view it, it will have repeated the contents of the "Input - Youtube" template twice.

The original template above is much larger, there are about 20 or so file prefixes that it checks for, however, the behaviour exists for the original as well as several revisions where I removed all but two choices of templates.

I thought that a recent update would have caused this, but I downloaded the 1.8.0 release, installed it manually, and it still exhibits the behaviour.

Plugin conflict? I have also removed all other community plugins and the behaviour is still there. I have also shutoff the Core Plugins, and the behaviour is still there.

theodeyle avatar Oct 20 '21 19:10 theodeyle

is this still an issue?

shabegom avatar Jan 13 '22 15:01 shabegom

Yes, this still occurs.

OS: Windows 11, I upgraded from 10 in December. Templater version: [e.g. 0.3.4] 1.11.3 Obsidian version: [e.g. 0.9.11] 0.13.31 Templater settings:

  • Trigger Templater on New File Creation = Enabled
  • Folder templates = Enabled
    • Add new "/" -> Templates/Template - Blank Page.md
    • Add new "Work/Tasks" ->
  • Startup Templates = Templates/Template - Task.md
  • Script Files = None

theodeyle avatar Mar 19 '22 18:03 theodeyle

At least in your template that you pasted the issue is that you aren't using the Execution Command format: <%* command %>

So your template:

  <% tR +=`${output}` %>

This should either use tR with the execution command syntax:

  <%* tR +=`${output}` %>

Or just return the output from the normal syntax:

  <% output %>

Maybe it doesn't make sense to expose tR (the templater result) when using the normal syntax to avoid this issue.

liamcain avatar Mar 19 '22 19:03 liamcain

Thank you!!! I used the latter suggestion with an "_" to trim preceding whitespace and it worked for me.

Unsure of the original creator of this issue though.

theodeyle avatar Mar 24 '22 12:03 theodeyle