Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

How to enable Jinja Extension? Jinjax #38

Open
AhnafCodes opened this issue Apr 13, 2024 · 0 comments
Open

How to enable Jinja Extension? Jinjax #38

AhnafCodes opened this issue Apr 13, 2024 · 0 comments

Comments

@AhnafCodes
Copy link

JinjaX is a new library(Jinja extension) which gives us ability to define granular components and compose Pages/Views using those smaller components.

I was try to make it work with "go-jinja2" setup

Changes I made:
File: requirements.txt

click==8.1.7
Jinja2==3.1.3
jinjax==0.31
jsonpath-ng==1.6.1
MarkupSafe==2.1.5
ply==3.11
python-slugify==8.0.4
PyYAML==6.0.1
text-unidecode==1.3
whitenoise==6.6.0

File: python_src/go_jinja2/jinja2_renderer.py

from jinjax import JinjaX, Catalog
from .jinja2_utils import MyEnvironment, extract_template_error, RootTemplateLoader, SearchPathAbsLoader, \
 ......
class Jinja2Renderer:
    def __init__(self, opts):
        self.opts = opts
    def build_env(self):
        debug_enabled = self.opts.get("debugTrace", False)
        root_loader = RootTemplateLoader()
        loader = ChoiceLoader([
            root_loader,
            SearchPathAbsLoader(self.opts.get("searchDirs", [])),
            MyFileSystemLoader(self.opts.get("searchDirs", [])),
        ])
        environment = MyEnvironment(debug_enabled=debug_enabled,
                                    ......
        environment.globals.update(self.opts.get("globals", {}))
......
        for e in self.opts.get("extensions", []):
            environment.add_extension(e)

        environment.add_extension(JinjaX)
        catalog = Catalog(jinja_env=environment)
        catalog.add_folder("components") ......

File: example/main.go:

func main() {
	j2, err := jinja2.NewJinja2("example", 1,
		jinja2.WithGlobal("test_var1", 1),
		jinja2.WithGlobal("test_var2", map[string]any{"test": 2}))
	......
	template1 := "{{ test_var1 }}"

	s, err := j2.RenderString(template1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("template: %s\nresult: %s\n", template1, s)

	template2 := "<Message message='Hello' />"
	s2, err := j2.RenderString(template2)
	if err != nil {
		panic(err)
	}

	fmt.Printf("template: %s\nresult: %s", template2, s2)
}

template1 renders earlier but, template2 := "" is not evaluated with i.e. Jinjax part is not worling

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

No branches or pull requests

1 participant