~fabrixxm/pytrek

7b0df9681ae9f2af61cffdc78601ec44644f61e7 — fabrixxm 3 months ago 4657160
Add `jstr` template filter

`jstr` convert the input value to json and mark it as safe markup
2 files changed, 17 insertions(+), 0 deletions(-)

M docs/theming.md
M pytrek/template.py
M docs/theming.md => docs/theming.md +10 -0
@@ 124,6 124,16 @@ Format a `datetime` object as a time string, with optional [format](https://docs
{{ entry.date.end|time("%X") }}
```

### jstr

Return a string ready to be used in javascript

```jinja
<script>
	var title = {{ entry.title|jstr }};
</script>
```

## Custom tags

### asset

M pytrek/template.py => pytrek/template.py +7 -0
@@ 1,6 1,7 @@
from typing import Union
from pathlib import Path
from datetime import datetime, date
import json
from jinja2_simple_tags import StandaloneTag  # type: ignore
from jinja2 import (
    Environment,


@@ 9,6 10,7 @@ from jinja2 import (
    FileSystemLoader,
    BaseLoader,
)
from markupsafe import Markup

from .types import Config, Site
from . import meta


@@ 26,6 28,10 @@ def time_format(value: datetime, format: str = "%H:%M"):
    return value.strftime(format)


def js_str(value: str):
    return Markup(json.dumps(value))


class AssetExtension(StandaloneTag):
    safe_output = True
    tags = {"asset"}


@@ 79,4 85,5 @@ def setup(cfg: Config) -> Environment:
    env.filters["date"] = date_format
    env.filters["datetime"] = datetime_format
    env.filters["time"] = time_format
    env.filters["jstr"] = js_str
    return env