添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Search the docs for APIs, endpoints or guides.

Can't find what you're looking for? Ask in the community.

Endpoints
Articles

Filters affect the ultimate output of your HubL. They can be applied to various HubL statements and expressions to alter the template markup outputted by the server.

The basic syntax of a filter is |filtername . The filter is added directly following the statement or the expression, within its delimiters. Some filters have additional parameters that can be added in parentheses. The basic syntax of a filter with a string, a number, and a boolean parameters is: |filtername("stringParameter", 10, true) . Notice that string parameters should be written in quotes. Also note that HubL filters have an alias that can be used to serve the same purpose as the primary filter.

The following article contains all of the supported HubL filters.

In the example below, there is variable containing a sequence of types of fruits. The batch filter is applied to a loop that iterates through the sequence. The nested loop runs three times to print 3 types of fruit per row, before the outer loop runs again. Notice in the final output that since there are only 5 types of fruit, the final item is replaced by a &nbsp (the second parameter).

{% set begin = "2018-07-14T14:31:30+0530"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {% set end = "2018-07-20T14:31:30+0530"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {{ begin|between_times(end, "days") }} 6
before{{ var|center(80) }}after </pre> <pre> before string to center after {{ my_color|convert_rgb }} {% color "my_color" color="#000000", export_to_template_context=True %} <div style="background: rgba({{ widget_data.my_color.color|convert_rgb }}, .5)"></div> 255, 255, 255 <div style="background: rgba(0, 0, 0, .5)"></div>

If the value is undefined it will return the first parameter, otherwise the value of the variable will be printed. If you want to use default with variables that evaluate to false, you have to set the second parameter to true . The first example below would print the message if the variable is not defined. The second example applied the filter to an empty string, which is not undefined, but it prints a message due to the second parameter.

{{ my_variable|default("my_variable is not defined") }} {{ ""|default("the string was empty", true) }} my_variable is not defined the string was empty

Sort a dict and yield (key, value) pairs. Dictionaries are unsorted by default, but you can print a dictionary, sorted by key or value. The first parameter is a boolean to determine, whether or not the sorting is case sensitive. The second parameter determines whether to sort the dict by key or value. The example below prints a sorted contact dictionary, with all the known details about the contact.

Escapes the content of an HTML attribute input. Accepts a string and converts the characters & , < , , , and escape_jinjava into HTML-safe sequences. Use this filter for HubL variables that are being added to HTML attributes.

Note that when escaping values of attributes that accept URLs, such as href , you should use the escape_url filter instead.

{% set escape_string = "\tThey said 'This string can safely be inserted into JavaScript.'" %} {{ escape_string|escape_js }} \tThey said \x27This string can safely be inserted into JavaScript.\x27
{% set escape_string = "<script>alert('oh no!')</script>" %} {% require_js position="head" %} <script data-search_input-config="config_{{ name }}" type="application/json"> "autosuggest_results_message": "{{ escape_string|escapejson }}" </script> {% end_require_js %} <script data-search_input-config="config_widget_1234567" type="application/json"> "autosuggest_results_message": "<script>alert('oh no!')<\/script>" </script>
{{ content.publish_date | format_date('long') }} {{ content.publish_date | format_date('yyyyy.MMMM.dd') }} {{ content.publish_date | format_date('medium', 'America/New_York', 'de-DE') }} November 28, 2022 02022.November.28 28.11.2022
{{ content.updated | format_time('long') }} {{ content.updated | format_time('hh:mm a') }} {{ content.updated | format_time('medium', 'America/New_York', 'de-DE') }} 3:25:06 PM Z 03:25 PM 10:25:44
the HubDB Location = 42.3667, -71.1060 (Cambridge, MA) | Chicago, IL = 37.3435, -122.0344 --> {{ row.location | geo_distance(37.3435, -122.0344, "mi") }} MI 861.1655563461395 MI

The indent filter uses whitespace to indent text within a given field length. This filter is not recommended or particularly useful since HubSpot's HTML compiler will automatically strip out the white space; however, it is included here for the sake of comprehensiveness. The example below shows a center filter being applied to a variable in a pre tag, so the whitespace isn't stripped out. The first parameter controls the amount of whitespace and the second boolean toggles whether to indent the first line.

{% set three = 3 %} {% set four = ["four"] %} {% set list_num = one|list + two|list + three|list + four|list %} {{ list_num }} [1,2,3]

Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.

The basic usage is mapping on an attribute. For example, if you want to use conditional logic to check if a value is present in a particular attribute of a dict. Alternatively, you can let it invoke a filter by passing the name of the filter and the arguments afterwards.

{% set seq = ["item1", "item2", "item3"] %} {{ seq|map("upper") }} {{ content|map("currentState")}} [ITEM1, ITEM2, ITEM3] DRAFT
{% set date = "2018-07-14T14:31:30+0530"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {{ date }} {{ date|minus_time(2, "months") }} 2018-07-14 14:31:30 2018-05-14 14:31:30
{% set date = "2018-07-14T14:31:30+0530"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {{ date }} {{ date|plus_time(5, "days") }} 2018-07-14 14:31:30 2018-07-19 14:31:30

Please note: when using this filter, the page will be prerendered periodically rather than every time the page content is updated. This means that the filtered content will not be updated on every page reload.

This may not be an issue for certain types of content, such as displaying a random list of blog posts. However, if you need content to change randomly on every page load, you should instead use JavaScript to randomize the content client-side.

Searches for a regex pattern and replaces with a sequence of characters. The first argument is a RE2-style regex pattern, the second is the replacement string.

Information on the RE2 regex syntax can be found here .

{% for content in contents|rejectattr("post_list_summary_featured_image") %} <div class="post-item"> {% if content.post_list_summary_featured_image %} <div class="hs-featured-image-wrapper"> <a href="{{content.absolute_url}}" title="" class="hs-featured-image-link"> <img src="{{ content.post_list_summary_featured_image }}" class="hs-featured-image"> {% endif %} {{ content.post_list_content|safe }} {% endfor %} <div class="post-item">Post with no featured image</div> <div class="post-item">Post with no featured image</div> <div class="post-item">Post with no featured image</div>
{% if topic %} <h3>Posts about {{ page_meta.html_title|replace("Blog | ", "") }}</h3> {% endif %} <h3>Posts about topic name</h3>
Optional

Options include common round either up or down (default); ceil always rounds up; floor always rounds down.

If you don’t specify a method common is used.

Sanitizes the content of an HTML input for the output of rich text content. Accepts a string, then strips HTML tags that are not allowed. Use this filter for HubL variables that are used in HTML that should allow safe HTML.

When using this filter, you can include the following parameters for allowing specific types of HTML tags: FORMATTING , BLOCKS , STYLES , LINKS , TABLES , IMAGES . For example, sanitize_html(IMAGES) .

Using sanitize_html will include all parameters in the filter.

You can also include a STRIP parameter to strip all HTML. All content is run through escape_jinjava as well to prevent nested interpretation.

{% set escape_string = "This <br> <div>markup is <img src='test.com/image'> <span>printed</span> as text.</div>" %} {{ escape_string|sanitize_html("IMAGES") }} This markup is <img src="test.com/image"> printed as text.</div>
{% for content in contents|selectattr("post_list_summary_featured_image") %} <div class="post-item"> {% if content.post_list_summary_featured_image %} <div class="hs-featured-image-wrapper"> <a href="{{content.absolute_url}}" title="" class="hs-featured-image-link"> <img src="{{ content.post_list_summary_featured_image }}" class="hs-featured-image"> {% endif %} {{ content.post_list_content|safe }} {% endfor %} <div class="post-item"> <div class="hs-featured-image-wrapper"> <a href="http://blog.hubspot.com/marketing/how-to-get-a-job" title="" class="hs-featured-image-link"> <img src="//cdn2.hubspot.net/hub/53/hubfs/00-Blog-Related_Images/landing-a-job-featured-image.png?t=1431452322770&width=761" class="hs-featured-image"> Post with featured image

Please note: when using this filter, the page will be prerendered periodically rather than every time the page content is updated. This means that the filtered content will not be updated on every page reload.

This may not be an issue for certain types of content, such as displaying a random list of blog posts. However, if you need content to change randomly on every page load, you should instead use JavaScript to randomize the content client-side.

{% for content in contents|shuffle %} <div class="post-item">Markup of each post</div> {% endfor %} <div class="post-item">Markup of each post 5</div> <div class="post-item">Markup of each post 3</div> <div class="post-item">Markup of each post 1</div> <div class="post-item">Markup of each post 2</div> <div class="post-item">Markup of each post 4</div>
{% set items = ["laptops", "tablets", "smartphones", "smart watches", "TVs"] %} <div class="columwrapper"> {% for column in items|slice(3," ") %} <ul class="column-{{ loop.index }}"> {% for item in column %} <li>{{ item }}</li> {% endfor %} {% endfor %} </div> <div class="columwrapper"> <ul class="column-1"> <li>laptops</li> <li>tablets</li> <li>smartphones</li> <ul class="column-2"> <li>smart watches</li> <li>TVs</li> <li>&nbsp;</li>

Sorts an iterable. This filter requires all parameters to sort by an attribute in HubSpot. The first parameter is a boolean to reverse the sort order. The second parameter determines whether or not the sorting is case sensitive. And the final parameter specifies an attribute to sort by. In the example below, posts from a blog are rendered and alphabetized by name.

{% set my_posts = blog_recent_posts("default", limit=5) %} {% for item in my_posts|sort(False, False, "name") %} {{ item.name }}<br> {% endfor %} A post<br> B post<br> C post<br> D post<br> E post<br>

Splits the input string into a list on the given separator. The first parameter specifies the separator to split the variable by. The second parameter determines how many times the variable should be split. Any remaining items would remained group. In the example below, a string of names is split at the ";" for the first 4 names.

{% set string_to_split = "Stephen; David; Cait; Nancy; Mike; Joe; Niall; Tim; Amanda" %} {% set names = string_to_split|split(";", 4) %} {% for name in names %} <li>{{ name }}</li> {% endfor %} </ul> <ul> <li>Stephen</li> <li>David</li> <li>Cait</li> <li>Nancy; Mike; Joe; Niall; Tim; Amanda</li>
{% set sum_this = [1, 2, 3, 4, 5] %} {{ sum_this|sum }} Total: {{ items|sum(attribute="price:") }} 15 Total: 20
{% text "my_title" label="Enter a title", value="My title should be titlecase", export_to_template_context=True %} {{ widget_data.my_title.value|title }} My Title Should Be Titlecase
"slug":"sample-user", "jsonBody":{ "avatar":"https://app.hubspot.com/settings/avatar/109d6874a0cb066c1c7263ac5df6ce7a", "bio":"Sample Bio", "facebook":"", "linkedin":"", "twitter":"", "website":"https://www.hubspot.com" "bio":"Sample Bio", "facebook":"", "linkedin":"", "avatar":"https://app.hubspot.com/settings/avatar/109d6874a0cb066c1c7263ac5df6ce7a", "gravatarUrl":"https://app.hubspot.com/settings/avatar/108bb5ac667ded34796271437dfe8d58", "twitterUsername":"", "hasSocialProfiles":false, "website":"https://www.hubspot.com", "twitter":"", "displayName":"Sample User"

Cuts off text after a certain number of characters. The default is 255. Please note that HTML characters are included in this count. The length is specified with the first parameter which defaults to 255. If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. If the text was in fact truncated it will append an ellipsis sign ("..."). If you want a different ellipsis sign than "..." you can specify it using the third parameter.

{{ "I only want to show the first sentence. Not the second."|truncate(40) }} {{ "I only want to show the first sentence. Not the second."|truncate(35, True, "..........") }} I only want to show the first sentence. I only want to show the first sente..........

Truncates a given string, respecting html markup (i.e. will properly close all nested tags). This will prevent a tag from remaining open after truncation. HTML characters do not count towards the character total. This filter has a length parameter and a truncation symbol parameter. There is a third boolean parameter that specifies whether words will be broken at length. This parameter is false by default in order to preserve the length of words. If using only one of the optional parameters, use keyword arguments, such as truncatehtml(70, breakwords = false).

{% text "encode" value="Escape & URL encode this string", label="Enter slug", export_to_template_context=True %} {{ widget_data.encode.value|urlencode }} Escape+%26+URL+encode+this+string
{% text "decode" value="Escape+%26+URL+decode+this+string", label="Enter slug", export_to_template_context=True %} {{ widget_data.decode.value|urldecode }} Escape & URL encode this string
{{ "http://hubspot.com/"|urlize }} {{ "http://hubspot.com/"|urlize(10,true) }} {{ "http://hubspot.com/"|urlize("",true) }} {{ "http://hubspot.com/"|urlize("",false,target="_blank") }} <a href="//hubspot.com/">http://hubspot.com/</a> <a href="//hubspot.com/" rel="nofollow">http://...</a> <a href="//hubspot.com/" rel="nofollow">http://hubspot.com/</a> <a href="//hubspot.com/" target="_blank">http://hubspot.com/</a>
{% set wrap_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam efficitur, ipsum non sagittis euismod, ex risus rhoncus lectus, vel maximus leo enim sit amet dui. Ut laoreet ultricies quam at fermentum." %} {{ wrap_text|wordwrap(10) }} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam efficitur, ipsum non sagittis euismod, ex risus rhoncus lectus, maximus leo enim sit amet dui. Ut laoreet ultricies quam at fermentum.
This form is used for documentation feedback only. Learn how to get help with HubSpot .