We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Excerpt

Development and Programming

Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

This plugin has been built up from an earlier plugin, with the plan on being an awful lot more efficient and more reliable.

This plugin is designed to be wrapped around the {full_text} variable on your search results template and will return relevant ‘chunks’ of text containing your search terms. Think Google search results.

Parameters:

wrap=”“

The html tag name (don’t include the < > parts) to wrap around each highlighted word. The default value is “strong”.

chunk_wrap=”“

The html tag name (don’t include the < > parts) to wrap around each relevant ‘chunk’ extracted from the entire {full_text} value. The default value is “span”. If supplied with “none”, nothing will be wrapped around each chunk.

chunk_prepend=”“

Data to prepend to each individual chunk returned. This data will be inserted inside the chunk_wrap tags.

chunk_append=”“

Like the chunk_prepend parameter, but this inserts the data immediately before the closing chunk_wrap tag. Defaults to ‘… ‘.

output_wrap=”“

The html tag name (don’t include the < > parts) to wrap around the entire output. Defaults to nothing.

output_prepend=”“

Data to insert in the output, before any chunks but after the opening output_wrap tag, if supplied.

output_append=”“

Like the output_prepend parameter, but this will insert the data immediately before the closing output_wrap tag if supplied.

pre_chars=”“

The number of characters to include before the highlighted term. Regardless of given value, the pointer will not dissect whole words. The default value is “50”. If given a value such as “50-100” it will choose a random value between 50 and 100.

post_chars=”“

The number of characters to include after the hightlighted term. Regardless of given value, the pointer will not dissect whole words. The default value is “50”. If given a value such as “50-100” it will choose a random value between 50 and 100.

chunks=”“

The number of relevant chunks to return. Values can be numeric or “all”. The default value is “3”. Setting this parameter to all will return all relevant chunks found.

keywords=”“

Instead of acting dynamically, the plugin can be given the search term, or any term, via this parameter.

sort=”“

Sort the chunks ascendingly or descendingly from the order in which they were found. values are “asc” or “desc”. Default is “asc”.

order=”“

Order the chunks. Currently this parameter accepts only “random”.

Example usage:

(on a search results template)

{exp:search:search_results}
<h2><a href="http://{auto_path}" title="{title}">{title}</a></h2>
{exp:zm_excerpt output_wrap="p" output_prepend="<dfn>[excerpt] </dfn>"}{full_text}{/exp:zm_excerpt}
{/exp:search:search_results}

Might return (for each result, on a search for “lorem” in some non-sensical entries)

<h2><a href="http://www.example.com/index.php/template-group/template/nam-tincidunt-pharetra-pede/" title="Nam tincidunt pharetra pede">Nam tincidunt pharetra pede</a></h2>


<dfn>[excerpt] </dfn>
<span>Curabitur ut tortor. Vivamus nonummy felis a mi. <strong>Lorem</strong> ipsum dolor sit amet, consectetuer adipiscing... </span>
<span>Aenean lobortis. Proin <strong>lorem</strong>. Aenean nunc nisi, sodales a, tincidunt non,... </span>
<span>augue purus posuere velit. Cras blandit leo sed <strong>lorem</strong>. Mauris auctor luctus mi. Curabitur erat ante,... </span>

Extract and upload to your system/plugins directory.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Updated with new parameters and a single bug fix. Please report any odd results.

       
mArzsmAn's avatar
mArzsmAn
45 posts
17 years ago
mArzsmAn's avatar mArzsmAn

Great plugin, works perfect for me.(Till now) I was just thinking it would be great to have such a plugin. 😉

One question: How can I prevent wrapping a tag? I tried chunk_wrap=”” without parameters but it still wraps the default tags

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Updated.

  • Added ‘keywords’ parameter, unrestricting this plugins usage from just the search results template.
  • Added sort parameter. (desc/asc) default is ‘asc’.
  • Added order parameter, currently only accepts ‘random’
  • Removed possible php error with no chunks found.
  • Added ability to not wrap chunks with span by using “none” in the chunk_wrap parameter.
  • Added ability to create a range of pre/post chars with the effect of each complete excerpt having differing amounts of pre and post text. This helps a page look less ‘machined’.
       
