From 7734dfa4214f4fcd6cb8f05da076282a0373fdef Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Tue, 4 Mar 2025 10:54:39 -0800 Subject: [PATCH] Test foobar filter --- cookiecutter.json | 3 ++- cookiecutter_extensions.py | 7 ++++++ hooks/post_gen_project.py | 35 -------------------------- {{cookiecutter.out_dir}}/lib/main.dart | 2 +- 4 files changed, 10 insertions(+), 37 deletions(-) create mode 100644 cookiecutter_extensions.py delete mode 100644 hooks/post_gen_project.py diff --git a/cookiecutter.json b/cookiecutter.json index 9632202..87c4a6f 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -28,5 +28,6 @@ "pwa_theme_color": "#0175C2", "split_per_abi": false, "options": null, - "pyproject": null + "pyproject": null, + "_extensions": ["cookiecutter_extensions.FoobarExtension"] } \ No newline at end of file diff --git a/cookiecutter_extensions.py b/cookiecutter_extensions.py new file mode 100644 index 0000000..f374c8d --- /dev/null +++ b/cookiecutter_extensions.py @@ -0,0 +1,7 @@ +from jinja2.ext import Extension + + +class FoobarExtension(Extension): + def __init__(self, environment): + super(FoobarExtension, self).__init__(environment) + environment.filters["foobar"] = lambda v: v * 2 diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py deleted file mode 100644 index 0e7512f..0000000 --- a/hooks/post_gen_project.py +++ /dev/null @@ -1,35 +0,0 @@ -from typing import Any, Optional - -import cookiecutter.utils - - -def get_pyproject(pyproject_toml: Any, setting: Optional[str] = None): - if not setting: - return pyproject_toml - - d = pyproject_toml - for k in setting.split("."): - d = d.get(k) - if d is None: - return None - return d - - -def my_custom_function(value): - """Example function: returns the value in uppercase""" - return value.upper() - - -def another_function(value1, value2): - """Concatenates two values with a space""" - return f"{value1} {value2}" - - -# Inject custom functions into Jinja's global context -def add_custom_filters(cookiecutter): - cookiecutter.globals["my_custom_function"] = my_custom_function - cookiecutter.globals["another_function"] = another_function - - -# Run after project is generated -add_custom_filters(cookiecutter) diff --git a/{{cookiecutter.out_dir}}/lib/main.dart b/{{cookiecutter.out_dir}}/lib/main.dart index 2a05f85..f747e68 100644 --- a/{{cookiecutter.out_dir}}/lib/main.dart +++ b/{{cookiecutter.out_dir}}/lib/main.dart @@ -26,7 +26,7 @@ loading_screen: {{ loading_screen }} loading_screen_text: {{ loading_screen_text }} -Uppercase Example: {{ my_custom_function("abc") }} +Uppercase Example: {{ 2 | foobar }} */ {% for dep in cookiecutter.flutter.dependencies %}