search results links should not use default template

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

search results links should not use default template

Post by Sendlingur »

I'm using the search module on my page.

I have a results page that displays the search results.
The search searches through the news module and displays the title as a link to the page.

My problem is that I have different details page for each news category and when the user hits a link on the search results page the user should be able to go to the news item and see it displayed with the corresponding news_detail_template. As it is now the news details opens up in a default template and that is not good enough.

For clarification : News category A --- News detail template A
News category B --- News detail template B
News category C--- News detail template C

Isn't there a way to just skip this default template which opens all the links in the search results in. And make the links to open up like:

News category A --- News detail template A
News category B --- News detail template B
News category C--- News detail template C


Here is a sample of my code in the results page:

Code: Select all

{if $itemcount > 0}
<ul class="search-ul">
  {foreach from=$results item=entry}
    {if (strpos($entry->urltxt, "http://") !== false)}   
{else}
  <div class="search-results-box-item search-results">
    <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
    </li>
  </diV>
    {/if}

  {/foreach}
</ul>

{*<p>{$timetaken}: {$timetook}</p>*}
{else}
 <div class="search-results-box-item search-results">
  <p><strong>No result</strong></p>
</div>
{/if}

Here is the {search tag}

Code: Select all

{search formtemplate='mms_search' resultpage='results'}

here is the search input code:

Code: Select all

{$startform}

<input type="text" class="search-input form-control" id="{$search_actionid}searchinput" name="{$search_actionid}searchinput" size="20" maxlength="50" placeholder="search"/>

<button class="btn btn-search" name="submit" value="{$submittext}" type="submit" />
{if isset($hidden)}{$hidden}{/if}
{$endform}

It would be really nice if someone could clarify this for me?
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: search results links should not use default template

Post by velden »

I think it can't be done easily.

Functionality of the Search module is limited: you will get an url and title only. Not an item id, object or something like that.

Further, there's no real relation between a category and a template. You probably create the 'relation' in your pages by using something like {news category='category-A' detailtemplate='template-A'}. But in that case you explicitly define the category, something you can't do in a search template.

Only thing I can think of - and I realize it isn't a beautiful solution - would be to first load all the news items and store their title and category in some array.

Then in the search result, get the category from the news array based on title, and then create the url yourself based on that category (https://docs.cmsmadesimple.org/tags/cms ... action_url)

If you will be having tons of news items and/or search requests, this may be a bad idea.
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: search results links should not use default template

Post by rotezecke »

the Search module is limited: you will get an url and title only. Not an item id, object or something like that.
The search module returns a little info about modules (at least it does for me). That alone is not enough, you'd need to write a plugin that checks the category of the given News ID, and return your detail template

Code: Select all

{* in search result template *}
{if $entry->module == "News"} 
  {yourPlugin newsID=$entry->modulerecord assign=myTmplate}
  {News ... }
{/if}
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: search results links should not use default template

Post by velden »

The search module returns a little info about modules (at least it does for me). That alone is not enough, you'd need to write a plugin that checks the category of the given News ID, and return your detail template
Mmm, missed that one.

In that case it might be possible to do it without plugin. Create a new News detail template and call

Code: Select all

{* in search result template *}
{if $entry->module == "News"}
  {News action=detail articleid=$entry->modulerecord detailtemplate=YOURNEWSDETAILTEMPLATE  }
{/if}
Then in YOURNEWSDETAILTEMPLATE something like

Code: Select all

{$detail_template = ''}
{if $entry->category == 'category-A'}{$detail_template = 'template-A'}
{elseif $entry->category == 'category-B'}{$detail_template = 'template-B'}
}
...
{/if}

<a href="{cms_action_url action=detail articleid=$entry->id detailtemplate=$detail_template}">{$entry->title}</a>
Note: it may be possible that this call to the News module overwrites your search template $entry variable. To prevent that from happening you can change the foreach loop of your search results template:

{foreach $results as $result}

Of course you need to change the code accordingly.

Note 2: if you're using different pages to show the different News detail categories on you probably want to use the returnid parameter of cms_action_url. Then it would make sense to make use of the cms_module_hint tag (https://docs.cmsmadesimple.org/tags/cms ... odule_hint) for those pages. Then you don't need to explicitly provide the detailtemplate parameter to the cms_action_url tag.
Post Reply

Return to “CMSMS Core”