algoo / jfmengine

A soft fork of JSSG for algoo websites use case
Other
0 stars 0 forks source link

Settings for index page slug and to add jinja2 filters and functions #50

Closed ClmntBcqt closed 2 months ago

ClmntBcqt commented 2 months ago

1. Index page configuration

Currently we need to configure the index page (page at URL "/") in views.py, in IndexView class by changing the lines :

self.kwargs["slug"] = "accueil"
self.kwargs["dir"] = "fr/index"

Expected : It would be clearer and easier if we could configure the slug and folder of index page as a setting in settings.py. For instance to configure the index view with the page content/pages/fr/index/home.md that has the slug accueil :

JFME_INDEX_PAGE = "fr/index/accueil" # <folder>/<slug>

2. Add jinja2 filters and functions

Currently the users can't add Jinja2 filters or functions unless modify the file jinja2.py. Expected : It could be useful to add settings JFME_ADDITIONAL_JINJA2_FILTERS and JFME_ADDITIONAL_JINJA2_FUNCTIONS For instance in settings.py:

import base64, readtime

def base64encode(value):
    return base64.b64encode(value.encode('utf-8')).decode('utf-8')

def readtime_markdown(s):
    result = readtime.of_markdown(s).minutes
    if result == 0:
        return 1
    return result

JFME_ADDITIONAL_JINJA2_FILTERS = {"encode_base_64" : base64encode, "readtime_markdown": readtime_markdown}
JFME_ADDITIONAL_JINJA2_FUNCTIONS = {"encode_base_64" : base64encode, "readtime_markdown": readtime_markdown}

Then in templates :

{{ "foo" | encode_base_64 }}
{{ read_time_markdown("##title **text** blabla") }}
lebouquetin commented 2 months ago

Also, it would be interesting to put all standard filters in a unique place

ClmntBcqt commented 2 months ago

To avoid to import files in settings.py, we give the string of module to import :


JFME_ADDITIONAL_JINJA2_FILTERS = {"encode_base_64" : "jssg.templatetags.base_64.base64encode", "readtime_markdown": "readtime.of_markdown"}
JFME_ADDITIONAL_JINJA2_FILTERS = {"encode_base_64" : "jssg.templatetags.base_64.base64encode", "readtime_markdown": "readtime.of_markdown"}

jfmengine/jssg/templatetags/ is the standard directory to put filters and functions.

ClmntBcqt commented 2 months ago

Merged in #54