Skip to content

cli

Class info

🛈 DocStrings

render

render(
    template_path: str = Option(None, *IN_CMDS, help=IN_HELP, show_default=False),
    cfg_files: list[str] = Option(None, *CFG_CMDS, help=CFG_HELP, show_default=False),
    output: str = Option(None, *OUT_CMDS, help=OUT_HELP, show_default=False),
    undefined: str = Option("strict", *UNDEF_CMDS, help=UNDEF_HELP, show_default=False),
    trim_blocks: bool = Option(None, *TRIM_CMDS, help=TRIM_HELP, show_default=False),
)

Render a Jinja template.

Parameters:

Name Type Description Default
template_path str

Path to a (remote) template file.

Option(None, *IN_CMDS, help=IN_HELP, show_default=False)
cfg_files list[str]

JinjaFiles to load.

Option(None, *CFG_CMDS, help=CFG_HELP, show_default=False)
output str

Output path for resolved template. If not given, print to stdout.

Option(None, *OUT_CMDS, help=OUT_HELP, show_default=False)
undefined str

Set undefined behavior. Overrides JinjaFiles settings.

Option('strict', *UNDEF_CMDS, help=UNDEF_HELP, show_default=False)
trim_blocks bool

Trim blocks. Overrides JinjaFiles settings.

Option(None, *TRIM_CMDS, help=TRIM_HELP, show_default=False)
Source code in src/jinjarope/cli.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@cli.command()
def render(
    template_path: str = t.Option(
        None,
        *IN_CMDS,
        help=IN_HELP,
        show_default=False,
    ),
    cfg_files: list[str] = t.Option(  # noqa: B008
        None,
        *CFG_CMDS,
        help=CFG_HELP,
        show_default=False,
    ),
    output: str = t.Option(
        None,
        *OUT_CMDS,
        help=OUT_HELP,
        show_default=False,
    ),
    undefined: str = t.Option(
        "strict",
        *UNDEF_CMDS,
        help=UNDEF_HELP,
        show_default=False,
    ),
    trim_blocks: bool = t.Option(
        None,
        *TRIM_CMDS,
        help=TRIM_HELP,
        show_default=False,
    ),
):
    """Render a Jinja template.

    Args:
        template_path: Path to a (remote) template file.
        cfg_files: JinjaFiles to load.
        output: Output path for resolved template. If not given, print to stdout.
        undefined: Set undefined behavior. Overrides JinjaFiles settings.
        trim_blocks: Trim blocks. Overrides JinjaFiles settings.
    """
    env = jinjarope.Environment()
    for path in cfg_files:
        env.load_jinja_file(path)
    if undefined:
        env.set_undefined(undefined)  # type: ignore[arg-type]
    if trim_blocks is not None:
        env.trim_blocks = trim_blocks
    text = env.render_file(template_path)
    if output is None:
        print(text)
    else:
        import fsspec

        with fsspec.open(output, mode="w", encoding="utf-8") as f:
            f.write(text)