【问题标题】:Including django templates in other templates so they are actually parsed在其他模板中包含 django 模板,以便实际解析它们
【发布时间】:2012-02-18 10:29:58
【问题描述】:

Django 的继承结构快把我逼疯了。

我正在创建几个皮肤,每个皮肤都有两列和三列布局:

site1
  - two_cols.html
  - three_cols.html
site2
  - two_cols.html
  - three_cols.html

three_cols.html 布局扩展了 two_cols.html,只是在布局中添加了一个额外的列,其中包含一个额外的 django-cms 占位符用于内容。

对于 site1 和 site2,在这个阶段几乎唯一的变化是不同的样式表名称,所以我想将 two_cols.html 和 three_cols.html 移动到一个“common”目录中,并让 site1 和 site2 下的那些扩展他们。

所以:

  • two_cols.html 为 CSS 路径设置块名称
  • three_cols.html 扩展了 two_cols.html 并提供了一个额外的列

我已经为两个 col 布局做了这个,它工作正常。但是,例如,对于 site1 中的 three_col 布局,我想扩展 site1/two_cols.html (设置 CSS 路径),然后只包含 'common/three_cols.html' 的内容(定义额外的列)。

这对我不起作用,因为 three_cols.html 包含 django-cms 占位符标签,因为我只是“包含”模板,显然没有被解析,而只是被评估。

换一种说法,我希望 site1/two_cols.html 中的一些设置能够从 site1/three_cols.html 中使用,它们都扩展了不同的模板。

总结

我的问题是我怎样才能包含一些东西,这样就好像它在父模板中一样(django 文档说 include 不会这样做:https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#include)?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    为什么你不能用普通的extends 做这个? three_cols 可以扩展two_cols 并在主块内添加另一列,使用block.super 来渲染原来的两列。

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2019-04-21
      • 2015-10-25
      • 2015-07-25
      • 2016-12-28
      • 1970-01-01
      相关资源
      最近更新 更多