Is it possible to customize a footer template per-post?

My use case - I have a footer with a CTA in it. I want to be able to customize this CTA on a per-post basis, or use a default one if I don’t customize it. Rough example:

single.html:

<html>
<body>
<div class="container">
  <h1>
    {{.Title}}
  </h1>
  {{.Content}}
</div>
<div class="cta">
  {{ block "cta" . }}
  This is a default CTA
  {{ end }}
</div>
</body>
</html>

Then in my posts I’d like to write:

some-post.md:

My blog post here...

{{ define "cta" }}
Custom CTA
{{ end }}

This approach doesn’t work - you can’t define templates inside a markdown file - so I’m wondering, are there other ways to effectively do the same thing?

I could just put the CTA in a shortcode and call it in my posts, but then if I ever forget to call it I won’t have a default CTA and it is a bit more annoying to put that portion of the post outside of the container I use for my main content.

Any help would be awesome. Thanks!

I’m gonna put out an example of how I would do it, and you see which parts you like or have questions about.

I’d base the CTA on front matter. Then, in your templates you also have that default CTA, and conditionally show something else based on your parameters.

I don’t know what a CTA is for you. If it is just some hypertext, I’d probably just make that a line in front matter, like so:

cta: "Don't be a lozer, [join our cult today](https://example.com/phish.php?redirect=localhost)!"

If it is more complicated than that, well, maybe load conditional templates based on keywords or something.

Also…

If you really want to include the CTA via a shortcode, you can still have a separate CTA outside .Content, and then check for shortcode use. If your CTA shortcode is present, don’t create the default CTA, and so forth.

I personally don’t recommend that, unless you ensure your default CTA is included wherever your content is rendered, such as RSS feeds, otherwise your content has inconsistent CTAs sprinkled through them… ugh, that would keep me up at night! :slight_smile:

1 Like

Yeah, then it’s easy to use with to grab that front matter param.

1 Like

Thanks - I really appreciate the help :slight_smile:

The front matter solution sounds like it would work perfectly; I hadn’t thought about it despite using front matter for other parts of my page - like related articles - but it really is a perfect fit.

Let me take some time to set it up before asking any further questions, but I believe I know what I’m doing now and should be good. Thanks a ton!

1 Like