Choose a basic template for each site

I am creating an installation that will contain the main site and several microsites. Each microsite will have separate branding, but uses the same page types.

Given that Wagtail already has an object Sitethat references the corresponding tree Page, there are also built-in functions for setting up template loaders to choose the appropriate one base.htmlor I will need to write a custom template loader

+4
source share
2 answers

Wagtail does not have built-in functions for this, since it makes no assumptions about how your templates are combined. However, you could probably implement this yourself quite easily, using a module wagtail.contrib.settingsthat provides the ability to attach custom properties to individual sites. For example, you can define a TemplateSettings model with a field base_template- your templates can then check this parameter and dynamically expand the corresponding template using something like:

{% load wagtailsettings_tags %}
{% get_settings %}
{% extends settings.my_app.TemplateSettings.base_template %}
+8
source

I added the answer above to provide a tag override {% extends ... %}to use the parameter template_dir.

myapp.models:

from wagtail.contrib.settings.models import BaseSetting, register_setting

@register_setting
class SiteSettings(BaseSetting):
    """Site settings for each microsite."""

    # Database fields
    template_dir = models.CharField(max_length=255,
                                    help_text="Directory for base template.")

    # Configuration
    panels = ()

myapp.templatetags.local:

from django import template
from django.core.exceptions import ImproperlyConfigured
from django.template.loader_tags import ExtendsNode
from django.template.exceptions import TemplateSyntaxError


register = template.Library()


class SiteExtendsNode(ExtendsNode):
    """
    An extends node that takes a site.
    """

    def find_template(self, template_name, context):

        try:
            template_dir = \
                context['settings']['cms']['SiteSettings'].template_dir
        except KeyError:
            raise ImproperlyConfigured(
                "'settings' not in template context. "
                "Did you forget the context_processor?"
            )

        return super().find_template('%s/%s' % (template_dir, template_name),
                                     context)


@register.tag
def siteextends(parser, token):
    """
    Inherit a parent template using the appropriate site.
    """

    bits = token.split_contents()

    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' takes one argument" % bits[0])

    parent_name = parser.compile_filter(bits[1])
    nodelist = parser.parse()

    if nodelist.get_nodes_by_type(ExtendsNode):
        raise TemplateSyntaxError(
            "'%s' cannot appear more than once in the same template" % bits[0])

    return SiteExtendsNode(nodelist, parent_name)

myproject.settings:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            ...
            'builtins': ['myapp.templatetags.local'],
        },
    },
]
+6
source

Source: https://habr.com/ru/post/1628827/


All Articles