Media & imagery
Medias can be added to products by merchants, such as images, videos, and YouTube videos. In this tutorial, you will learn how to support product media in your theme.
Resources
- The
media
attribute of the product object - Media helper
Implementing Product Media
Product media is usually shown on the product page. However, you might want to show product media in other parts of the theme, so it's helpful to build the media display in a snippet
for reuse.
To show product media, you might loop through the media
attribute of the product
object and use the related media helper
based on the media type.
Example
If you want to display product media on the product page, and your product page content is hosted in a product.html
section, you might do the following:
- Create a
media.html
snippet to host your media display. - Render
media.html
in yourproduct.html
section.
<!-- sections/product.html -->
<!-- snippets/media.html -->
<div style="padding-top: %;" data-media-id="">
</div>
<div class="product-media-item"
style="padding-top: %;"
data-media-id=""
>
</div>
<div class="product-media-item" data-media-id="">
</div>
<div class="product-media-item"
style="padding-top: 100%;"
data-media-id=""
>
</div>
In the example above, each media type is wrapped in a <div>
element with custom and data attributes.
For another example of supporting media in themes, you can refer to Seed's implementation in the main-product.html
section and the product-thumbnail.html
snippet.
Using Media Preview Images
Each media type has a preview_image
attribute. This can be useful in the following scenarios:
Using the image_url
helper to the media object returns the preview_image
URL.
Product Thumbnails
If your theme displays thumbnails for each media source on a product, you can use the preview_image
attribute of the media object to show each media source's thumbnail.
For example:
<div class="thumbnails-box">
<a data-thumbnail-id="">
</a>
</div>
The above example adds an attribute designed to work with the data-thumbnail-id
attribute included in the general media loop example above. The data-media-id
attribute provides a simple way to associate thumbnails with their corresponding media display.
Using Parameters to Control Video Features
SHOPLINE-hosted videos can set all HTML5 video attributes when rendered with the Handlebars video_tag
or media_tag
helper. For example:
controls
- Allow users to control video playback.autoplay
- Automatically play the video after loading.muted
- Mute the video's audio.loop
- Continuously loop the video.
Each parameter is not injected by default, but you can set them to take effect as shown below:
<!-- Autoplay video -->
<!-- Loop video -->
<!-- Mute and loop video -->
<!-- Mute, loop video, and allow user control -->
You can use the external_video_url
helper to control the same behaviors for externally hosted videos. However, available parameters depend on the video site.
Error loading component.