SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.33k stars 199 forks source link

front matter formating number as a string #1376

Closed Birbs-world closed 6 months ago

Birbs-world commented 6 months ago

first of all this plugin is a life saver hahaha Describe the bug Unless i'm not doing something correctly so it looks like templater is not treating raw numbers in the tp.system.prompt("cost") command as actual integers but as a string. this is causing an issue where another plugin (DBFolder) doesnt see it as an intiger in one of its formulas because in the frontmatter its showing as

---
CostUnit: "23.50"
---

instead of

---
CostUnit: 23.50
---

and i confirmed that it is to do with templater not sure if the quotations are being added by the tp.system.promp or at the frontmatter["CostUnit"] = vCostUnit; its the same across all the fields that are ment to be intigers

My code:

<%*
const fileName = await tp.system.prompt("MPM")
const vPart = await tp.system.prompt("Part")
const vsource = await tp.system.suggester(["Jaycar","Digi-key", "Rs Components", "Bulk Buy"], ["Jaycar","Digi-key", "Rs Components", "Bulk Buy"])
const vCostUnit = await tp.system.prompt("cost")
const vGST = await tp.system.suggester((item) => item, ["GST ext", "GST inc"])
const vPieceUnit = await tp.system.prompt("pieces per unit")
const vQuantity = await tp.system.prompt("Quantity")
const vSourcePN = await tp.system.prompt("Source sku")
const vurl = await tp.system.prompt("url")
const vcat = await tp.system.suggester((item) => item, ["resistor", "capacitor", "diode", "transistor", "IC", "Other"])
const vStatus = await tp.system.suggester((item) => item, ["Current", "Obsolete"])
const vreplacement = await tp.system.prompt("Replacement link")
const templateName = "Item stock template"

await tp.file.rename(fileName)
tp.hooks.on_all_templates_executed(async () => {
  const file = tp.file.find_tfile(tp.file.path(true));
  await app.fileManager.processFrontMatter(file, (frontmatter) => {
    frontmatter["Part"] = vPart;
    frontmatter["source"] = vsource;
    frontmatter["CostUnit"] = vCostUnit;
    frontmatter["GST"] = vGST;
    frontmatter["PieceUnit"] = vPieceUnit;
    frontmatter["Quantity"] = vQuantity;
    frontmatter["SourcePN"] = vSourcePN;
    frontmatter["url"] = vurl;
    frontmatter["catigory"] = vcat;
    frontmatter["Status"] = vStatus;
    frontmatter["Replacement"] = vreplacement
  });
  tp.file.move("permanent notes/Stock/" + fileName)
}); 
%>

just incase you want to know the template is being called by a meta bind button with the folowing code


label: Add
icon: ""
hidden: false
class: ""
tooltip: Add new Item to stock
id: AddStock
style: primary
actions:
  - type: templaterCreateNote
    templateFile: Templates/Addstock.md
    openNote: false
Zachatoo commented 6 months ago

This is expected behavior, tp.system.prompt will always return a string. If you want to coerce it into a number you can do so.

<%*
const vCostUnit = await tp.system.prompt("cost")

tp.hooks.on_all_templates_executed(async () => {
  const file = tp.file.find_tfile(tp.file.path(true));
  await app.fileManager.processFrontMatter(file, (frontmatter) => {
    frontmatter["CostUnit"] = Number(vCostUnit); // Parse string as number
  });
}); 
%>