Skip to content

Commit

Permalink
Merge pull request #99 from cmason3/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
cmason3 authored Oct 10, 2024
2 parents 9a23f0d + 5edde6e commit 6389a30
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 95 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## CHANGELOG

### [1.21.0] - October 10, 2024
- Added support for a `filters` lookup to dynamically use filters
- Column widths are now correctly worked out if `jinjafx.tabulate()` uses row values with simple ANSI colour codes
- You can now append additional data using `-d` alongside `-dt`
- Jinja2 Extension `jinja2.ext.loopcontrols` is now enabled by default

### [1.20.1] - May 15, 2024
- Added suport for different table styles for `jinjafx.tabulate()`

Expand Down Expand Up @@ -528,6 +534,7 @@ Updated `to_yaml` and `to_nice_yaml` to use `SafeDumper`
- Initial release


[1.21.0]: https://github.com/cmason3/jinjafx/compare/v1.20.1...v1.21.0
[1.20.1]: https://github.com/cmason3/jinjafx/compare/v1.20.0...v1.20.1
[1.20.0]: https://github.com/cmason3/jinjafx/compare/v1.19.3...v1.20.0
[1.19.3]: https://github.com/cmason3/jinjafx/compare/v1.19.2...v1.19.3
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ python3 -m pip install --upgrade --user jinjafx

```
jinjafx -t <template.j2> [-d [<data.csv>]] [-g <vars.(yml|json)>]
-dt <dt.yml> [-ds <dataset>] [-g <vars.(yml|json)>]
-dt <dt.yml> [-ds <dataset>] [-d [<data.csv>]] [-g <vars.(yml|json)>]
-encrypt/-decrypt [<file1>] [<file2>] [..]
-t <template.j2> - specify a Jinja2 template
Expand Down Expand Up @@ -336,6 +336,16 @@ The [varnames lookup](https://docs.ansible.com/ansible/latest/collections/ansibl
{{ lookup("varnames", "^a", "^b") }} {# return all variables which begin with "a" or "b" #}
```

JinjaFx also supports the following non-Ansible lookup:

- <code><b>lookup("filters", filter_name</b>: String<b>)(args</b>: Any<b>)</b> -> Any</code><br />

This allows you to call a filter dynamically, e.g:

```jinja2
{{ lookup("filters", "cisco10hash")("password") }}
```

### JinjaFx Variables

The following variables, if defined within `vars.yml` control how JinjaFx works:
Expand Down Expand Up @@ -469,7 +479,7 @@ While this is valid in YAML, it isn't valid to have a list when Jinja2 expects a

### JinjaFx DataTemplates ###

JinjaFx also supports the ability to combine the data, template and vars into a single YAML file called a DataTemplate (no support for JSON as JSON doesn't support multiline strings, although you can specify the `vars` section using JSON), which you can pass to JinjaFx using `-dt`. This is the same format used by the JinjaFx Server when you click on 'Export DataTemplate'. It uses headers with block indentation to separate out the different components - you must ensure the indentation is maintained on all lines as this is how YAML knows when one section ends and another starts.
JinjaFx also supports the ability to combine the data, template and vars into a single YAML file called a DataTemplate (no support for JSON as JSON doesn't support multiline strings, although you can specify the `vars` section using JSON), which you can pass to JinjaFx using `-dt`. This is the same format used by the JinjaFx Server when you click on 'Export DataTemplate'. It uses headers with block indentation to separate out the different components - you must ensure the indentation is maintained on all lines as this is how YAML knows when one section ends and another starts. If you specify `-d` alongside `-dt` then it will append to the data section of the DataTemplate with what is provided via `-d`.

```yaml
---
Expand Down Expand Up @@ -524,6 +534,8 @@ jinja2_extensions:
- 'jinja2.ext.debug'
```

The `jinja2.ext.loopcontrols` extension is enabled by default unless you have defined `jinja2_extensions` and then it isn't enabled unless you have explicitly enabled it.

JinjaFx will then attempt to load and enable the extensions that will then be used when processing your Jinja2 templates. You also have the ability to check whether an extensions is loaded within your template by querying `jinja2_extensions` directly.

Unfortunately writing Jinja2 Extensions isn't that obvious - well, I didn't find it that obvious as it took me quite a while to work out how to write a custom filter. Let's assume we want to write a custom filter called `add` that simply adds a value to a number, for example:
Expand Down
Loading

0 comments on commit 6389a30

Please sign in to comment.