Search - results template - add content?

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
admsh
Forum Members
Forum Members
Posts: 93
Joined: Tue Aug 19, 2008 6:30 pm
Location: NY

Search - results template - add content?

Post by admsh »

I'm trying to add a few lines of content to the search results template. I wonder if anyone knows of an easy solution to do this?

I've tried adding "$entry->content" to the template, like this, but it doesn't seem to work:

Code: Select all

{foreach from=$results item=entry}
  <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />{$entry->content|truncate:300}</li>
{/foreach}
vinyl
Forum Members
Forum Members
Posts: 149
Joined: Mon Jul 13, 2009 8:18 pm

Re: Search - results template - add content?

Post by vinyl »

Don't know if you are still looking for an answer for your question but I can help you a bit.

I'm looking into how the search works right now and this is what i found:

Let's say you want some more text from the news module. Make a new detail template in the news module (call it search or whatever) and only add the fields you want your search page to show:

Code: Select all

{$entry->postdate|cms_date_format}{$entry->title|cms_escape:htmlall}


You can add any detail field you want that would normally be available in detail view.

Now in the search module template edit it so that when it stumbles on news results it will display the result with your news template:

Code: Select all

 {if $entry->module == 'News'}
          <a href="{$entry->url}">
          {News action='detail' articleid=$entry->modulerecord detailpage='News' detailtemplate="search" }
          </a>
{elseif}
rest of your search code


Hope it helps..!
uniqu3

Re: Search - results template - add content?

Post by uniqu3 »

Just recently i needed to show small chunck of content text for search results so for content pages you can do a combo of UDT, CGSimpleSmarty.

As Search module gives only Title of the page, or i at least didn't find a way to get page alias from it i was in need of a UDT:

Call it set_alias

Code: Select all

/** 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 you can build your search result template with this UDT and CGSimpleSmarty (note that i didn't use any module search here, so if you need that to add it as suggested above).

Code: Select all

<h3>{$searchresultsfor} &quot;{$phrase}&quot;</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}
konsument
Forum Members
Forum Members
Posts: 137
Joined: Thu Oct 26, 2006 9:20 am
Location: Dresden - Saxony - Germany

Re: Search - results template - add content?

Post by konsument »

wow uniqu3 - uve just made my day :)

Is it possible to get the the news module output too within your solution?
uniqu3

Re: Search - results template - add content?

Post by uniqu3 »

Well probably it would work if you combine it with vinyl's solution.
konsument
Forum Members
Forum Members
Posts: 137
Joined: Thu Oct 26, 2006 9:20 am
Location: Dresden - Saxony - Germany

Re: Search - results template - add content?

Post by konsument »

works fine. You just need to insert {if $param_module != 'News'} within the foreach because your solution also displays the title of the news module.
slabbe
Forum Members
Forum Members
Posts: 29
Joined: Wed Sep 28, 2011 1:22 am

Re: Search - results template - add content?

Post by slabbe »

When I try your solution it gives me the following error...

Code: Select all

Fatal error: Cannot access protected property Content::$mAlias in /var/websites/test.com/lib/classes/class.usertagoperations.inc.php(260) : eval()'d code on line 9
klenkes
Power Poster
Power Poster
Posts: 307
Joined: Mon Sep 08, 2008 9:43 am

Re: Search - results template - add content?

Post by klenkes »

andy4oo wrote:Hey guys i using babel multi language site, is there a way to show the results by language? I meant to show them for each language so the guy from english cant see the spanish or oter lang pages. Thanks
I did it this way:
I have different news categories for each language and named them de_DE and en_US and so on...

In Babel's language menu I assign the current language to a variable $currentlang:(later used in news template)

Code: Select all

{if $language->is_current}{assign var=currentlang value=$language->name}
I have a news detailtemplate called searchresult in which I compare $currentlang to news-category:

Code: Select all

{if $currentlang eq 'en' && $entry->category eq "en_US"}
...result...
{elseif $currentlang eq 'de' && $entry->category eq "de_DE"}
...result...
This way I filter out the language results not applicable to the current language.

Finally in the SEARCH resulttemplate:

Code: Select all

{cms_module module='News' action='detail' articleid=$entry->modulerecord detailtemplate='searchresult'}
Works fine!
uniqu3

Re: Search - results template - add content?

Post by uniqu3 »

@slabbe

This works with 1.10.x, it's slightly different then above UDT, as instead of comparing results against title i compare it with url, title wasn't realy good way as it isn't unique.

Code: Select all

/** 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;
}


@andy4oo
To return only results for current language i did something like that (note using CGSimpleSmarty):

Code: Select all

  {foreach from=$results item=entry}
	{get_alias url=$entry->url assign='set_alias'} {* above UDT *}
	{$cgsimple->get_root_alias($set_alias, 'is_lang')}

    {if $is_lang == $lang_parent}{* compare root_alias to mle var *}
		<a href="{$entry->url}">{$entry->urltxt}</a>
		<span class="result-content"> 
			{$cgsimple->get_page_content($set_alias,'content_en','result_content')}
				{$result_content|truncate:'100'|strip_tags}
		</span>
// AND SO ON...
    {/if}
  {/foreach}
urbini
Forum Members
Forum Members
Posts: 28
Joined: Sun Dec 16, 2012 11:41 pm

Re: Search - results template - add content?

Post by urbini »

Hi. I've put the above code by uniqu3 in CMS Made Simple 1.11.4, but doesn't work. Could you tell me if it has to be modified for 1.11.4 and eventually could you post the updated UDT and Results Template code?
Thanks
Locked

Return to “Modules/Add-Ons”