HubL语法详解:HubSpot模板语言入门指南

本文详细介绍HubSpot CMS专用模板语言HubL的核心语法、常用标签和实际应用示例,帮助开发者快速掌握HubL编程。

HubL语法详解:HubSpot模板语言入门指南

摘要说明

HubL是HubSpot CMS专用的模板语言,基于Jinja2开发,用于动态生成网页内容。本文详细介绍HubL的核心语法、常用标签和实际应用示例。

目录大纲

  1. HubL基础概念
  2. 变量与表达式
  3. 控制结构
  4. 模块化模板
  5. 常用HubL过滤器
  6. 实战代码示例

正文内容

1. HubL基础概念

HubL(HubSpot Language)是专为HubSpot CMS设计的服务器端模板语言,主要特点包括:

  • 类似Python的简洁语法
  • 支持逻辑控制和数据操作
  • 与HubSpot CRM数据无缝集成
  • 自动XSS防护机制

2. 变量与表达式

{# 变量输出 #}
{{ page.title }}  {# 输出当前页面标题 #}

{# 字典访问 #}
{{ content.widgets.sidebar.value }}

{# 数学运算 #}
{{ 5 + 3 * 2 }}  {# 输出11 #}

3. 控制结构

条件语句

{% if user.is_logged_in %}
  <p>欢迎回来, {{ user.email }}!</p>
{% elif show_guest_content %}
  <p>请登录查看更多内容</p>
{% else %}
  <p>默认欢迎信息</p>
{% endif %}

循环语句

{# 遍历博客文章 #}
{% for post in contents.posts %}
  <article>
    <h3>{{ post.name }}</h3>
    <p>{{ post.post_body|truncate(100) }}</p>
  </article>
{% endfor %}

4. 模块化模板

{# 引入头部模板 #}
{% include "header.html" %}

{# 宏定义 #}
{% macro render_button(text, type='primary') %}
  <button class="btn btn-{{ type }}">{{ text }}</button>
{% endmacro %}

{# 使用宏 #}
{{ render_button('提交表单') }}

5. 常用HubL过滤器

过滤器说明示例
datetimeformat日期格式化`{{ content.publish_date
pprint友好打印`{{ object
tojson转为JSON`{{ data
unique去重`{% for item in list

6. 实战代码示例

{# 动态生成导航菜单 #}
<nav>
  {% for item in menu_items %}
    <a href="{{ item.link }}" 
       class="{% if item.active %}active{% endif %}">
      {{ item.label }}
    </a>
  {% endfor %}
</nav>

{# 集成CRM数据 #}
{% set deals = hubdb_table_rows(123456) %}
{% for deal in deals %}
  <div class="deal-card">
    <h4>{{ deal.name }}</h4>
    <p>金额: {{ deal.amount|format_currency }}</p>
  </div>
{% endfor %}

关键要点

  1. HubL语法类似Jinja2,但深度集成HubSpot生态
  2. 通过{{ }}输出变量,{% %}执行逻辑控制
  3. 支持模块化开发(include/macro)
  4. 过滤器系统可扩展数据展示方式
  5. 可直接访问HubSpot CRM和CMS数据

参考资料