mArzsmAn's avatar
mArzsmAn
45 posts
17 years ago
mArzsmAn's avatar mArzsmAn

Just tried your updated version and got a lot of broken span and strong tags This is my result html <pre><code>From insights in the activation of the leptin receptor to the design of leptin antagonists </strong>

Leptin.<strong>..</strong> <<strong>/span></strong><span>primarily produced by adipocytes,<strong> and functions in a negative feedback loop acting on neuronal cells.</strong>..<strong> <</strong>/span><strong><span>the hypothalamus.</strong> Inactivating mutations in the leptin or leptin receptor <strong>(LR)</strong> genes lead to a.<strong>..</strong> <<strong>/span>

</span></span></span>[/code]

I’ve left all the spaces and linebreaks from the html output. And the chunks showing are not the one with the search keyword in it.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Can you show me the parameters you are using? A snippet of the implementation on page will do

       
mArzsmAn's avatar
mArzsmAn
45 posts
17 years ago
mArzsmAn's avatar mArzsmAn
{exp:zm_excerpt output_wrap="p"}{full_text}{/exp:zm_excerpt}

when I use chunk_wrap=”none” I get this result

Signalling in the innate immune response
<strong></strong>

Invading pathogens are rapidly sensed by the hosts'<strong></strong>..<strong></strong>. immune system.<strong></strong> The molecular mechanisms underlying pathogen detection and the subsequent cellular.<strong></strong>..<strong></strong> are rapidly being unveiled,<strong></strong> but major knowledge gaps still remain.<strong></strong> We focus on two aspects of the.<strong></strong>..<strong></strong> <<strong></strong>/p>
   
       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

What happens if you omit that parameter?

       
mArzsmAn's avatar
mArzsmAn
45 posts
17 years ago
mArzsmAn's avatar mArzsmAn

This is the result without any parameters {exp:zm_excerpt}{full_text}{/exp:zm_excerpt}

<span>Signalling in the innate immune response
<strong></strong>

Invading pathogens are rapidly sensed by the hosts'<strong></strong>..<strong></strong>. <strong></strong></span><strong></strong><span>immune system.<strong></strong> The molecular mechanisms underlying pathogen detection and the subsequent cellular.<strong></strong>..<strong></strong> <<strong></strong>/span><strong></strong><span>are rapidly being unveiled,<strong></strong> but major knowledge gaps still remain.<strong></strong> We focus on two aspects of the.<strong></strong>..<strong></strong> <<strong></strong>/span>
</span></span>
       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

This issue has been fixed in the latest download.

       
mArzsmAn's avatar
mArzsmAn
45 posts
17 years ago
mArzsmAn's avatar mArzsmAn

Everything works perfect now but I have a feature request. When I do a search for a certain word(ie: interact) thats part of another word(ie: interaction) it shows a result but not the excerpt, could it be possible to show chunks when the word is part of another?

Thanks

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

I could… but the problem with searching is that it has 4 options - exact phrase / any words / all words / exact word - and the search module doesn’t yet (hint hint for prying eyes) log what type of search you ran. Without knowing that, it is difficult to create an excerpt that would follow the search type that was made.

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

Wow, this looks great. Will give it a shot on my current build. Thanks, the_butcher!

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

I think I’m nuts. I just installed this and put this code on my search results (which were previously working fine, but with no highlighting or anything):

{exp:search:search_results}
<dt><a href="http://{auto_path}">{title}</a></dt>
{exp:zm_excerpt output_wrap="dd"}{full_text}{/exp:zm_excerpt}
{/exp:search:search_results}

It’s not returning anything at all - the full text comes back unenclosed by anything. I’ll try re-uploading - any other ideas? Thanks.

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

I got this working with a little help from the_butcher but want to confirm: this does not work with pagination, correct? It is now working on the first page of results, but going to page 2 or 3 ends up with the full text being shown again, and nothing highlighted. This seems to be how almost all the search hilight plugins behave. I’ve tried:

pi.search_hilite.php pi.search_marker.php pi.search_term.php pi.zm_excerpt.php

The only one that seems to work with pagination is the 1st party one, search_hilite - although I like the zm_excerpt one best due to the excerpting, prepending, etc. Should pagination work with this? Thanks!

       
1 2 3

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.