给出液体模板机车类

发布于 2024-11-24 07:25:35 字数 338 浏览 2 评论 0原文

我有以下液体标记:

{{ 'image.jpg' | theme_image_tag }}

它呈现为,

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

如何向其添加类或任何选项?我希望它的渲染效果如下:

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

我使用 Locomotive CMS 及其附带的液体。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情丝乱 2024-12-01 07:25:35

对于大多数控制,请考虑使用 theme_image_url 而不是 theme_image_tag。这是一个更详细的示例,应该可以满足您的需求。

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" />

战皆罪 2024-12-01 07:25:35

尽管文档没有明确说明这一点,但您可以将类添加到image_tagtheme_image_tag 过滤器如下所示:

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

更一般地说,您可以添加任意 HTML 属性。像这样的流式代码...

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

将生成如下 HTML:

<img src="image.png" id="foo" some_custom_attr="bar">

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">
ぃ弥猫深巷。 2024-12-01 07:25:35

如果您想自定义液体,可以考虑编辑位于 lib/locomotive/liquid/filters/html.rb 中的 html.rb 文件。

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

或者您甚至可以编辑当前的 theme_image_tag 过滤器。

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.

烟酒忠诚 2024-12-01 07:25:35

image_tag 和 theme_image_tag 之间的区别在于 image_tag 将为您提供后端上传的数据中的 url,而 theme_image_tag 是您从主题中获得的 URL。

两者都接受参数。

{{ '/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 %}

干杯,
霍里昂·格雷戈里

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

孤寂小茶 2024-12-01 07:25:35

输出过滤器从左到右运行,因此您也可以这样做:

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

Locomotive CMS 可能有自己的过滤器,但有关的代码应该通用。

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.

淡写薰衣草的香 2024-12-01 07:25:35

对我来说,以下工作有效

{% image "book.jpeg" alt="My book" class="book-123" %}

For me, the following worked

{% image "book.jpeg" alt="My book" class="book-123" %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文