Skip to content

Instantly share code, notes, and snippets.

@ceelian
Last active March 9, 2023 17:34
Show Gist options
  • Save ceelian/c4ea1699c60289a47b2c9084d3ce7f74 to your computer and use it in GitHub Desktop.
Save ceelian/c4ea1699c60289a47b2c9084d3ce7f74 to your computer and use it in GitHub Desktop.
Mixin for Wagtail Pages to have custom templates per site domain
# The MIT License (MIT)
#
# Copyright (c) 2023 Christian Haintz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from django.template import Engine, TemplateDoesNotExist
from wagtail.core.models import Page, Site
class SiteDomainPageTemplateMixin(Page):
"""
Mixin for Wagtail Page Class to have different html template files for
different Wagtail sites. For example you can create multiple templates for a single
wagtail Page class. In your templates folder just create additional files
for the Page with the Site hostname added before the extension of the file
(usually .html) like so:
my_page.html <-- default/fallback template
my_page.example.com.html <-- is used when the site hostname example.com is called
my_page.otherdomain.com.html <-- is used for otherdomain.com site
Use the mixin for your Template like:
class MyPage(SiteDomainPageTemplateMixin, Page):
pass
"""
class Meta:
abstract = True
def get_template(self, request, *args, **kwargs):
current_site = Site.find_for_request(request)
try:
template_path = super().get_template(request, args, kwargs)
# split the template path in "app/filename" and "fileextension"
path_segments = template_path.rsplit('.', 1)
site_template_path = (
path_segments[0] + '.' + current_site.hostname + '.' + path_segments[1]
)
# get the default template engine and try to load the template
# if no exception happens, the custom site template exists
# and we return the path
engine = Engine.get_default()
engine.get_template(site_template_path)
return site_template_path
except TemplateDoesNotExist as e:
# if an exception happens a site template could not be found and we
# fallback to the default template path
regular_template_path = super().get_template(request, args, kwargs)
return regular_template_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment