Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Page with or without summary

Started by Muut Archive 10 years ago · 13 replies · 933 views
10 years ago

I have posts with summary and post without. Sometimes the summary is the same as the content begins.
In the post detail template I cannot use
{% set content = page.content|slice(page.summary|length) %}
because, if the page has not summary, the first part of content would cut.
How can I know if a page has or has not the summary?

10 years ago

You can check page.summary != page.content

10 years ago

Sorry, maybe I'm stupid or confusing, but I don't be able to show only content (without summary specified in markdown, before summary separator ===) of my pages considering that I have 2 cases: content without summary and content with summary. Was I able to esplain?

10 years ago

Please make sure you have a blank line before and after the line with the summary delimiter, as described in this post. If you still have a problem try to explain it in a different way and include as many code as you can in your question. Hopefully with that we can help you further.

10 years ago

This is my md files:

first md: there is a summary

TXT
Summary of news 1

===

Summary of news 1. (there could be the same text of the summary)
Content of news 1.  
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.,...  

second .md: there is no summary

TXT
Content of news 2.  
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...

How do I do to view only content without summary?

Thank you.

10 years ago

You have to manually splice the content. Just ignore the first part of content that's the same length as the summary. In PHP, it would be something like substr($page->content, strlen($page->summary)).

10 years ago

No, in second .md, where there is no summary, if I use page.content|slice(page.summary|length), the first part of content is truncated.

10 years ago

I cannot slice summary because some pages have no summary diveder in markdown file.
If summary_size property of the Page class were public, I would be able to know if there is summary in markdown (the part of text before === delimiter).

10 years ago

@samuele, Please check your messages, see the message button. Please test that solution. If it helps we can let everyone know about it.

10 years ago

The solution was tested and works. The recipe goes like this.

  • Create a plugin named "HasSummary" by following steps 1 and 2 of the Plugin Tutorial.
  • Replace the function onPageContentRaw() by the following:

    PHP
    
    public function onPageContentRaw(Event $e)
    {
    
    // Get the current raw content
    $raw = $e['page']->getRawContent();
    
    $summaryseparator = '===';
    $summaryseparatorpos = mb_strpos($raw, $summaryseparator);
    
    if ($summaryseparatorpos) {
        $this->config->set('plugins.hassummary.result', 'true');
    }
    else {
        $this->config->set('plugins.hassummary.result', 'false');
    }
    

}

TWIG
This function sets the configuration variable `config.plugins.hassummary.result`to `true` if the page has a summary. If not the variable will be set to `false`.

You can then use this variable in Twig like any other page header / frontmatter / configuration / YAML variable (sorry, but many terms are being used for the same thing).

To print the result use `{{ config.plugins.hassummary.result }}` or use the result in a conditional statement like:
```{% if config.plugins.hassummary.result == 'true' %}
Yeah!
{% else %}
Nope.
{% endif %}
10 years ago

I'd still suggest creating an issue in the main repo. This really shouldn't require a custom plugin to solve.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago