markata icon indicating copy to clipboard operation
markata copied to clipboard

Create Base html and extend to templates

Open WaylonWalker opened this issue 3 years ago • 3 comments

Each template currently has heavy copy/paste between templates. Leveraging jinja to extend would be cleaner.

Here is how mkdocs does it.

WaylonWalker avatar Sep 22 '22 12:09 WaylonWalker

How about using the Environment class to load the templates for parsing the available base template?

# plugins/feeds.py
from jinja2 import Environment, FileSystemLoader


with open(template) as f:
    env = Environment()
   # add in some config for the default path to custom template folder
    env.loader = FileSystemLoader("markata/plugins/")
    if type(template) is PosixPath:
        template = env.get_template(template.name)
    else:
        template = Template(f.read())

I have added the config for my site and it has worked for most parts but it might not work for all types of config since we will have to load the template by inheriting the base template. It would give errors like no loader for this environment specified as it might try to load the template with the extends tag and it won't find the template loaded.

So, maybe you have different thoughts on this?

Mr-Destructive avatar Oct 15 '22 15:10 Mr-Destructive

Is it possible to have more than one directory. We can make the filesystemloader directory configurable, but if a user decides to use their own template do they have to completely leave built in templates behind, or is there a way leverage built ins and override them per project?

WaylonWalker avatar Oct 16 '22 01:10 WaylonWalker

Cool, so we can provide a config by selecting a template for a particular page(title). If there is a templates folder provided for that type of page, it will be chosen else it will pick up from the template's parent which will be the default template directory.

# plugins/feeds.py

from jinja2 import Environment, FileSystemLoader


with open(template) as f:
    env = Environment()
    # get the template path in the config as:
    #
    # [markata.templates_dir]
    # feed = "pages/templates"
    # 
    # if not provided a config, the default templates parent directory is considered
    templates_dir = markata.config.get("templates_dir")
    templates_dir = str(template.parent) if not templates_dir else templates_dir[title] 

    env.loader = FileSystemLoader(template_dir)

    # check if the path exists and if the template to be rendered is default or custom one
    if type(template) is PosixPath and template in env.list_templates():
        template = env.get_template(template.name)
    else:
        template = Template(f.read())

Should this be configurable enough?

Mr-Destructive avatar Oct 16 '22 11:10 Mr-Destructive