How to avoid repeated code usage in jekyll

I have following code which will repeat hundreds of times in a post.

<a download="image" href="https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/28468309_192592021334350_7146814669583302137_n.jpg?oh=83c432cff19cf9060c6407af1e3ac43b&oe=5B434DE5"><img class="lazyload card-img" onerror="this.style.display='none'" data-src="https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/28468309_192592021334350_7146814669583302137_n.jpg?oh=83c432cff19cf9060c6407af1e3ac43b&oe=5B434DE5" /></a><br><br>

Only link https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/28468309_192592021334350_7146814669583302137_n.jpg?oh=83c432cff19cf9060c6407af1e3ac43b&oe=5B434DE5
is changing. Can I avoid the repeatation and call the code each time?

By using Jekyll includes.

1 Like

Thanks @mmistakes for reply. I am newbie, can you give the correct code. The changing link repeats 2 times in the code

Have a read through how to use variables with includes for more help.

You’ll want to do something like:

<!-- _includes/your-include.html -->
<a download="image" href="{{ include.url }}"><img class="lazyload card-img" onerror="this.style.display='none'" data-src="{{ include.url }}" /></a><br><br>

And then you can call it into your page with:

{% include your-include.html url="https://scontent-sit4-1.xx.fbcdn.net/v/t1.0-9/28468309_192592021334350_7146814669583302137_n.jpg?oh=83c432cff19cf9060c6407af1e3ac43b&oe=5B434DE5" %}
2 Likes

Thank you very much @mmistakes. It worked. But it is more resource heavy so I can’t use it…