Skip to content

Lazy evaluation on import / string interpolation? #1433

Answered by vkleen
suimong asked this question in Q&A
Discussion options

You must be logged in to vote

The idiomatic Nickel way to do this is to use an incompletely defined record in template.ncl like so:

{
  name,
  age,
  welcome_msg = "%{name}'s age is %{std.string.from age}",
}

# config.ncl
let vars = { name = "Adam", age = 20 } in let tmpl = (import "template.ncl") & vars in templ.welcome_msg

This keeps the ability to override name and age and the possibility of merging more configuration snippets together. If you need to target a specific export format that doesn't tolerate extra fields, you can annotate them like so:

{
  name | not_exported,
  age | not_exported,
  welcome_msg = "%{name}'s age is %{std.string.from age}",
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@suimong
Comment options

@vkleen
Comment options

@suimong
Comment options

Answer selected by suimong
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