Adding to <head> from an include?

I have a contact form contact.html in my _includes directory, and I use it in pages index.html, contact.html, and a few other places. I have recaptcha on the form, and I don’t want to load it for every page a user visits.

At the moment my contact page looks like this:

---
title: "Contact"
permalink: /contact
use_recaptcha: true
---

{% include contact.html %}

and I have this in my default layout:

{% if page.use_recaptcha %}
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
  function onSubmit(token) {
    document.getElementById("contact-form").submit();
  }
</script>
{% endif %}

It works, but is there any cleaner way to do it? Django’s templating language and jinja2 both have blocks, Blade templates have stacks, is there any equivalent for Jekyll or a design pattern that mimics it? Is what I’ve already done as close as I can get?

I’ve done that, works well for me. I’ve not used the other systems you mentioned so can’t comment on that part.