CMS Made Simple Forums
https://forum.cmsmadesimple.org/

Search results from News by category using only templates
https://forum.cmsmadesimple.org/viewtopic.php?f=4&t=61947
Page 1 of 1

Author:  psy [ Fri Jul 13, 2012 4:49 am ]
Post subject:  Search results from News by category using only templates

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':
\$1:
{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:
\$1:
{strip}
<h3>{$searchresultsfor} &quot;{$phrase}&quot;</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

Author:  Calimer0 [ Mon Nov 12, 2012 11:17 am ]
Post subject:  Re: Search results from News by category using only template

This works great for me!

I use it in combination with my custom search.
\$1:
<span class="resulttext">{$searchresultsfor} &quot;{$phrase}...&quot;</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!

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/