Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
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

Description

@AhnafCodes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions