我正在使用名为HubL的模板语言在一个名为Hubspot的cms中工作 . Hubspot是用python编写的,HubL非常类似于Jinja2或Django模板 . 通常当我需要找到答案时,我可以在jinja2中搜索答案并找到我需要的答案 . 我一直试图解决这个问题几个星期,但我认为问题是具体的 .
Hubspot主持博客 . 这些博客可以在博客模板中的“contents”变量下访问,也可以在任何其他模板类型上使用预定义函数:
{% for content in contents %}
{# output blog list #}
{% endfor %}
要么
{% set test_list = blog_recent_posts('default', 250) %}
{% for post in test_list %}
{# output blog list #}
{% endfor %}
每篇博文都有一个topics_list变量 . 此变量包含为特定博客设置的主题列表 . 你可以通过循环来在博客列表循环中使用它 .
{% for content in contents %}
{% for topic in content.topic_list %}
{{ topic.name }}
{% endfor %}
{% endfor %}
如果你只是调用topic_list:
{% for content in contents %}
{{ content.topic_list }}
{% endfor %}
它输出:
[topic1, topic2]
我遇到的问题是我需要从主列表中排除具有特定主题的帖子 . 我可以使用嵌套for循环和条件从输出中排除:
{% for content in contents %}
{% for topic in content.topic_list %}
{% unless topic.name = 'topic 1' %}
{# ouput if condition is not met #}
{% endunless %}
{% endfor %}
{% endfor %}
这很麻烦 . 它遍历博客,每个索引循环遍历主题 . 如果主题与定义的主题匹配,则从输出中省略它 . 这是有效的,除了它没有从内容中省略博客 . 阻止输出的博客仍然存在于他们的索引中 . 例如,如果我将输出限制为3个博客,并且三个博客中的一个具有与“主题1”匹配的主题,则该空间不会填充下一个博客,空格只留空 .
实际上,这样做是为每个博客的每个主题输出列表项的标记,只要该主题与被阻止的主题不匹配,因此如果存在多个主题,它将复制帖子 .
我考虑过使用预定义函数,您可以使用它来定义您想要帖子的特定主题,因此我循环查看与博客相关的全局主题列表,如果主题不等于被阻止的主题,则运行主题具体功能:
{% set mlist = [] %}
{% set global_topics = blog_topics('default', 250) %}
{% for gtopic in global_topics %}
{% unless gtopic == 'topic1' %}
{% set wlist = blog_recent_posts('default', 250, gtopic.slug) %}
{% set mlist2 = mlist.append(wlist) %}
{% endunless %}
{% endfor %}
{% for post in mlist[0] %}
<strong>{{post.name }}</strong><br>
{% for topic in post.topic_list %}
{{topic.name}}<br>
{% endfor %}<br> <br>
{% endfor %}
我真的很惊讶我把它带到了工作期间,但它结合了从每个主题创建的列表并输出它们 . 唯一的问题是,因为多个主题应用于主题列表,虽然它不包括从被阻止的主题创建的列表,但它仍然包含包含使用第二个或第三个(等)主题创建的列表中的被阻止主题的博客在博客文章中 .
最简单的解决方案是通过过滤post来创建一个新列表,如果主题是 IN topic_list但是这不起作用 .
{% set mlist = [] %}
{% for content in contents %}
{% unless 'topic-1' is in content.topic_list %}
{% set mlist2 = mlist.append(content) %}
{% endunless %}
{% endfor %}
但我想你不能像这样使用'in'?它不起作用 .
请记住,我理解如果我在python中直接而不是在模板中进行此过滤,这将是一个非问题,并且我理解逻辑不应该在模板中处理,但是Hubspot不允许自定义python脚本或任何后端功能所以这是我必须使用的 . 谁能想到创意解决方案?