#template #variables #package #replace #aim #double #cli

bin+lib variable-resolver

The aim of this package is to easily replace variables inside a given template

4 stable releases

1.1.0 Nov 18, 2024
1.0.2 Nov 18, 2024
1.0.1 Nov 17, 2024

#95 in Template engine

Download history 292/week @ 2024-11-15 39/week @ 2024-11-22 6/week @ 2024-11-29 8/week @ 2024-12-06

345 downloads per month

MIT license

20KB
524 lines

variable-resolver

The aim of this package is to easily replace variables inside a given template.

The package contains a library and a cli application (repvar).

Format of the template

The package is capable of finding variable names inside double curly braces.

Example template:

Hello, {{name}}

Escaping double curly braces happens by using triple curly braces. Examples:

  • The following template: Hello, {{{name}}} will resolve to Hello, {{name}}
  • The following template: Hello, {{{{name}}}} will resolve to Hello, {{{name}}}
  • The following template: Hello, {{{{{name}}}}} will resolve to Hello, {{Jane}}
  • The following template: Hello, {{{{{{name}}}}}} will resolve to Hello, {{{{name}}}}

Using repvar cli utility

The following example demonstrates how every occurence of a variable is replaced with a value. In the following case every {{name}} is replaced with Jane:

echo "Hello, {{name}}" | repvar -v name=Jane

Templates inside files can be handled by piping the file into repvar. Example:

cat greeting.txt | repvar -v name=Jane

Be aware that writing to the same file as the source is not safe. Example:

cat greeting.txt | repvar -v name=Jane > greeting.txt

Todos

  • docstring
  • case-insensitive variable resolver mode
  • load variables from environment variables
  • load variables from .env files
  • load template from file

Dependencies

~1.1–1.7MB
~32K SLoC