如何使用HubSpot的HubL为数组赋值

时间:2015-08-07 14:50:14

标签: arrays jinja2 hubspot

我目前正试图弄清楚如何允许内容管理员更改元素在最近的帖子链接列表中的显示顺序。本质上,该概念涉及他们在排序数组中选择索引,该排序数组将该元素的类型保存为字符串,稍后将使用输出逻辑迭代,确定每个片段的放置位置。不幸的是,使用HubL的一个缺点是缺乏调试或错误报告,所以我无法看到我的语法失败的地方。我想知道InternetLand中是否有人对此有过类似的经历。

这是HubL:

<div class="theme-recent-posts">
  {% if widget.title_name %}
    <div class="theme-recent-posts-title">
      <{{ widget.title_tag }}><span>{{ widget.title_name }}</span></{{ widget.title_tag }}>
    </div>
  {% endif %}
  {% set posts = blog_recent_posts(widget.select_blog, widget.max_links) %}
  {% for post in posts %}
    {% set item_objects = [] %}
    {% if widget.show_images == "true" %}
      {% set item_objects[widget.sort_images] = "image" %}
    {% endif %}
    {% if widget.show_titles == "true" %}
      {% set item_objects[widget.sort_titles] = "title" %}
    {% endif %}
    {% if widget.show_dates == "true" %}
      {% set item_objects[widget.sort_dates] = "date" %}
    {% endif %}
    {% if widget.show_authors == "true" %}
      {% set item_objects[widget.sort_authors] = "author" %}
    {% endif %}
    <div class="theme-recent-posts-item">
      <a href="{{ post.absolute_url }}">
        {% for object in item_objects %}
          {% if object == "image" %}
            <span class="theme-recent-posts-item-image"><img src="{{ post.featured_image }}" alt="{{ post.name }}" /></span>
          {% endif %}
          {% if object == "title" %}
            <span class="theme-recent-posts-item-title">{{ post.name }}</span>
          {% endif %}
          {% if object == "date" %}
            <span class="theme-recent-posts-item-date">{{ post.created }}</span>
          {% endif %}
          {% if object == "author" %}
            <span class="theme-recent-posts-item-author">{{ post.author_name }}</span>
          {% endif %}
        {% endfor %}
      </a>
    </div>
  {% endfor %}
</div>

目前输出结果为:

<div class="theme-recent-posts">
  <div class="theme-recent-posts-title">
    <h2><span>Recent Posts</span></h2>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-3"></a>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-2"></a>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-1"></a>
  </div>
</div>

假设在小组件面板中连接了默认值:

widget.show_images = true
widget.show_images = true
widget.show_images = true
widget.show_images = true
widget.sort_images = 0
widget.sort_titles = 1
widget.sort_dates = 2
widget.sort_authors = 3

输出应该基本上是这样的:

<div class="theme-recent-posts">
  <div class="theme-recent-posts-title">
    <h2><span>Recent Posts</span></h2>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-3">
      <span class="theme-recent-posts-item-image"><img src="path/to/image-3.ext" alt="Post Name 3" /></span>
      <span class="theme-recent-posts-item-title">Post Name 3</span>
      <span class="theme-recent-posts-item-date">1234567890</span>
      <span class="theme-recent-posts-item-author">FirstName LastName</span>
    </a>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-2">
      <span class="theme-recent-posts-item-image"><img src="path/to/image-2.ext" alt="Post Name 2" /></span>
      <span class="theme-recent-posts-item-title">Post Name 2</span>
      <span class="theme-recent-posts-item-date">1234567890</span>
      <span class="theme-recent-posts-item-author">FirstName LastName</span>
    </a>
  </div>
  <div class="theme-recent-posts-item">
    <a href="//website/path/post-name-1">
      <span class="theme-recent-posts-item-image"><img src="path/to/image-1.ext" alt="Post Name 1" /></span>
      <span class="theme-recent-posts-item-title">Post Name 1</span>
      <span class="theme-recent-posts-item-date">1234567890</span>
      <span class="theme-recent-posts-item-author">FirstName LastName</span>
    </a>
  </div>
</div>

我要在这篇文章中标记Jinja,因为HubL基于Jinja,虽然它不是直接翻译。

如果有足够声誉的人正在阅读此内容,我将非常感谢添加HubL语言标记,因为它目前不存在以供选择(即使HubSpot是)。

更新

Twig也是HubL的近亲,所以我查看了如何在那里更新数组中的元素,并提出了这个答案:

Setting element of array from Twig

{% set item_objects = item_objects|merge({widget.sort_images:"image"}) %}

实施没有按预期进行。输出保持不变。

1 个答案:

答案 0 :(得分:0)

虽然这不是我问的问题的答案,但它是一种替代解决方案,因此我将其作为一种解决方案提供,直到确定真正的解决方案。

<div class="theme-recent-posts">

  {% if widget.title_name %}
    <div class="theme-recent-posts-title">
      <{{ widget.title_tag }}><span>{{ widget.title_name }}</span></{{ widget.title_tag }}>
    </div>
  {% endif %}

  {% set object_order = [0, 1, 2, 3] %}
  {% set posts = blog_recent_posts(widget.select_blog, widget.max_links) %}

  {% for post in posts %}
    <div class="theme-recent-posts-item">
      <a href="{{ post.absolute_url }}">
        {% for order in object_order %}
          {% if widget.show_images == "true" and widget.sort_images == order %}
            <span class="theme-recent-posts-item-image"><img src="{{ post.featured_image }}" alt="{{ post.name }}" /></span>
          {% endif %}
          {% if widget.show_titles == "true" and widget.sort_titles == order %}
            <span class="theme-recent-posts-item-title">{{ post.name }}</span>
          {% endif %}
          {% if widget.show_dates == "true" and widget.sort_dates == order %}
            <span class="theme-recent-posts-item-date">{{ post.created }}</span>
          {% endif %}
          {% if widget.show_authors == "true" and widget.sort_authors == order %}
            <span class="theme-recent-posts-item-author">{{ post.author_name }}</span>
          {% endif %}
        {% endfor %}
      </a>
    </div>
  {% endfor %}

</div>

这给了我正在寻找的灵活性,并且代码实际上与最初提出的方法相比有所减轻。我们的想法是使用object_order列表作为for order in object_order循环的掩码。这可以完成工作,虽然我知道它很笨拙,但它有一丝优雅。