Adding {$entry->content} to search results.

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.
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Adding {$entry->content} to search results.

Post by Sendlingur »

Im trying to add {$entry->content} to the search results of my page but with out any luck.

Code: Select all

 <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)
      
</li>
{$entry->content} 
I know there is a 7 years old post here about the the same issue.
The solution presented in that post doesn't seems to be working with cms 2.x....

Can someone experienced explain to me if it it is possible or not?

thanks in advance.
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Adding {$entry->content} to search results.

Post by DIGI3 »

You could add <pre>{$entry|print_r}</pre> to see what options are available.

If content isn't one of them, you might be able to do something with {page_attr} to obtain the content.
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Re: Adding {$entry->content} to search results.

Post by Sendlingur »

I tried <pre>{$entry|print_r}</pre> content is not one of the options.

I'm to much of a newbie to know how to play with the {page_attr} tag :)
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Adding {$entry->content} to search results.

Post by DIGI3 »

I just did a bit of testing, and it doesn't look like it's going to be possible to reliably get the page alias from the $entry array.

You could use javascript/ajax to pull in the content from the url, which might actually be nicer anyway as if there's lots of results you could lazy load them as the user scrolls. The url you use in js would be something like {$entry->url}&showtemplate=false
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Re: Adding {$entry->content} to search results.

Post by Sendlingur »

I will try the javascript approach, but honestly I think it's a bit strange that the Search Module is so unflexible. Somehow I thought it would be rather easy to just implement $entry -> content to the foreach loop and then the magic would happen :)
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Adding {$entry->content} to search results.

Post by DIGI3 »

Agreed, ideally the alias should be returned to make it easier to obtain the content. You can always put in a feature request at http://dev.cmsmadesimple.org/projects/cmsmadesimple
Not getting the answer you need? CMSMS support options
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Adding {$entry->content} to search results.

Post by Rolf »

From default Search result template:
{*
You can also instantiate custom behaviour on a module by module basis by looking at
the $entry->module and $entry->modulerecord fields in $entry
ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'}
*}
This way you can show any News content you want...
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Adding {$entry->content} to search results.

Post by Rolf »

Sendlingur wrote:

Code: Select all

 <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)
      
</li>
{$entry->content} 
Note:
{$entry->url}, {$entry->urltxt} and {$entry->weight} are Search module Smarty tags
{$entry->content} is from the News module...
You can't mix them here.
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Re: Adding {$entry->content} to search results.

Post by Sendlingur »

Rolf wrote:From default Search result template:
Quote:
{*
You can also instantiate custom behaviour on a module by module basis by looking at
the $entry->module and $entry->modulerecord fields in $entry
ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'}
*}
This way you can show any News content you want...
But what if I want to show the first senteces from a page in content manager? would it work the same way?

And Rolf... I'm using the autocomplete search snippet from your website. It is working like a charm but as you know it is searching through titles and obviously it returns both article titles from the news module and page titles from the content manager.

So my problem is basically to show content text, from any possible module.
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Adding {$entry->content} to search results.

Post by Rolf »

Working example

At https://beeldbank.zeijen.nu I use the Uploads module for photo's.
The important part of the Search Result template is:

Code: Select all

    {foreach from=$results item=entry}
        {if $entry->module == 'Uploads'}
            <div class="grid_2 m_bottom_15">
                {Uploads action='detail' detailtemplate='search' upload_id=$entry->modulerecord}
            </div>
        {/if}
    {/foreach}
I created a detail template in the Uploads module with my needed content:

Code: Select all

<a href="{$entry->canonical}" title="#{$entry->id} - {$entry->summary|escape:'htmlall'|default:''}">
    {CGSmartImage src="{$entry->origfile_url}" class="uploads_summary_img" filter_croptofit="500,500" filter_watermark=true alt="#{$entry->id} - {$entry->summary|escape:'htmlall'|default:''}"}
</a>
As far as I understand your question, this is what you need. This is for Uploads, but it also works for News, CGBlog, LISE, etc.

This link displays the search content: https://beeldbank.zeijen.nu?zoekwoord=rolf
Five times a result, five times displaying an Uploads detail template
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Adding {$entry->content} to search results.

Post by velden »

I just checked and it seems IF the 'module' is a content page, neither '$entry->module' nor the '$entry->modulerecord' are provided in the results.

IMO it would make sense to implement this (and probably easy to implement).

Feature request submitted: http://dev.cmsmadesimple.org/feature_request/view/11886
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Re: Adding {$entry->content} to search results.

Post by Sendlingur »

I've been trying to make this work so I can display few lines from the content in the search results. I found this old post with a great answer, but it is not working for cmsms 2.x. It seems to be exactly what I need.

I've been trying to rewrite the answer for cmsms 2.x but with out any luck. I believe I need more experience to be able to rewrite it so it works.

here is a UDT that have to be made (called '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;
}

Here is the code for the search template

Code: Select all

<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}
Can anyone help me to make this work for CMSMS 2.x?

here is a link to the old post viewtopic.php?f=7&t=54145

the answer is from a user called uniqu3, maybe he is still active and sees this :)
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Adding {$entry->content} to search results.

Post by DIGI3 »

2.3 will allow for accessing the page content in search results
(I don't have an answer for your UDT request, but for reference it won't be needed once 2.3 is out)
Not getting the answer you need? CMSMS support options
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Adding {$entry->content} to search results.

Post by velden »

UDT:

Code: Select all

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

$return = false;

$title = strip_tags($params['title']); // search might add span to title to highlite specific searched words

foreach ($cntnt->GetAllContent(false) as $page) {
 if (strip_tags($page->Name()) == $title) {
   $return = ($page->Alias());
   break;
 }
} 

if(!empty($params['assign'])){
       // $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}
Sample search result template:

Code: Select all

<h3>{$searchresultsfor} "{$phrase}"</h3>
{if $itemcount > 0}
<ul>
  {foreach from=$results item=entry}
  <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
   {if empty($entry->module)}
     {set_alias title=$entry->title assign='get_alias'} {* call set_alias udt and assign as get_alias *}
     {page_attr key=content_en page=$get_alias assign=result_content}{$result_content|truncate:'160'|strip_tags} {* page_attr 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 *}
  {/if}
</li>
  {/foreach}
</ul>

<p>{$timetaken}: {$timetook}</p>
{else}
  <p><strong>{$noresultsfound}</strong></p>
{/if}
Please test thoroughly before implementing in production environment

Further note that if in future versions the 'module' property of a search result entry is provided for 'content' too - as DIGI3 posted - that this template will fail. You then might need to check for {if $entry->module == 'content' } or something, in stead of {if empty($entry->module)}
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

Re: Adding {$entry->content} to search results.

Post by Sendlingur »

Thank you all for your help, I'll test the solution from Velden.

Is there a release date for 2.3?
Locked

Return to “CMSMS Core”