# Incrementing variable in every build

**URL:** <https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923>\
**Category:** Help\
**Created:** [February 13, 2020, 2:54pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923 "2020-02-13T14:54:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/arnab/32/1238_2.png) [@arnab](https://talk.jekyllrb.com/u/arnab)\
**Post date:** [February 13, 2020, 2:54pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923/1 "2020-02-13T14:54:49Z")

</div>

I am trying to increment a front matter variable in every build. Initially the front-matter would be -

```auto
---
layout: essay
title: "Steampunk Metal Engraving"
counter: 0
---

```

and after a build the counter will increment automatically.

```auto
---
layout: essay
title: "Steampunk Metal Engraving"
counter: 1
---

```

Therefore, during every build down the line -

```auto
---
layout: essay
title: "Steampunk Metal Engraving"
counter: {{ counter++ }}
---

```

I want this to happen every time a netlify form is submitted and CI triggers a build. I think liquid is not allowed inside front-matter section of a file. Is there a way I can make this work?

Any suggestions will be greatly appreciated. TIA.

---

<div class="post-metadata">

**Author:** ![mvogelsang](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mvogelsang](https://talk.jekyllrb.com/u/mvogelsang)\
**Post date:** [February 14, 2020, 2:34pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923/2 "2020-02-14T14:34:50Z")

</div>

If I understand you correctly then you can’t do it like that as far as I am aware. You build static pages with Jekyll and they will always be generated the same way from identical source files.  
So what you want to do is either manually edit the source file with some script or keep an extern database (or really just a file) that gets changed every time the site is built. For this you can use [Jekyll’s Hooks](https://jekyllrb.com/docs/plugins/hooks/) which are really just [plugins](https://jekyllrb.com/docs/plugins/) and are written in a [similar way](https://jekyllrb.com/docs/step-by-step/10-deployment/#plugins).

Then you can run a shell script from ruby as suggested [here](https://github.com/jekyll/jekyll/issues/5378#issuecomment-248124728) or just write the file editing in ruby if you feel you are up for the task.

---

<div class="post-metadata">

**Author:** ![mvogelsang](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mvogelsang](https://talk.jekyllrb.com/u/mvogelsang)\
**Post date:** [February 14, 2020, 3:00pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923/3 "2020-02-14T15:00:34Z")

</div>

I looked at a plugin I wrote a few months ago that does a similar thing. I’ll leave it here and explain what it does:

```auto
Jekyll::Hooks.register :site, :post_write do

  # checks whether or not the directory 'downloads' exists in the
  # target folder and creates it if necessary.

  downloads = "[-d '_site/downloads'] || ([-d '/path/to/original/downloads'] && ln -s /path/to/original/downloads _site/downloads)"
  %x( #{downloads} )

end

```

So what did I do here? `:site, :post_write` says “After the whole site was built and written into the target directory, do the thing that is written below”.  
In there I first define a linux shell oneliner as a string and execute that one afterwards using  
`%x( #{name_of_the_defined_command_string} )`. This should work for you, too. If it doesn’t, have a look at [the other options](https://jekyllrb.com/docs/plugins/hooks/) at the bottom of the page on hooks.  
Those two things can be done in the same line of course but in my opinion that hurts readability (which is already pretty bad here). `[-d]` just says if a directory exists and `ln -s` creates symbolic links. The rest is getting the logic straight.

For your problem you could look at [how to read and write from files](https://alvinalexander.com/linux-unix/linux-bash-script-how-to-read-write-files) (if you are using a unix system) and use that instead of my string. Also, create the file if it doesn’t exist yet and maybe write an initial 1 into it.  
You’ll _also_ also need to figure out if you want to increase the counter with shell or with ruby and you have to keep in mind that this is a file editing process, which could lead you to potential deadlocks or information loss should multiple people trigger the process at the same time on the same file. Lastly, if you work locally and edit the file locally, then push it onto some server where multiple users can use it, then the two files need to synch up and maybe add their numbers correctly. Maybe only execute that script on the server or execute it all the time but exclude the file from being pushed to the server.

My solution isn’t perfect but all these latter problems you’d need to tackle anyway should you want to solve it in a way like you initially suggested.

Edit: That hook file of mine just sits in the same `_plugins` directory as all the other plugins in case you were wondering. I was stuck at this for a while when I started writing hooks.

---

<div class="post-metadata">

**Author:** ![arnab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/arnab/32/1238_2.png) [@arnab](https://talk.jekyllrb.com/u/arnab)\
**Post date:** [February 19, 2020, 12:42pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923/4 "2020-02-19T12:42:56Z")

</div>

I will try to build on top of your example. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![mvogelsang](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mvogelsang](https://talk.jekyllrb.com/u/mvogelsang)\
**Post date:** [February 19, 2020, 4:05pm UTC](https://talk.jekyllrb.com/t/incrementing-variable-in-every-build/3923/5 "2020-02-19T16:05:59Z")

</div>

You are welcome 🙂  
Good luck!
