How to Add Content Summary for Search Results

There are often topics in CMS Made Simple official boards on how short content text could be added to search results from search module.
Well recently I was in need of exactly this function, and here is my solution. Unlike some solutions that require hacking core functions, I always seek for a solution that is not in need of hacking, and in most cases, there is one.

All we need for this is CGSimpleSmarty module and a UDT.

As I was unable to find aliases of the page from search module, the first thing I needed was a UDT that finds page aliases from their title.
Go to “Extensions -> User Defined Tags” and create a new UDT called ‘set_alias’ with this chunk of code:

/** Get page alias from title
* @params string $params['title']
*/
$gCms = cmsms();
$cntnt = cmsms()->GetContentOperations();

foreach ($cntnt->GetAllContent() as $page) {
if ($page->mName == $params['title']) {
   $return = ($page->mAlias);
   break;
}
}
if(!empty($params['assign'])){
        $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}

Now we have a UDT that extracts page aliases from page titles with a simple call like {set_alias title=’Some page title’}

As we have our UDT done,  the next step is installing the CGSimpleSmarty module and editing our search results template. To install CGSimpleSmarty, simply go to “Extensions -> ModuleManager”, look for CGSimpleSmarty and install it.
Then, move on to “Extensions -> Search” and click on the “Result Template” tab. Edit your template as follows:

<h3>{$searchresultsfor} "{$phrase}"</h3>
{if $itemcount > 0}
<ul>
  {foreach from=$results item=entry}
  {set_alias title=$entry->title assign='get_alias'} {* call set_alias udt and assign as get_alias *}
  <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
   {$cgsimple->get_page_content($get_alias,'content_en','result_content')}{$result_content|truncate:'160'|strip_tags} {* cgsimplesmarty looks through our var get_alias and content and assigns var result_content, then we output found content with result_content and truncate it to desired number of character *}
</li>
  {/foreach}
</ul>

<p>{$timetaken}: {$timetook}</p>
{else}
  <p><strong>{$noresultsfound}</strong></p>
{/if}

As you can see, there is the {set_alias} tag assigned to the ‘$get_alias’ variable.
Then, we use that variable in CGSimpleSmarty call to get the content of the page using {$cgsimple->get_page_content($get_alias,’content_en’,’result_content’)}. As you can see, we have ‘$get_alias’, search for ‘content_en’ and assign a new variable called ‘result_content’.
Then, ‘$result_content’ is called with smarty modifier truncate, so that we can shorten our text (to 160 character in this example) and strip_tags modifier so that HTML tags are removed, preventing our layout from braking.

That’s it! Now search results will be shown with a short summary from our content!

Update:

This is updated UDT for CMSMS 1.10.x compatibility and improved results. The UDT is using URL to check for Alias, this means you should use {set_alias url=$entry->url assign=’get_alias’}.

/** Get page alias from url
* @params string $params['url']
*/
$gCms = cmsms();
$cntnt = cmsms()->GetContentOperations();

foreach ($cntnt->GetAllContent() as $page) {
if ($page->GetURL() == $params['url']) {
   $return = $page->Alias();
   break;
}
}
if(!empty($params['assign'])){
        $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}

Looking for CMS Made Simple Hosting? Look no further than Arvixe Web Hosting.

Tags: , , , , , , , , | Posted under CMS Made Simple | RSS 2.0

Author Spotlight

Goran Ilic

Goran Ilic

Goran Ilic is a designer, all-rounder, husband, father, and founder of a CMS Made Simple related blog called: I do this (www.i-do-this.com). After long search for a perfect tool for his clients he has found CMSMS and devoted his passion to it.

2 Comments on How to Add Content Summary for Search Results

  1. Bertus says:

    Works great! Thanks for the effort!

  2. With the latest updates to CMSMS 1.11.9, it appears this can be simplified even further. I was able to implement this without a custom UDT by changing the Search template from this:

    {$cgsimple->get_page_content($get_alias,’content_en’,’result_content’)}{$result_content|truncate:’160’|strip_tags}

    To this:

    {$cgsimple->get_page_content($entry->modulerecord,’content_en’,’result_content’)}{$result_content|truncate:’160’|strip_tags}

    It appears that “modulerecord” (the page ID) was added to the search results and get_page_content seems to take the ID.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 9 = 18

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>