Give Liquid-templated <img> a class in Locomotive
Asked Answered
D

6

6

I have the following liquid markup:

{{ 'image.jpg' | theme_image_tag }}

and it renders like,

<img src="/site.com/site/3424242/image.jpg" />

How to add a class or whatever option to it? I want it to render like:

<img src="/site.com/site/3424242/image.jpg" class="thumbnail" />

I use the Locomotive CMS and the liquid that comes with it.

Decrease answered 16/7, 2011 at 14:1 Comment(0)
Z
7

For the most control, consider using theme_image_url instead of theme_image_tag. Here's a more detailed example that should get you want you want.

<img src="{{ 'image.jpg' | theme_image_url }}" class="thumbnail" />

Zoilazoilla answered 2/9, 2011 at 3:45 Comment(0)
C
4

Although the docs don't make this clear, you can add classes to the image_tag or theme_image_tag filters like this:

{{ "image.png" | image_tag: "class:image" }}

More generally, you can add arbitrary HTML attributes. Liquid code like this...

{{ "image.png" | image_tag: "id:foo", "some_custom_attr:bar" }}

will produce HTML like this:

<img src="image.png" id="foo" some_custom_attr="bar">
Comparison answered 10/8, 2011 at 13:19 Comment(0)
P
2

If you want to customize your liquid, you could consider editing the html.rb file located in lib/locomotive/liquid/filters/html.rb .

    def my_custom_tag (input,*args)
      "<img src=\"#{theme_image_url(input)}\" class=\"#{args.first}\" />"
    end

Or you can even edit the current theme_image_tag filter.

Papaw answered 10/12, 2011 at 13:28 Comment(0)
O
1

the difference between image_tag and theme_image_tag is taht image_tag will give you the url from the data you have upload in your backend and theme_image_tag is the one you get form your theme.

both accept params.

{{ '/public/images/exemple.jpg' | theme_image_tag:'class: c1,c2'}}

{% for blog_post in contents.blog_posts%}
 {{ blog_post.image.url | image_tag, 'alt:my beautyfull image', 'id:exemple'}}
{% endfor %}

cheers, Horion grégory

Oneupmanship answered 8/2, 2012 at 17:23 Comment(0)
P
0

Output filters run from left to right, so you could also do this:

{{ 'image.jpg' | theme_image_tag | replace_first' />',' class="thumbnail" />' }}

Locomotive CMS may have its own filters, but the code about should work universally.

Persecution answered 5/9, 2012 at 22:54 Comment(1)
This seems needlessly hacky when there are clean solutions for this exact problem built into Liquid, as described by the other answers here. It also depends upon image_tag and similar filters generating a tag that has a / before the > (not required in HTML 5) and a space before the / (not required except for compatibility with Netscape 4 and older browsers); at least the latter of these points feels like an implementation detail in Liquid that the devs would think little of changing in future. I'd worry that relying on this could be fragile.Sorce
T
0

For me, the following worked

{% image "book.jpeg" alt="My book" class="book-123" %}
Teem answered 17/7, 2015 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.