envygeeks / jekyll-assets

:art: Asset pipelines for Jekyll.
ISC License
1.12k stars 170 forks source link

Include Image Asset Using Liquid Variable #62

Closed semireg closed 10 years ago

semireg commented 10 years ago

Is there any way to pass a variable to an asset command?

{% assign badge_url = page_url | append: '/' | append: page_url | append: 'png' %}
{% image badge_url %}

Also, is there a way for an asset command to fail gracefully? For example, if the asset does not exist, it would be nice if it returned null. Another option would be to include another set of boolean commands such as image_exists, so this could be performed:

{% assign badge_url = page_url | append: '/' | append: page_url | append: 'png' %}
{% if image_exists badge_url %}
{% image badge_url %}
{% endif %}
semireg commented 10 years ago

Solution to question number 1 -- just pipe it to the image command.

{% assign badge_url = page_url | append: '/' | append: page_url | append: 'png' %}
{% badge_url | image %}
semireg commented 10 years ago

Solution to number 2 -- I used what amounts to a front-matter BOOL to control whether or not the image is loaded.

ixti commented 10 years ago

Heya,

I'm glad that you found solution. Solution (1) is absolutely legit. All liquid tags provided by jekyll assets are also available as liquid filters. Solution (2) is not available out of the box. But if it's very important/interesting it's possible to add {% asset_exist? %} liquid tag.

semireg commented 10 years ago

Here's another solution from Jekyll docs - http://jekyllrb.com/docs/templates/

ProTip™: Use variables as file name The name of the file you wish to embed can be literal (as in the example above), or you can use a variable, using liquid-like variable syntax as in {% include {{my_variable}} %}. Note that unlike usual liquid variable syntax, you cannot have spaces inside the curly braces.

chrishough commented 10 years ago

Sorry to reopen this, but any thoughts on this issue:

{% assign post_image = post.image_thumbnail_500x500 | 500x500.gif %}
{% image {{post_image}} %}

No matter what I try, it will not access the variable?

ixti commented 9 years ago

Sorry to reply with such a delay. But I didn't mentioned your last comment. The issue is pretty common in liquid:

Liquid does not interpolates liquid inside liquid

So instead of {% image {{some_var}} %} you have to use filter helper: {{ some_var | image }}. Although I'm not sure what you mean with:

{% assign post_image = post.image_thumbnail_500x500 | 500x500.gif %}
chrishough commented 9 years ago

Np, thanks man.

phillipadsmith commented 9 years ago

@ixti Tried the approaches described above, but I'm hitting this (old and oddly familiar) issue: https://github.com/jekyll-assets/jekyll-assets/issues/121#issuecomment-95358660

Any help appreciated.

Phillip.

phillipadsmith commented 9 years ago

Hmmm... actually {{ 'file_name' | asset_path }} works (no variable, using single quotes around the file name), but {{ file_name | asset_path }} does not (where file_name is an assigned variable).

Thoughts on this?

chrishough commented 9 years ago

@phillipadsmith sorry, I am not much help on this, will have to defer to others. I moved to Middleman.

phillipadsmith commented 9 years ago

@ixti Actually, I figured it out. Never mind. Bad quoting on my part. :(