# HTML filters

HTML filters create HTML elements from Liquid properties or from the shop's assets.

HTML filters create HTML elements from Liquid properties or from the shop's assets.

## image_tag

Creates an image tag. You can adjust the [aspect ratio](#aspect-ratio) or the [sizing behavior](#responsive-images-and-sizing) of the image, [preload the image](#performance), [add alt text](#alt-text) or pass [additional HTML attributes](#additional-attributes) to the tag.

The input of this filter must be an [image_url](https://developer.versacommerce.de/articles/liquid-filters-url-filters) with a `width` or `height` keyword argument.

```liquid
{{ settings.favicon | image_url: width: 2000 | image_tag }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" srcset="
	//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=352 352w,//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=832 832w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1200 1200w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1920 1920w" width="2000" height="2007">
```

## Aspect ratio

Based on the dimensions given in the `image_url` filter and the aspect ratio of the image, `width` and `height` are added to the tag automatically. If you set one of these attributes, only the attribute you set is used.

```liquid
 <!-- width is set to nil -->

{{ settings.favicon | image_url: width: 2000 | image_tag: width: nil }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" srcset="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=352 352w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=832 832w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1200 1200w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1920 1920w">
```

```liquid
 <!-- only the width attribute is specified -->

{{ settings.favicon | image_url: width: 2000 | image_tag: width: 200 }}
```

```text
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" srcset="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=352 352w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=832 832w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1200 1200w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1920 1920w" width="200">
```

## Responsive images and sizing

`image_tag` returns a responsive image by default. A `srcset` is generated automatically with a sensible default set of widths up to the width given in the [image_url](https://developer.versacommerce.de/articles/liquid-filters-url-filters) filter. You can adjust or override this behavior with these keyword arguments:

- `sizes`
- `widths`
- `srcset`

Set the source sizes with the `sizes` keyword argument. It uses the same syntax as the HTML `sizes` attribute.

```liquid
{{ settings.favicon | image_url: width: 200 | image_tag: sizes: '(min-width: 1100px) 535px, (min-width: 750px) calc((100vw - 130px) / 2), calc((100vw - 50px) / 2)' }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" srcset="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=352 352w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=832 832w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1200 1200w,
          //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=1920 1920w" sizes="(min-width: 1100px) 535px, (min-width: 750px) calc((100vw - 130px) / 2), calc((100vw - 50px) / 2)" width="2000" height="2007">
```

Although a srcset is generated for every `image_tag`, you can set your own widths with the `widths` keyword argument. It takes a string of comma-separated integer widths.

```liquid
{{ settings.favicon | image_url: width: 2000 | image_tag: widths: '200, 300, 400' }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" srcset="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=200 200w,
         //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=300 300w,
         //cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=400 400w" width="2000" height="2007">
```

You can also pass a custom `srcset` keyword argument. `srcset` takes precedence over `widths`. We do not recommend a custom `srcset` unless you want to remove the attribute completely by setting it to `nil`.

```liquid
{{ settings.favicon | image_url: width: 2000 | image_tag: srcset: nil }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=2000" alt="" width="2000" height="2007">
```

## Performance

`image_tag` accepts a `preload` keyword argument to mark images you want to preload.

If `preload` is true, a resource hint for the image is sent as a Link header with a `rel` value of `preload`. The Link header also contains `imagesrcset` and `imagesizes` matching the `srcset` and `sizes` attributes of the image tag, if present.

Use this parameter sparingly. For example, preload only images that appear above the fold.

```liquid
{{ settings.favicon | image_url: width: 200 | image_tag: preload: true }}
{{ settings.favicon | image_url: width: 400 | image_tag: sizes: '40vw', preload: true }}
```

```text
Link: {{ ... other link hints ... }},
<//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=200>; rel=preload; as=image

Link: {{ ... other link hints ... }},
<//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=400>; rel=preload; as=image; imagesrcset=//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=352 352w; imagesizes=40vw
```

You can also lazy-load an image by adding `loading: 'lazy'` as an additional attribute.

## Alt text

Use the `alt` argument to set alt text for the image.

If no value is given, the alt text of the media is used. If the image has no description, the value defaults to an empty string.

## Additional attributes

All additional HTML attributes passed to this filter are added to the resulting HTML tag.

```liquid
<!-- Adding CSS classes to the image -->

{{ settings.favicon | image_url: width: 200 | image_tag: alt: 'My favicon', srcset: nil, class: 'css-class-1 css-class-2' }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=200" alt="My favicon" class="css-class-1 css-class-2" width="200" height="200">
```

```liquid
<!-- Lazy-loading the image -->

{{ settings.favicon | image_url: width: 200 | image_tag: srcset: nil, loading: 'lazy' }}
```

```html
<img src="//cdn.versacommerce.de/s/files/1/0251/7476/9720/files/myfavicon.png?v=1580676830&width=200" width="200" height="200" loading="lazy">
```

## payment_button

Outputs a buy-now button for a product form. Apply the filter to the `form` object inside a product form.

```liquid
{{ form | payment_button }}
```

```html
<div data-shopify="payment-button" class="shopify-payment-button">
  <button type="button" data-variant-id="123" class="shopify-payment-button__button shopify-payment-button__button--unbranded">
    Buy now
  </button>
</div>
```

In VersaCommerce this is a neutral button without payment provider branding. A click adds the selected variant to the cart and goes straight to the VersaCommerce checkout. If the variant tracks inventory and is out of stock, the button is disabled. The `shopify-payment-button` classes are kept for theme compatibility.

## payment_terms

Available in VersaCommerce so that themes run without errors. The filter currently always returns an empty string (no instalment banner).

```liquid
{{ form | payment_terms }}
```

## payment_type_svg_tag

Returns the `<svg>` tag of the requested payment type image for inlining. Use it with the [shop.enabled_payment_types](https://developer.versacommerce.de/articles/liquid-objects-shop#shop-enabledpaymenttypes) variable. Accepts an optional `class` attribute that is applied to the `<svg>` tag to style the icon.

```liquid
{% for type in shop.enabled_payment_types %}
  {{ type | payment_type_svg_tag: class: 'custom-class' }}
{% endfor %}
```

```html
 <!-- If the shop accepts Mastercard and Discover -->

 <svg class="custom-class" xmlns="http://www.w3.org/2000/svg">

  <circle fill="#EB001B" cx="15" cy="12" r="7"></circle>

  <circle fill="#F79E1B" cx="23" cy="12" r="7"></circle>
  ...

 </svg>

 <svg class="custom-class" xmlns="http://www.w3.org/2000/svg">

  <path fill="#494949" d="M9 11h20v2H9z"></path>
  ...

 </svg>
```

## preload_tag

Creates a `<link>` tag with a `rel` attribute of `preload` to prioritize loading a specific asset hosted by VersaCommerce. The asset URL is also added to the Link header with a `rel` attribute of `preload`.

Use this filter sparingly. For example, preload only resources needed to render above-the-fold content.

To preload a stylesheet, use `stylesheet_tag`. To preload an image, use `image_tag`.

The input must be the URL of a theme or shop asset. The filter requires an `as` keyword argument, which is passed as the `as` attribute.

```liquid
{{ 'some_font.woff' | asset_url | preload_tag: as: 'font' }}
```

```html
 <link href="//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/some_font.woff?28178" rel="preload" as="font">
```

```text
Link:
<//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/some_font.woff?28178>; rel="preload"; as="font"
```

The filter sets the rel attribute to preload automatically, so you cannot set your own value.

You can add more [link attributes](https://developer.versacommerce.de/articles/liquid-objects-link) to the HTML tag and the Link header by passing them as keyword arguments. Their values are not validated.

```liquid
{{ 'mobile_font.woff' | asset_url | preload_tag: as: 'font', media: 'mobile', type: 'font/woff' }}
```

```html
 <link href="//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/mobile_font.woff?28178" rel="preload" as="font" media="mobile" type="font/woff">
```

## script_tag

Creates a script tag.

```liquid
{{ 'shop.js' | asset_url | script_tag }}
```

```html
<script src="//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/shop.js?28178" type="text/javascript"></script>
```

## stylesheet_tag

Creates a stylesheet tag.

```liquid
{{ 'shop.css' | asset_url | stylesheet_tag }}
```

```html
<link href="//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/shop.css?28178" rel="stylesheet" type="text/css" media="all" />
```

`stylesheet_tag` accepts these parameters:

| Parameter | Description |
|---|---|
| `media` | Sets the [media](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-media) attribute of the link element. The stylesheet is then only loaded if the media condition is true. |
| `preload` | If `true`, a resource hint for the stylesheet is sent as a [Link header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) with a `rel` value of [preload](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload). Use it sparingly, for example only for render-blocking stylesheets needed for above-the-fold content. |

```liquid
{{ 'mobile.css' | asset_url | stylesheet_tag: media: 'screen and (max-width: 600px)', preload: true }}
```

```html
<link href="//cdn.versacommerce.de/s/files/1/0087/0462/t/394/assets/mobile.css?28178" rel="stylesheet" media="screen and (max-width: 600px)" />
```

## time_tag

Converts a timestamp into a `<time>` tag.

```liquid
{{ article.published_at | time_tag }}
```

```html
<time datetime="2018-12-31T18:00:00Z">Monday, December 31, 2018 at 1:00 pm -0500</time>
```

`time_tag` accepts the same parameters as Ruby's `strftime` method. See the Ruby documentation or a site like [strfti.me](https://strfti.me) for the format codes. These parameters do not affect the `datetime` attribute of the `<time>` tag.

```liquid
{{ article.published_at | time_tag: '%a, %b %d, %Y' }}
```

```html
<time datetime="2018-12-31T18:00:00Z">Mon, Dec 31, 2018</time>
```

## format

Pass a `format` parameter to `time_tag` to output a date format for the storefront language. The available formats are:

- `abbreviated_date`
- `basic`
- `date`
- `date_at_time`
- `default`
- `on_date`

```liquid
{{ article.published_at | time_tag: format: 'date' }}
```

```html
<time datetime="2018-12-31T18:00:00Z">December 31, 2018</time>
```

The table previews the `format` options in different languages.

| `format` | English | French | Japanese |
|---|---|---|---|
| `abbreviated_date` | Dec 31, 2018 | 31 déc 2018 | 2018年12月31日 |
| `basic` | 12/31/2018 | 31/12/2018 | 2018/12/31 |
| `date` | December 31, 2018 | 31 décembre 2018 | 2018年12月31日 |
| `date_at_time` | December 31, 2018 at 1:00 pm | 31 décembre 2018 à 13:00 | 2018年12月31日 13:00 |
| `default` | Monday, December 31, 2018 at 1:00 pm -0500 | lundi 31 décembre 2018 à 13:00 -0500 | 2018年12月31日(月曜日) 13:00 -0500 |
| `on_date` | on Dec 31, 2018 | le 31 déc 2018 | 2018年12月31日で |

## datetime

Pass a `datetime` parameter with strftime format codes to use a custom format for the `datetime` attribute of the `<time>` tag.

```liquid
{{ article.published_at | time_tag: '%a, %b %d, %Y', datetime: '%Y-%m-%d' }}
```

```html
<time datetime="2016-02-24">Wed, 24 Feb, 2016</time>
```

## Setting format options in locale files

You can also define date formats in your theme's locale files:

```json
"date_formats": {
  "month_day_year": "%B %d, %Y"
}
```

```liquid
{{ article.published_at | time_tag: format: 'month_day_year' }}
```

```html
<time datetime="2016-02-24T14:47:51Z">February 24, 2016</time>
```
