Template sets

pull/569/head
Zef Hemel 2023-11-15 14:56:34 +01:00
parent b26b7f4f0a
commit 663a15fe33
14 changed files with 121 additions and 16 deletions

View File

@ -1,9 +1,10 @@
import { WidgetContent } from "$sb/app_event.ts";
import { handlebars, markdown, space, system, YAML } from "$sb/syscalls.ts";
import { markdown, space, system, YAML } from "$sb/syscalls.ts";
import { rewritePageRefs } from "$sb/lib/resolve.ts";
import { loadPageObject, replaceTemplateVars } from "../template/template.ts";
import { renderToText } from "$sb/lib/tree.ts";
import { PageMeta } from "$sb/types.ts";
import { renderTemplate } from "../template/plug_api.ts";
type TemplateConfig = {
// Pull the template from a page
@ -39,13 +40,15 @@ export async function widget(
)
: undefined;
let rendered = config.raw ? templateText : await handlebars.renderTemplate(
templateText,
value,
{
page: pageMeta,
},
);
console.log("Value", value);
let { text: rendered } = config.raw
? { text: templateText }
: await renderTemplate(
templateText,
pageMeta,
value,
);
if (templatePage) {
const parsedMarkdown = await markdown.parseMarkdown(rendered);

View File

@ -57,7 +57,7 @@ export function PageNavigator({
darkMode={darkMode}
completer={completer}
allowNew={true}
helpText="Press <code>Enter</code> to open the selected page, or <code>Shift-Enter</code> to create a new page."
helpText="Press <code>Enter</code> to open the selected page, or <code>Shift-Enter</code> to create a new page with this exact name."
newHint="Create page"
completePrefix={completePrefix}
onSelect={(opt) => {

View File

@ -4,6 +4,7 @@ release.
---
## Next
* The `Alt-q` command is now bound to the new {[Live Queries and Templates: Refresh All]} command refreshing all [[Live Queries]] and [[Live Templates]] on the page. This is to get yall prepared to move away from directives.
* Its time to get ready for the removal of directives, and convert your entire space over using (one time operation). Please backup your space before you do: {[Directive: Convert Entire Space to Live/Templates]}
---
@ -505,8 +506,8 @@ Besides these architectural changes, a few other breaking changes were made to s
## 0.0.31
* Update to the query language: the `render` clause now uses page reference
syntax `[[page]]`. For example `render [[template/task]]` rather than
`render "template/task"`. The old syntax still works, but is deprecated,
syntax `[[page]]`. For example `render [[template/tasks/task]]` rather than
`render "template/tasks/task"`. The old syntax still works, but is deprecated,
completion for the old syntax has been removed.
* Updates to templates:
* For the `Template: Instantiate Page` command, the page meta value `$name` is

View File

@ -48,10 +48,10 @@ The following query shows all attributes available for tasks:
```query
upnext
```
Although you may want to render it using a template such as [[template/task]] instead:
Although you may want to render it using a template such as [[template/tasks/task] instead:
```query
upnext render [[template/task]]
upnext render [[template/tasks/task]]
```
## taskstate

43
website/Template Sets.md Normal file
View File

@ -0,0 +1,43 @@
This is an attempt at collecting useful, reusable templates so you dont have to reinvent the wheel.
The most convenient ways to use them is using [[Federation]]. This will synchronize these templates into your space and make them available for use instantly.
To set this up, add this to your [[SETTINGS]]:
```yaml
federate:
- uri: silverbullet.md/template
```
If you dont want to sync _all_ these templates, you can use more specific URIs, e.g.
```yaml
federate:
- uri: silverbullet.md/template/tasks
```
to just get the `tasks` stuff.
To reference a template, use the federation syntax, e.g. `[[!silverbullet.md/template/tasks/task]]`.
## Maintenance
```query
template where name =~ /^template\/maintenance/
order by order
render [[template/documented-template]]
```
## Pages
```query
template
where name =~ /^template\/pages/
order by order
render [[template/documented-template]]
```
## Tasks
```query
template where name =~ /^template\/tasks/
order by order
render [[template/documented-template]]
```
## Debugging
```query
template where name =~ /^template\/debug/ render [[template/documented-template]]
```

View File

@ -0,0 +1,12 @@
---
tags: template
description: |
Renders its object value in a `key: value` format
usage: |
Can be used by passing in a YAML object in a template via `value` or in a `render` clause of a query
---
{{#each .}}
{{@key}}: {{.}}
{{/each}}
---

View File

@ -0,0 +1,4 @@
* [[{{ref}}|{{ref}}]] {{description}}
{{#if usage}}
* **Usage:** {{usage}}
{{/if}}

View File

@ -0,0 +1,9 @@
---
tags: template
description: Lists all pages with ".conflicted" in the name, created as a result of a synchronization conflict.
---
### Conflicting pages
```query
page where name =~ /\.conflicted/ render [[template/pages/page]]
```

View File

@ -0,0 +1,6 @@
---
tags: template
description: A page reference link as a list item
---
* [[{{name}}]]

View File

@ -1,4 +1,4 @@
#template
```query
task where tags = "{{.}}" and done = false render [[template/task]]
task where tags = "{{.}}" and done = false render [[template/tasks/task]]
```

View File

@ -0,0 +1,10 @@
---
tags: template
description: |
Shows all tasks that reference (tag) the current page. For instance a task that references `[[John]]` in its name, would appear on the `John` page if it would use this [[sets/tasks/incoming]] template.
order: 2
---
```query
task where name =~ /\[\[{{escapeRegexp @page.name}}\]\]/ where done = false render [[template/tasks/task]]
```

View File

@ -0,0 +1,10 @@
---
tags: template
description: Queries all tasks tagged with a specific tag.
usage: Pass in the tag to filter on as the `value` of this template
order: 2
---
```query
task where tags = "{{.}}" and done = false render [[template/tasks/task]]
```

View File

@ -0,0 +1,7 @@
---
tags: template
description: generic task template that supports updating the status back in the origin page
order: 1
---
* [{{state}}] [[{{ref}}]] {{name}}

View File

@ -42,12 +42,12 @@ task where page = "{{@page.name}}"
```
## Rendering
There is a [[!silverbullet.md/template/task]] template you can use to render tasks nicely rather than using the default table (as demonstrated above). When you use this template, you can even cycle through the states of the task by click on its state _inside_ the rendered query, and it will update the state of the _original_ task automatically (although not yet in reverse) — this works across pages.
There is a [[!silverbullet.md/template/tasks/task]] template you can use to render tasks nicely rather than using the default table (as demonstrated above). When you use this template, you can even cycle through the states of the task by click on its state _inside_ the rendered query, and it will update the state of the _original_ task automatically (although not yet in reverse) — this works across pages.
Try it (by clicking on the checkbox inside of the directive):
```query
task where page = "{{@page.name}}" and name = "Remote toggle me" render [[template/task]]
task where page = "{{@page.name}}" and name = "Remote toggle me" render [[template/tasks/task]]
```
* [ ] Remote toggle me