# Template macros/functions possible?

**URL:** <https://talk.jekyllrb.com/t/template-macros-functions-possible/8760>\
**Category:** Help\
**Created:** [December 11, 2023, 11:05pm UTC](https://talk.jekyllrb.com/t/template-macros-functions-possible/8760 "2023-12-11T23:05:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VillyVretthorn](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@VillyVretthorn](https://talk.jekyllrb.com/u/VillyVretthorn)\
**Post date:** [December 11, 2023, 11:05pm UTC](https://talk.jekyllrb.com/t/template-macros-functions-possible/8760/1 "2023-12-11T23:05:09Z")

</div>

I often find myself having to add certain types of content that requires HTML/CSS that is not possible to do in pure Markdown.

For example, I might want to add an image to a blog post like this:

```auto
<div class="text-center">
<img class="article-image" src="/assets/images/diagram.png">
<p class="text-muted text-center">Image:
By Joe Smith (CC BY-SA 3.0)
</p>
</div>

```

Of course, Jekyll allows me to just put this HTML into my markdown blog post. So that is all great and works.  
However, the problems start when I want to, say add a CSS class to the img tag. Then I have to go through all blog posts that have images in them and add the class to all instances.

Also having to add this code everywhere is error prone: I may forget to add some class in one post. Or I may make a typo in another post, etc.  
This causes things to get messy really fast.

So I was thinking if it is possible to create a small library of template macros or functions that I would be able to call.

Something like:

`{% blog_image(imgsrc="diagram.png", credit="Joe smith (CC BY-SA 3.0)") %}`

Which would then generate:

```auto
<div class="text-center">
<img class="article-image" src="{{ imgsrc }}">
<p class="text-muted text-center">Image:
By {{ credit }}
</p>
</div>

```

Is this possible?

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [December 12, 2023, 1:38am UTC](https://talk.jekyllrb.com/t/template-macros-functions-possible/8760/2 "2023-12-12T01:38:16Z")

</div>

Yes, you can use a Liquid include tag with parameters. See these docs on [how includes works in Jekyll](https://jekyllrb.com/docs/includes/). Example:

```
{% include blog_image.html imgsrc="diagram.png" credit="Joe smith" %}

```

---

<div class="post-metadata">

**Author:** ![VillyVretthorn](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@VillyVretthorn](https://talk.jekyllrb.com/u/VillyVretthorn)\
**Post date:** [December 15, 2023, 7:12am UTC](https://talk.jekyllrb.com/t/template-macros-functions-possible/8760/3 "2023-12-15T07:12:44Z")

</div>

This seems to work.

Is this a common pattern that people use in this way?  
Or is there a better way to do stuff like this?

---

<div class="post-metadata">

**Author:** ![michaelbach](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelbach/32/2151_2.png) [@michaelbach](https://talk.jekyllrb.com/u/michaelbach)\
**Post date:** [December 15, 2023, 7:42am UTC](https://talk.jekyllrb.com/t/template-macros-functions-possible/8760/4 "2023-12-15T07:42:34Z")

</div>

while the `include` syntax looks a bit wordy, it works **very well** for me. Whenever I repeat myself in code, I check whether it makes sense to generalise the stuff a bit and make it into an `include`. The default values for arguments are very handy there.  
As example, to make an include very general, I use the `file_exists` plugin, for instance to include images w/o specifying whether they are .png or .jpg or .webp.
