【问题标题】:Select base template per site为每个站点选择基本模板
【发布时间】:2016-02-15 11:17:18
【问题描述】:

我正在构建一个包含一个主站点和多个微型站点的设置。每个微型网站都将有一个单独的品牌,但使用相同的页面类型。

鉴于 Wagtail 已经有一个链接到适当的 Page 树的 Site 对象,是否还有内置功能来配置模板加载器以选择合适的 base.html 或者我必须编写自定义模板加载器?

【问题讨论】:

    标签: wagtail


    【解决方案1】:

    Wagtail 没有为此提供任何内置功能,因为它不对模板的组合方式做出任何假设。但是,您可以使用wagtail.contrib.settings 模块自己轻松实现这一点,该模块提供了将自定义属性附加到各个站点的能力。例如,您可以使用 base_template 字段定义一个 TemplateSettings 模型 - 然后您的模板可以检查此设置并动态扩展适当的模板,使用类似:

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

    【讨论】:

      【解决方案2】:

      我扩展了the above answer 以提供对{% extends ... %} 标记的覆盖以使用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)
      

      我的项目设置:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        相关资源
        最近更新 更多