Posted in Code on August 25th, 2014

Sculpin "Read More" Links

I've gotten more than one question on how I manage the "read more" buttons (more accurately "continue" in my case) in my lengthier posts with Sculpin¹.

The approach is pretty straight forward, leveraging an html comment and Twig's split filter, like so:


{% if '<!--more-->' in post.blocks.content %}
    {{ post.blocks.content | split('<!--more-->') | first | raw }}
    <a href='{​{ post.url }​}' class='continue'> continue </a>
{% else %}
    {{ post.blocks.content | raw }}
{% endif %}

If the post body contains the html comment <!--more-->, the post body is split on that breakpoint and only the first element of the returned array is displayed as the excerpt (along with the "continue" button which links to the full post), otherwise the entire post body is displayed.



¹ If you're following along closely, you'll notice that I've made the switch from Jekyll to Sculpin. The combination of categories, redirects (it took me a while to settle on a url structure) and previous/next links with pagination weren't playing nicely together with Jekyll. I'm happy to report that I've had nothing but smooth sailing with Sculpin, a great project from my friend Beau Simensen.
 
 
Applies to:
  • Twig 1.10.3+