Search results from News by category using only templates

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Search results from News by category using only templates

Post by psy »

It seems that the passthru parameter when using the Search module to search other modules is not yet implemented. Until it is, you may want to try the following. It works for me.

The module I'm searching is News and I only want to find articles in the 'faq' or 'faq' child categories.

In the News module, create the following summary template. I've named it 'searchnewsfaqsummary':

Code: Select all

{if $items|count gt 0}
{assign var=newsids value='~'}
{foreach from=$items item=item}
{$newsids|cat:$item->id|cat:'~'}
{/foreach}
{/if}
In Search, modify the results template as follows:

Code: Select all

{strip}
<h3>{$searchresultsfor} "{$phrase}"</h3>
{if $itemcount > 0}
	{capture assign=newsids}
		{cms_module module=News category="faq*" summarytemplate='searchnewsfaqsummary'}
	{/capture}
	
	{foreach from=$results item=entry}
		{assign var=modrecord value='~'|cat:$entry->modulerecord|cat:'~'}
		{if $entry->module == 'News' and $newsids|strpos:$modrecord !== false}
			<p><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)</p>
		{/if}
	{/foreach}
{else}
	<p><strong>{$noresultsfound} Try again</strong></p>
	{cms_module module=Search modules='News' search_method="post"}
{/if}
{/strip}
It works as follows:
- The Search module only makes one call to the News module so overhead is minimal.
- The news summary template creates a string something like ~12~ ~22~ ~37~
- The Search results template creates a variable {$modrecord} to which is prepended and appended a tilde ~ character to ensure each modrecord id string is unique
- The Search template then iterates through the $result list and compares the $modrecord (with ~ on both ends) to see if the same string appears in the captured News output.
- When a match is found, it outputs the Search result link

Please Note:
1. I changed the News template from the default {foreach from=$items item=entry} to {foreach from=$items item=item} to ensure it doesn't get confused with the Search $entry var.
2. While you can only have one Search result template, you can search and output results from multiple modules by refining the {if}{elseif}{else} statements to suit (and maybe adding eg 'and $page_alias eq 'faq'} or similar to narrow down the result set to get the correct results for a particular page.

It's not perfect but should get you by until the Search module passthru parameter is implemented.

Hope this helps
psy
Calimer0

Re: Search results from News by category using only template

Post by Calimer0 »

This works great for me!

I use it in combination with my custom search.

Code: Select all

<span class="resulttext">{$searchresultsfor} "{$phrase}..."</span>

{if $itemcount > 0}
   {capture assign=newsids}
   				{if ($page_language=="en")}
					{cms_module module=News category="English*" summarytemplate='searchnewsfaqsummary'}
				{elseif ($page_language=="nl")}
					{cms_module module=News category="Nederlands*" summarytemplate='searchnewsfaqsummary'}
				{/if}
	{/capture}
	{counter assign=idx print=0}
	{foreach from=$results item=entry}
		{if $idx <= 6}
			{assign var=modrecord value='~'|cat:$entry->modulerecord|cat:'~'}
			{assign var="url_lang" value="/"|explode:$entry->url}
			{if (($url_lang[3]=="en") || ($url_lang[3]=="fr") || ($url_lang[3]=="nl") || ($url_lang[4]=="en") || ($url_lang[4]=="fr") || 		($url_lang[4]=="nl"))}
				{if ($url_lang[3]==$page_language) || ($url_lang[4]==$page_language)}
					<a href="{$entry->url}">
						<div class="searchitem"><h3>{truncate_better text=$entry->title truncate='29' add='...' }</h3>
							{truncate_better text=$entry->urltxt truncate='90' add='...' }
						</div>
					</a>
					<hr/>
					{counter}
				{/if}
			{elseif $entry->module == 'News' and $newsids|strpos:$modrecord !== false}
				{if ($page_language=="en")}
					{news action='detail' articleid=$entry->modulerecord detailpage='news' detailtemplate="CLBNewsSearch"}
					{counter}
				{elseif ($page_language=="nl")}
					{news action='detail' articleid=$entry->modulerecord detailpage='nieuws' detailtemplate="CLBNewsSearch"}
					{counter}
				{/if}
			{/if}			
		{/if}
	{/foreach}

{else}
  <p class="noresult">{$noresultsfound}</p>
{/if}
See: http://www.soundintel.com/ for a working solution!
Locked

Return to “Tips and Tricks”