[英]How to assign values to an array using HubSpot's HubL
I'm currently trying to figure out how to allow a content manager to change the order in which elements appear within recent post link lists. 我目前正在尝试弄清楚如何允许内容管理员更改元素在最近的帖子链接列表中显示的顺序。 Essentially, the concept involves them picking the index in a sorting array that will hold that element's type as a string, which will later be iterated over with output logic determining where each piece gets placed.
从本质上讲,该概念涉及他们在排序数组中选择索引,该索引将将该元素的类型保留为字符串,随后将通过输出逻辑确定每个元素的放置位置进行迭代。 Unfortunately, one of the drawbacks of using HubL is the lack of debugging or error reporting so I can't see where my syntax is failing.
不幸的是,使用HubL的缺点之一是缺少调试或错误报告,因此我看不到语法失败的地方。 I was wondering if anyone in InternetLand has had any similar experience with this.
我想知道InternetLand中是否有人对此有类似的经验。
Here's the HubL: 这是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>
Output currently results in: 当前输出结果为:
<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>
Assume the default values wired up in the widget panel: 假设在小部件面板中连接了默认值:
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
The output should essentially be this: 输出本质上应该是这样的:
<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>
I'm going to tag Jinja in this post, because HubL is based on Jinja, though it's not a direct translation. 我将在这篇文章中标记Jinja,因为HubL基于Jinja,尽管它不是直接翻译。
If someone with enough reputation is reading this, I would greatly appreciate the addition of the HubL language tag, as it currently does not exist to select (even though HubSpot was). 如果有足够声誉的人正在阅读本文,我将不胜感激添加HubL语言标记,因为它目前不存在可供选择(即使使用HubSpot也是如此)。
Update 更新
Twig is also a close cousin to HubL, and so I looked up how to update elements in an array there, and came up with this answer: Twig还是HubL的近亲,所以我查看了如何更新那里的数组中的元素,并得出了以下答案:
Setting element of array from Twig 从Twig设置数组的元素
{% set item_objects = item_objects|merge({widget.sort_images:"image"}) %}
Implementation did not work as expected. 实施未按预期进行。 Output remains unchanged.
输出保持不变。
While this is not an answer to the question I asked, it is an alternate solution, so I provide it as a work around until a real solution is determined. 虽然这不能解决我提出的问题,但它是一个替代解决方案,因此我将在解决问题之前提供它,直到确定一个真正的解决方案为止。
<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>
This gives me the flexibility I was looking for and the code is actually lightened quite a bit from the original proposed method. 这为我提供了所需的灵活性,并且与最初提出的方法相比,实际上使代码简化了很多。 The idea is to use the
object_order
list as a mask for the for order in object_order
loop. 想法是使用
object_order
列表作为for order in object_order
循环中for order in object_order
。 This gets the job done, and while I know it's hacky, it has a touch of elegance to it. 这样就完成了工作,尽管我知道它很笨拙,但是却颇具优雅感。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.