Django FeinCMS:获取模板中后续内容块的属性
如何在呈现Django FeinCMS:获取模板中后续内容块的属性,django,django-templates,feincms,Django,Django Templates,Feincms,如何在呈现RichTextContent的模板中获取有关后续FeinCMS内容块的信息 获得类名之类的东西会很理想,但是访问模型的字段也会对我有所帮助。开箱即用这里没有办法展望下一个内容块。你到底想完成什么 我们经常不得不用某个类将同一类型的多个内容块包装到另一个中。我们通过自定义templatetag(使用)完成了这一点: 导入ttag 从feincms.templatetags.feincms\u标记导入\u呈现\u内容 @register.tag 类别CmsRenderRegion(tta
RichTextContent
的模板中获取有关后续FeinCMS内容块的信息
获得类名之类的东西会很理想,但是访问模型的字段也会对我有所帮助。开箱即用这里没有办法展望下一个内容块。你到底想完成什么 我们经常不得不用某个类将同一类型的多个内容块包装到另一个
中。我们通过自定义templatetag(使用)完成了这一点:
导入ttag
从feincms.templatetags.feincms\u标记导入\u呈现\u内容
@register.tag
类别CmsRenderRegion(ttag.Tag):
'''
渲染FeinCMS模板区域,就像FeinCMS.templatetags.FeinCMS_tags.render_区域一样。
但是,它会检查每种内容类型是否有一个字段“wrapper\u css\u classes”。如果内容类型为
有了这样一个类,它就为所有的序列添加了一个环境
该类的内容类型。
'''
page=ttag.Arg()
region=ttag.Arg()
请求=ttag.Arg()
def呈现(自身、上下文):
data=self.resolve(上下文)
s=u“
最后一个包装器=无
对于getattr中的内容(数据['page']。内容,数据['region']):
当前包装器=getattr(内容为“包装器css标签”,无)
如果当前_包装为无:
如果最后一个包装器不是无:
#关闭上一个包装器
s+=u'%last_包装[0]
其他:
如果最后一个包装器为“无”:
s+=u'%当前\u包装
elif last_wrapper!=当前_包装器:
s+=u'%(
最后一个\u包装[0],当前\u包装[0],当前\u包装[1])
最后\u包装=当前\u包装
s+=\u呈现内容(内容,请求=数据['request'],上下文=上下文)
如果最后一个包装器不是无:
s+=u'%last_包装[0]
返回s
在模板中,您将使用{%cms\u render\u region feincms\u page'region'request%}
而不是{%cms\u render\u region feincms\u page'region'request%}
来呈现区域
您可以使渲染模板的逻辑适应您试图完成的任何操作。内容块位于page.content.{{region}
中,例如page.content.sidebar
import ttag
from feincms.templatetags.feincms_tags import _render_content
@register.tag
class CmsRenderRegion(ttag.Tag):
'''
Renders a FeinCMS template region, just like feincms.templatetags.feincms_tags.render_region.
However, it checks each content type for a field "wrapper_css_classes". If the content type
has such a class, it adds a surrounding <div class="{{ wrapper_css_classes }}"> to all sequential
content types with that class.
'''
page = ttag.Arg()
region = ttag.Arg()
request = ttag.Arg()
def render(self, context):
data = self.resolve(context)
s = u''
last_wrapper = None
for content in getattr(data['page'].content, data['region']):
current_wrapper = getattr(content, 'wrapper_css_tag', None)
if current_wrapper is None:
if last_wrapper is not None:
# close the previous wrapper
s += u'</%s>' % last_wrapper[0]
else:
if last_wrapper is None:
s += u'<%s class="%s">' % current_wrapper
elif last_wrapper != current_wrapper:
s += u'</%s><%s class="%s">' % (
last_wrapper[0], current_wrapper[0], current_wrapper[1])
last_wrapper = current_wrapper
s += _render_content(content, request=data['request'], context=context)
if last_wrapper is not None:
s += u'</%s>' % last_wrapper[0]
return s