-
-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
templates with dynamic variable reference #3810
Comments
Alternatively this would be solved by a merge mechanism on re-application of the same template (git like) which would be nice either way because this would allow to change a template and update the notes created with it. |
hey @dboeckenhoff, we support handlebars syntax for templates: https://wiki.dendron.so/notes/1mu1qb1vilhqr7tlatwyqxm/ Would this achieve what you are trying to do? |
Hey, Thanks for your answer. I am ready familiar with handlebars in the
template. Once the template is executed though, the handlebar is gone as it
is atm.. I would like to keep the handlebar and only evaluate it in the
markdown preview, so I can change values that the handlebar refers to
later. Awesome project!
Mark Hyunik Choi ***@***.***> schrieb am Mo., 28. Nov. 2022,
14:01:
… hey @dboeckenhoff <https://github.com/dboeckenhoff>,
we support handlebars syntax for templates:
https://wiki.dendron.so/notes/1mu1qb1vilhqr7tlatwyqxm/
Would this achieve what you are trying to do?
—
Reply to this email directly, view it on GitHub
<#3810 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHAWKXL6I4T23TY5L5RQNK3WKSUJDANCNFSM6AAAAAASLJDM34>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
See: this issue |
I have spent some time writing a python script using jinja to allow more powerful templating: FILE: apply_template.py (lives in dendron workspace) import argparse
import collections.abc
import yaml
from jinja2 import Environment, FileSystemLoader
FMSEP = "---\n"
notes_dir = "notes/"
parser = argparse.ArgumentParser()
parser.add_argument("path")
args = parser.parse_args()
path = notes_dir + args.path
def update(d, u, gaps_only=False):
for k, v in u.items():
if isinstance(v, collections.abc.Mapping):
d[k] = update(d.get(k, {}), v, gaps_only=gaps_only)
else:
if gaps_only and k in d:
continue
d[k] = v
return d
class Frontmatter:
def __init__(self, dict_):
# import pdb; pdb.set_trace()
for key, value in dict_.items():
if isinstance(value, dict):
value = Frontmatter(value)
keys = key.split(".")
first = keys[0]
key = ".".join(keys[1:])
if key:
if hasattr(self, key):
raise NotImplementedError()
else:
setattr(self, first, Frontmatter({key: value}))
else:
setattr(self, first, value)
class DotDict(dict):
"""
a dictionary that supports dot notation
as well as dictionary access notation
usage: d = DotDict() or d = DotDict({'val1':'first'})
set attributes: d.val2 = 'second' or d['val2'] = 'second'
get attributes: d.val2 or d['val2']
"""
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def get_yaml(file_):
pointer = file_.tell()
if file_.readline() != FMSEP:
file_.seek(pointer)
return ''
header = ""
while True:
line = file_.readline()
if line == FMSEP:
return header
header += line
def load_dendron_md(path: str) -> dict:
"""
Return the front matter section (between "---") read with yaml
Args:
path (str): path to dendron note
"""
with open(path) as file_:
config = yaml.load(get_yaml(file_), Loader=yaml.FullLoader)
content = file_.read()
return config, content
def save_dendron_md(path: str, front_matter: dict, content: str):
with open(path, "w") as file_:
file_.write(FMSEP)
file_.write(yaml.dump(front_matter))
file_.write(FMSEP)
file_.write(content)
environment = Environment(loader=FileSystemLoader(notes_dir))
fm_dict, main_content = load_dendron_md(path)
content = ""
for template_path in fm_dict["templates"]:
template_path = f"template.{template_path}.md"
template = environment.get_template(template_path)
fm_template_dict, _ = load_dendron_md(notes_dir + template_path)
update(fm_dict, fm_template_dict, gaps_only=True)
front_matter = DotDict(fm_dict)
template_content = template.render(
fm=front_matter
)
template_content = template_content.split(FMSEP)[2]
content += template_content
content += main_content
save_dendron_md(path, fm_dict, content) Templates are remembered using the "templates" variable in the frontmatter of a note: FILE: notes/dummy-person.md ---
id: 8x87qm5up7u7linpof376dr
title: Dummy Person
desc: ''
updated: 1673016779971
created: 1673016756941
templates:
- person.developer
- person.diagnostician
---
## MyNotes
with the two example templates FILE: notes/templates.person.developer.md ---
id: y3jky2vfggyio96i8wwdt1o
title: Developer
desc: ''
updated: 1673013780932
created: 1672993521263
person:
developer:
codes:
- link.name.of.note.referencing.the.code.without.extension
- other.code.note
---
## Codes
{% for code in fm.person.developer.codes %}
* [[{{ code }}]]
{% endfor% and FILE: notes/templates.person.diagnostician.md
Then you can run the python script (better would be a dendron command later obviously) to (re-)apply the templates at once. I do it via a launch.json in vscode automatically from the node.
This is just an example use-case of how things can work and I made it compatible with the current syntax. I am using this for now. A nice improvement would be to work with diffs to avoid duplicating the content of the templates but that is too much work for the current stage. Curious to hear your opinions. |
Please select if your request is either something new or an enhancement
Please select the area your request applies to. (Multiple selections are Possible. You can leave blank if you're not sure.)
Is your feature request related to a problem? Please describe
At the moment it is (to my understanding) not possible to apply a template that produces a text with the link e.g. {{fm.title}} to be able to later change title dynamically
Describe the solution you'd like
It would be very powerful, if we could escape {} brakets in templates so it is rendered correctly into the text after applying the template.
Describe alternatives you've considered
Additional context
The text was updated successfully, but these errors were encountered: