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?
You can check page.summary != page.content
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?
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.
This is my md files:
first md: there is a summary
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
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.
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))
.
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.
Might this recipe “Displaying page content without summary” be the answer you are looking for?
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).
Seems to me the real problem is that the summary
function returns the full page content if there is no explicit summary. While changing this would harm backwards compatibility, returning nothing if there is no explicit summary makes much more sense. I’d suggest filing an issue and seeing what they say about it.
@samuele, Please check your messages, see the message button. Please test that solution. If it helps we can let everyone know about it.
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:
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');
}
}
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:
Yeah!
{% else %}
Nope.
{% endif %}
I’d still suggest creating an issue in the main repo. This really shouldn’t require a custom plugin to solve.
Yup, please add this issue here https://github.com/getgrav/grav/issues