Is there a way to preserve liquid variables across pages during processing?

In the Jekyll theme I’m using, the post.excerpt variable seems to have an issue, it outputs the whole content instead of just first 50 words. Hence, I’m compelled to manually extract the 50 words and generate the excerpt out of the content like this:

	<div>
	{{ post.content | markdownify | strip_html | truncatewords: 50 }}
	<a href="{{ post.url }}" class="read-more">Read More</a>
	</div>

Since I have to do it at many places (index page of the blog, tags page, header include for the meta attribute, etc.), is there a shorter way so that I can assign this whole thing at one place and then it works automatically from there? I tried doing this but didn’t work:

{% assign short_content = ‘post.content | markdownify | strip_html | truncatewords: 50’ %}

Whatever variable I assign to in say head.html include part isn’t available for reference in other templates like thus:

{{ short_content }} // returns nothing

What is the right way to do this?

You can’t access something across pages like that unfortunately.

I think the excerpt may not work if you post starts with something other than a p tag? does yours start with an image or something?

seems hacky but maybe your code would work as an include and then pass in the post content as a parameter? not a great idea really.

The post doesn’t start with a <p>, it starts with a <h1>. Otherwise, how else am I supposed to show blog titles on the top of the post such as this one: Unpopular Opinion: Desktop GUI is the most efficient and fulfilling way of Human-Computer Interaction

The github source for my jekyll blog is here, it is in fact a fork of the widely used Hyde template.

Can you elaborate on that with an example or something? All I want to do is to turn this whole construct into a reusable function or something that I can put in the config.yml or any other place:

{{ post.content | markdownify | strip_html | truncatewords: 50 }}

I use the title in the front matter for that, then you can use that as the title of the page.
Descriptions I usually do manually the same way - front matter, then you can pull that in as the meta description.

I’ve not used the excerpt other than to play with it. There is also an excerpt separator you can use to specify what exactly the excerpt is.

As for how to use an include for what you want, I am not sure it would work but it might. Includes can have data passed in as a parameter and then in the include file you could so what you are doing with the truncate filter.