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

Add support for filters (like j2cli) #33

Open
ndbroadbent opened this issue Jun 4, 2024 · 7 comments
Open

Add support for filters (like j2cli) #33

ndbroadbent opened this issue Jun 4, 2024 · 7 comments

Comments

@ndbroadbent
Copy link

Hi there, I'm trying to switch to this tool from j2cli so I can run it on Python >= 3.12.3. I just have a few filters that my templates require (for my Home Assistant config):

import re

# value = string, regex = string, replace = string
def regex_replace(value, regex, replace):
    return re.sub(regex, replace, value)

def titleize(value):
    return value.title().\
        replace('1St', '1st').replace('2Nd', '2nd').replace('3Rd', '3rd').\
        replace(' S ', "'s ").\
        replace(' Tv ', " TV ").\
        replace(' And ', ' and ').replace(' Of ', ' of ').\
        replace(' The ', ' the ').replace(' To ', ' to ').\
        replace(' With ', ' with ')

def snakecase(value):
    return re.sub(r'[^a-z0-9]+', '_', value.lower())

I previously called it like this:

j2 --filters "$CURRENT_DIR/ha_compile_jinja_filters.py" -f json "$PREPROCESSED_INPUT_FILE" $JINJA_DATA_ARG >> "${OUTPUT_FILE}" 2> "$ERROR_LOG_FILE"

Is it possible to port this to jinjinator? Thanks a lot for working on this!

@ndbroadbent
Copy link
Author

Sorry I found https://github.com/kpfleming/jinjanator-plugins

Is it possible to import a plugin locally instead of releasing it as a public package?

@kpfleming
Copy link
Owner

Absolutely, all that is required is that you install the plugin package into the same Python environment where you've installed 'jinjanator', and it will be found and available to your templates. There is no need to distribute the plugin package.

There's an example 'minimal' plugin which supports all the various plugin features in https://github.com/kpfleming/jinjanator/tree/main/tests/test_plugin; hopefully you will be able to see how to strip that down to just the 'filter' portion and then put your filter functions into it.

@geaaru
Copy link

geaaru commented Oct 27, 2024

Hi, having a way to dynamically add the filters from CLI remain a good point IMHO. Is there a plan to add this without the need of create a plugin?

Thanks in advance for any feedback

@kpfleming
Copy link
Owner

I have no plans to do that, sorry. I have considered allowing plugins to add their own CLI arguments, which would then allow a plugin to be created which offered 'dynamic' filters as j2cli had, but haven't put any effort into that yet :-)

@geaaru
Copy link

geaaru commented Oct 29, 2024

Ok, thanks for the reply.

@weblab-misha
Copy link

Is it possible to use https://github.com/marooou/jinja2-strcase with jinjanator? It would be nice to have some kind of CLI flag, like jinjanator --extension 'jinja2_strcase.StrcaseExtension'

@kpfleming
Copy link
Owner

Not directly, since jinjanator plugins cannot (yet) accept command-line arguments. It would be possible to create a plugin which makes that extension available and @llange may be able to provide a link to a similar one that they've created. I understand that this is slightly more complex than most people would like to see for something as basic as using an extension, but until I have time to extend the plugin model to support command-line arguments, it's the only practical option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants