Show excerpt for all topics via a theme

(Joe) #1

Continuing the discussion from Show "topic-excerpt" for all post on Latest Page:

I am interested in showing an excerpt for all topics.

The linked topic above covers the subject very well.

And @angus left a very helpful reply describing how to achieve what I’m after.

However, this is all under plugin structure.

My question is, is it possible to turn on or enable excerpts for all posts via a theme?

In other words, I would like to change:


def include_excerpt?

via a theme component.

I know that @sam mentioned this:

But figured I should ask in case things have changed since.

My use case is a modified topic list template that looks like this:

Notice how only a pinned topic would show an excerpt while I’d like all of them to do that.

Show "topic-excerpt" for all post on Latest Page
(Angus McLeod) #2

It’s not possible via a theme. But you could just make a simple plugin that does this.


# name: post-excerpts
# about: All posts have excerpts
# version: 0.1
# authors: Huckleberry Finn

after_initialize do
  add_to_serializer(:listable_topic, :include_excerpt?) { true }

That’s the whole plugin (untested, but it should work).

(Carlo) #3

Hi Angus.
I’d like to add excerpt in the default json feed of discourse ( latest and top ), for each topics.

Can this plugin do the job ? Sorry if my question sounds dumb :roll_eyes:

(Angus McLeod) #4

Yup, those 3 lines should be enough to add excerpts to those json endpoints.

(Joe) #5

Amazing as always @angus!

Thank you so much for the detailed answer :grin:

If it really only comes down to a plugin, then me proceeding with this would be pointless as the Topic List Previews plugin does a much better job and with a lot more configurable settings.

My hope was to create a theme component that modifies the topic list to show a thumbnail and an excerpt for all posts - applies to all categories.

I got everything ready except the excerpt.

(Angus McLeod) #6

Well, maybe not pointless. The TLP plugin is big and a bit unwieldy. A theme + a 3 line plugin, may be a more lightweight solution to the specific problem you’re trying to solve.

(Joe) #7

Of course, you’re right. :sunflower:

It’s just that I was hoping to fit in that small gap where you just want show a preview for all topics without requiring a plugin.

The only reason I lean towards a theme here is ease of access and not performance. I use the TLP and performance was never an issue :wine_glass:


I followed your advice and packaged it as a plugin.

In case anyone is interested:

Does it work? You bet :grin:

I’d be happy to release the cleaned up - still not finished - template as a standalone theme if this ever becomes possible.

(Carlo) #8

You’re my hero! Thanks! I also confirm it works 100%

(magoz) #9

That’s awesome, thanks!!
My only issue is that is no longer possible to control the excerpt length via Settings > post excerpt maxlength. Changing the value has no effect anymore.

Or, at least, it’s not working for me.

Does it work for you @Johani? :thinking:

(Joe) #10

Yeah, I remember running into this as well.

(I’m going from memory here)

The setting still works but you need to rebake the posts to see the changes.

Something like this:

If that doesn’t work, let me know and I will look further.

(magoz) #11

I’ve tried using:

I also rebuilt the app.
But no luck, it doesn’t work.

To display the excerpts I’m using this script in the </head:

<script type='text/x-handlebars' data-template-name='list/topic-list-item.raw'>
  (more code)

  {{#if topic.hasExcerpt}}
    <div class="topic-excerpt">
       {{{dir-span topic.escapedExcerpt}}}

  (more code)

Thanks for your help @Johani!

(magoz) #12

As a temporary solution, I’m limiting the height of the excerpt in a way that only shows two lines.
Not ideal, but works more or less okay.

Here is the css code, in case someone else is facing the same issue:

.topic-list .topic-excerpt {
    height: 3.4rem;
    overflow: hidden;