Skip to content

Is it possible to store a dictionary in a .chezmoi.yaml.tmpl data variable? #3107

Closed Answered by halostatue
eugenesvk asked this question in Q&A
Discussion options

You must be logged in to vote

Absolutely. I take advantage of this in my .chezmoitemplates/programs.tmpl to cache all of my lookPath operations (some of them are used in multiple templates; I know that chezmoi caches the result).

The main thing to know is that only strings can return from templates, so you have to convert it to JSON (| toJson) and when you import it, you need to convert it from JSON ($x := importTemplate "programs.tmpl" | fromJson).

For your example, you would need to do

data:
  mydict: #{{ dict "key" 12 | toJson }}

I’m using TOML for my config file, which makes things somewhat uglier for keeping data clean between two different calls of chezmoi init as you can see in https://github.com/halostatue/dot…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by eugenesvk
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants