Skip to content
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

feat: load additional templates #40

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,20 @@ contemplate \
However, it is an error to specify standard input as multiple source or destination values.

[minijinja-compat]: https://github.com/mitsuhiko/minijinja/blob/main/COMPATIBILITY.md

## Additional templates

Additional templates can be loaded from the file system with the `--additional-templates` / `-a` argument. The specified directory will be added as a search path. Template files contained within can now be referenced by their relative path. They are loaded on demand the first time they are referenced in a template.

For example, if you had the following file structure:

```
extra_templates
├── content
│ └── news.txt
└── macros.j2
```

Then you could call `contemplate -a extra_templates` and refer to the files as `macros.j2` and `content/news.txt`.

For details on how to refer to additional templates, read the minijinja documentation about [`extends`](https://docs.rs/minijinja/latest/minijinja/syntax/index.html#-extends-), [`include`](https://docs.rs/minijinja/latest/minijinja/syntax/index.html#-include-) and [`import`](https://docs.rs/minijinja/latest/minijinja/syntax/index.html#-import-).
25 changes: 25 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ impl Cli {
.or_else(|| env::var("CONTEMPLATE_K8S_NAMESPACE").ok())
}

/// The additional-templates argument
///
/// Attempts to take this from the `--additional-templates` argument, falling back to the `CONTEMPLATE_ADDITIONAL_TEMPLATES` environment variable.
pub fn additional_templates(&self) -> Option<String> {
self.matches
.get_one::<String>("additional-templates")
.map(ToOwned::to_owned)
.or_else(|| env::var("CONTEMPLATE_ADDITIONAL_TEMPLATES").ok())
}

/// Should editing be done in-place
pub fn in_place(&self) -> InPlace {
if self.matches.get_occurrences::<String>("in-place").is_some() {
Expand Down Expand Up @@ -686,6 +696,21 @@ fn command() -> Command {
.help("Run as a daemon")
.requires("watch"),
)
.arg(
Arg::new("additional-templates")
.long("additional-templates")
.short('a')
.action(ArgAction::Set)
.value_name("DIRECTORY")
.num_args(1)
.value_hint(ValueHint::DirPath)
.help("Directory from which additional templates are loaded.")
.long_help(indoc! {
"Directory from which additional templates are loaded.

Needed for 'extends', 'include' and 'import' tags."
}),
)
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ fn main() -> Result<()> {

let mut env = minijinja::Environment::new();
env.set_undefined_behavior(minijinja::UndefinedBehavior::Chainable);
if let Some(load_path) = cli.additional_templates() {
env.set_loader(minijinja::path_loader(load_path));
}
filters::register(&mut env);
if let Err(e) = plan.ensure_cached(&mut env) {
log::error!("Error caching templates: {e}");
Expand Down
Loading