(YAML/Templater & Templates) Problem for loop

!!UNSOLVED!!

The plugins used:
Templater & templates.

I am trying to do some YAML formatting but it giving me the error:
template parsing error, aborting, etc. (There is no error in the console.)

Code:
There is probably very much wrong with this. But I for the life of me can’t see it.
---
title: <% $title = for (let $i = 0; $i < tp.file.title.split(" - ").length; $i++) {if($i >= 2){$title + tp.file.title.split(" - ")[$i];}}; if($title = null){$title = tp.file.title;} else{$title;} %>
---

Please help me fix this.

I am assuming you’re dealing with note name like 2023-01-20 - Some Topic - Some Subtopic where you want to define the title as the text that follows the date. I’m also assuming that not all notes follow this style, and in those cases the title is just the note’s name. If those assumptions are correct, I’d put the Templater template together as:

<%*
let title = tp.file.title;
const parts = title.split(' - ');
if (parts.length >= 2)
{
   title = parts.slice(1).join(' - ');
}
-%>
---
title: <% title %>
---

Also, if you’re looking for the problem in your code, it most likely stems from $title = for . This is trying to assign a variable to a for loop, which is not permitted.

Aaah simply said javascript loops above the
---

I might be doing it wrong. This makes it so that the metadata isn’t metadata.

2 ways I tried


<%*
let title = tp.file.title;
const parts = title.split(' - ');
if (parts.length >= 2)
{
   title = parts.slice(1).join(' - ');
}
---
title: title
---
-%>
<%*
let title = tp.file.title;
const parts = title.split(' - ');
if (parts.length >= 2)
{
   title = parts.slice(1).join(' - ');
}
-%>
---
title: <% title %>
---

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