翻译自官方文档 https://blacksmithgu.github.io/obsidian-dataview/
如有不同之处,请以官方文档为主。
这是个模板语言,能让你在笔记中插入变量和函数。还能让你执行js代码操作这些变量和函数。
这是个例子
1 | --- |
它会产生以下结果:
1 | --- |
可以看出,尖括号中的内容都被替换了(有点像jsp)
不再赘述。
和结束标签包裹的语句我们可以使用两种函数
例子:
1 | Yesterday: 2022-02-07 |
Templater 的所有函数都是使用命令调用的 JavaScript 对象。
命令须一个开始标签和一个结束标签,比如,使用tp.date.now内部函数:2022-02-08
所有的函数,无论用户函数还是内置函数,都在且只在 tp 对象下可用,因此调用方式为 tp.xxx....
通过设置 Template folder location 来告诉templater从哪里找模板
可以为新文件创建模板,也能为某一个特定的文件夹设置模板,这样一来,在指定文件夹下面创建新文件的时候就不用手动指定模板了。
可以和 Daily note、Calendar、Review、Note refactor 等兼容
调用方式 <% tp.<module_name>.<internal_function_name> %>
接下来介绍各种模块以及函数的用法。
本模板让我们可以直接使用moment.js 的moment对象,以及其所有的函数,
官方文档 https://momentjs.com/docs/#/displaying/
当前日期 tp.date.nowtp.date.now(format: string = "YYYY-MM-DD", offset?: number⎮string, reference?: string, reference_format?: string)
下一天的日期 tp.date.tomorowtp.date.tomorrow(format: string = "YYYY-MM-DD")
带有周几标记的tp.date.weekday(format: string = "YYYY-MM-DD", weekday: number, reference?: string, reference_format?: string)
上一天的时间 tp.date.yesterdaytp.date.yesterday(format: string = "YYYY-MM-DD")
format: 日期的格式,参考这里 format referenceoffset: 日期的偏移,-7代表上周 reference: 日期引用, 比如把本文件的标题设置为本函数的返回值reference_format: 日期引用的格式这里调用file的title接口有报错了,先跳过。没有列举在下面
1 | Date now: <% tp.date.now() %> |
生成之后是这样:
1 | Date now: 2022-01-25 |
tp.file.content 文件的内容
tp.file.create_new(template: TFile | string, filename?: string, open_new: boolean = false, folder?: TFolder) 用特定的模板和特定的内容创建新文件
filename: 新文件的文件名, 默认是 “Untitled”.folder: 放置新文件的路径,默认是obsidian的默认路径open_new: 是不是打开新文件。如果是,以后所有命令都会执行在新文件中template: 新文件模板tp.file.creation_date(format: string = "YYYY-MM-DD HH:mm") 返回文件创建日期
cursur 相关的没有写,以后可以补充
tp.file.exists(filename: string) 文件是否存在
tp.file.folder(relative: boolean = false) 文件所在的文件夹的名称
relative: 是否是相对路径tp.file.include(include_link: string | TFile) 包括文件的链接内容。将解析包含内容中的模板。
include_link: 要解析的链接,e.g. MyFile , 或者 TFile 对象.也支持文档块 e.g. MyFile#Section1tp.file.last_modified_date(format: string = "YYYY-MM-DD HH:mm") 文件最后一次修改时间
tp.file.move(new_path: string) 移动文件。能有目录名和文件名不能有后缀。
tp.file.path(relative: boolean = false) 返回文件路径
tp.file.rename(new_title: string) 重命名,只写文件不用写后缀
tp.file.selection() 查找选择的文本
tp.file.tags 文件中的标签,返回字符串列表
tp.file.title 标题
1 | File content: <% tp.file.content %> |
例子报错了,以后再看吧,还是path的问题。
元数据信息
不重要
User Guide
https://silentvoid13.github.io/Templater/internal-functions/internal-modules/frontmatter-module.html
一般用不到
User Guide
https://silentvoid13.github.io/Templater/internal-functions/internal-modules/obsidian-module.html
tp.system.clipboard() 返回用户剪贴板的内容
其他的应该用不到了
User Guide
https://silentvoid13.github.io/Templater/internal-functions/internal-modules/system-module.html
一般用不到
User Guide
https://silentvoid13.github.io/Templater/internal-functions/internal-modules/web-module.html
用于自主DIY这个软件
User Guide
https://silentvoid13.github.io/Templater/internal-functions/contribute.html
你可以从obsidian中调用你自己的脚本,获取其输出,但是要保证你的obsidian库能访问到脚本(需要设置脚本路径)
Let’s say you specified the Scripts folder as your script folder in Templater’s settings.
假设你的自定义脚本在这里 Scripts/my_script.js
脚本需要遵循 CommonJS module specification, 然后导出为单个函数
Let’s have an example with our previous script my_script.js:
1 | function my_function(msg){ |
Templater 将会加载指定文件夹中的所有js文件,然后你就能调用了
<% tp.user.my_script("Hello World!") %>
在用户自定义脚本中可以访问全局命名空间中的变量,比如 app 和 moment.
只不过在使用的时候,你需要将他们当作参数传进去。
这种函数让你执行系统命令获取他们的输出
需要在设置中进行设置
![[assets/image-20220125232545.png]]
暂时不需要这么多,先不看了
User Guide
https://silentvoid13.github.io/Templater/user-functions/system-user-functions.html
一共有三种
开始符号:
<%: 只显示里面命令的表达式输出<%~: 插值,添加了一些字符转义<%*: 执行里面的js代码,默认不输出内容结束符号:都是%>
命令工具
除了以上三种,还有一些工具, 在命令的开始标记中声明。
包括空白控制和动态命令
命令将在进入预览模式时被解析。
声明方式:把开标签换成 <%+
比如查看上次修改日期 <%+ tp.file.last_modified_date() %>
刷新问题:是放缓存中,每次预览刷一次,不会改变
可以通过tp访问对象和所有内部变量和函数
js执行时也可以访问app和moment等
不要忘了使用 await 异步关键字
最好使用原始的插值命令
非要想输出的话,就将要输出的值append到tR的后面 <%* tR += "test" %>
一些例子
1 | <%* if (tp.file.title.startsWith("Hello")) { %> |
Templater不会删除任何换行符,比如
1 | <%* if (tp.file.title == "MyFile" ) { %> |
会变成下面这样
1 | \\换行 |
命令执行完后的空行可以删除。如下。
<%_ 会删除命令前的所有空格_%>) 删除之后的<%-) 会删除命令前的1个空格-%> 会删除后面的1个如下
1 | <%* if (tp.file.title == "MyFile" ) { -%> |
输出为:
1 | This isn't my file! |