Page 1 of 1

[solved] Set list of news on each category

Posted: Sun Nov 10, 2013 6:00 am
by luya
Website: http://cwmag.ca/stg
Template of news summary:

Code: Select all

{strip}
{capture assign=submitted_images}{$entry->file_location}/{$entry->fieldsbyname.Images->value}{/capture}

<!-- .news-summary wrapper -->
<article class='news-summary'>
    <!-- h2>Latest articles</h2 -->
    {foreach from=$items item='entry'}
    <!-- .news-article (wrapping each article) -->
    <section class='news-article'>
    <strong class="category"><a href="{$entry->category|cms_escape:htmlall}">{$entry->category}</a></strong> 
    <header>
      <a href='{$entry->moreurl}' title='{$entry->title|cms_escape:htmlall}'>
        {if $entry->fieldsbyname.Images->value != ''}
          {CGSmartImage filter_croptofit="320,200,c" noembed='0' quality='85' src="{$entry->file_location}/{$entry->fieldsbyname.Images->value}" alt="{$entry->title}"}
        {/if}
        <h2>{$entry->title|cms_escape}</h2></a>
    </header>
        
    {if $entry->summary}
          <p>{eval var=$entry->summary|strip_tags} <span class='more'>{$entry->morelink} →</span></p>
    {else if $entry->content}
        <p>{eval var=$entry->content|strip_tags}</p>
    {/if}
    <!-- footer class='news-meta'>
    {if $entry->postdate}
        {$entry->postdate|cms_date_format}
    {/if}
    {if $entry->category}
        <strong> {$category_label}</strong> {$entry->category}
    {/if}
 <!--   {if $entry->author}
         <strong> {$author_label}</strong> {$entry->author}
    {/if} -->
    </footer -->
<!-- .news-summary wrapper -->
    {
    </section>
    
    <!-- .news-article //-->
    {/foreach}
        <!-- news pagination -->
        {if $pagecount > 1}
        <span class='paginate'>
            {if $pagenumber > 1}
                {$firstpage} {$prevpage}
            {/if}
                {$pagetext} {$pagenumber} {$oftext} {$pagecount}
            {if $pagenumber < $pagecount}
                {$nextpage} {$lastpage}
            {/if}
        </span>
        {/if}
</article>
<!-- .news-summary //-->

{/strip}
I would like to create a news summary template for each category containing the recent news first followed by other news link. For example with fictionous tags:

Code: Select all

<section class="news-article">
<category>Category</category>
<h2>
   <img>
    Recent news
</h2>
<h3>Old news n</h3>
..
<h3>Old news n+1</h3>
</section>
Suggestion welcome. Thank you in advance.

Re: Set list of news on each category

Posted: Sun Nov 10, 2013 10:19 am
by velden
And what is your problem?

As far as I know all information is available in summary template. So in your summary template's foreach loop check if it's the first item and show image and title and summary. If it's not the first item show only title.

Re: Set list of news on each category

Posted: Mon Nov 11, 2013 3:54 am
by luya
I would like to insert other news title from the same category limited to three items

Code: Select all

{foreach from=$category item='entry'}	
	 <a href='{$entry->moreurl}' title='{$entry->title|cms_escape:htmlall}'>    
    <h2>{$entry->title|cms_escape}</h2>
    </a>
    {/foreach}

Re: Set list of news on each category

Posted: Mon Nov 11, 2013 8:07 am
by velden
I THINK you're trying it the wrong way;

I would expect it should only be needed to customize the summary template. The category should already be determined from within the {News ...} tag.

OR do you want it to automatically loop through all CATEGORIES and for each categorie display a detailed view of the latest news item plus 3 links to older news items?

Re: Set list of news on each category

Posted: Tue Nov 12, 2013 3:43 am
by luya
Here is the illustration I tried to achieve. In this example, Fashion is the category and the rest are news article. I could use {news ...} with tag but none seems to do what I look for.

Re: Set list of news on each category

Posted: Tue Nov 12, 2013 8:54 am
by velden
Read the help on the News module to start with. The news tag has a category parameter and a pagelimit parameter. Those you need to select the category and limit the items to 3-4.

Then you need to customize a News summary template. Read the sample templates that come with CMSMS and try to understand what they do. You probably always need to customize templates so they suite your needs. That's the CMSMS way: full control over template.

Pseudo/example code

Code: Select all

{foreach from=$items item=entry}
  {if $entry@first}
    do your thing you want to do for your first item only
  {else}
    do your thing for the other items
  {/if}
{/foreach}

OR

{foreach from=$items item=entry}
  {if $entry@first}
    do your thing you want to do for your first item only like display picture
  {/if}
  
    here you do the things you want for EVERY item

{/foreach}
EDIT: corrected wrong use of syntax in code

Re: Set list of news on each category

Posted: Wed Nov 13, 2013 5:19 am
by luya
Following your suggestion here is how I applied the news parameters.

Code: Select all

{news sortasc="false" category="Fashion" number="4"}
After customizing the summary news template, I encountered a syntax error on which "@first" modifier is unknown.

Code: Select all

Syntax Error in template "module_db_tpl:News;summarytest"  on line 8 "{if $entry|@first}" unknown modifier "first"
Which module provides that modifier?

Edit:
I realized I I had the answer on the news section I made earlieron the website while looking at your suggestion, only issue will be modifier "first". Another issue is with the categories. For example

Code: Select all

<section class='news-article'>
<strong class="category"><a href="{$entry->category}">{$entry->category}</a></strong> 
{foreach from=$items item=entry}
    <header>
      <a href='{$entry->moreurl}' title='{$entry->title|cms_escape:htmlall}'>
        {if $entry->fieldsbyname.Images->value != ''}
          {CGSmartImage filter_croptofit="320,200,c" noembed='0' quality='85' src="{$entry->file_location}/{$entry->fieldsbyname.Images->value}" alt="{$entry->title}"}
        {/if}
        <h2>{$entry->title|cms_escape}</h2></a>
    </header>
    {if $entry->summary}
          <p>{eval var=$entry->summary|strip_tags} <span class='more'>{$entry->morelink} →</span></p>
    {else if $entry->content}
        <p>{eval var=$entry->content|strip_tags}</p>
    {/if}
{/foreach}
</section>
When issue those news parameters as example

Code: Select all

{news sortasc="false" category="Fashion" number="4" summarytemplate="test"}
{news sortasc="false" category="Entertainment*" number="4" summarytemplate="test"}
Category fashion is repeated within the {news category="Entertainment"}. How to resolve that problem?

Re: Set list of news on each category

Posted: Wed Nov 13, 2013 7:24 am
by chandra
Please read the Smarty docs.

@first is not a modifier, it's a property.

Correct syntax is {$entry@first} :).

Re: Set list of news on each category

Posted: Wed Nov 13, 2013 7:37 am
by velden
Sorry for the wrong syntax, my bad.

Regarding the repeated category: I don't know. Fashion is not a child category of Entertainment I assume?

Not sure if it makes a difference but you could call the News modules with cms_module tag

Re: Set list of news on each category

Posted: Wed Nov 13, 2013 10:18 pm
by luya
velden wrote:Sorry for the wrong syntax, my bad.
Regarding the repeated category: I don't know. Fashion is not a child category of Entertainment I assume?
No, it is not. By looking at the Simplex news summary template, I used this condition for the category

Code: Select all

{foreach from=$cats item='node'}    
<strong class="category">{$node.news_category_name}</strong>
{/foreach}
which resolved the problem. However, attempting to create a url from that category with, for example "{$node.url}" led to generate a list of them while directing the url to the pagedetail having the same i.e. category Fashion will be directed to the page Fashion.

The website is taking shape, once the foundation is done I will show my work soon.

Thank you chandra for the syntax and for the reference of smarty documentary. Very similar to PHP or python but easier to grasp.

Re: Set list of news on each category

Posted: Fri Nov 15, 2013 11:12 pm
by luya
Here is the final solution

Code: Select all

{strip}
{capture assign=submitted_images}{$entry->file_location}/{$entry->fieldsbyname.Images->value}{/capture}

<!-- .news-article (wrapping each article) -->
<section class='news-article'>
 {foreach from=$cats item='node'} {* Find the category, extract its name  and associated it with its pageid *} 
      <strong class="category">{cms_selflink page=$node.news_category_name}</strong>
 {/foreach}
 {foreach from=$items item=entry}{* find all articles associated with that category *}
 {if $entry@first}{* get the most recent article *}
    <header>
      <a href='{$entry->moreurl}' title='{$entry->title|cms_escape:htmlall}'>
        {if $entry->fieldsbyname.Images->value != ''}
          {CGSmartImage filter_croptofit="320,200,c" noembed='0' quality='85' src="{$entry->file_location}/{$entry->fieldsbyname.Images->value}" alt="{$entry->title}"}
        {/if}

        <h2>{$entry->title|cms_escape}</h2></a>
    </header>
    {if $entry->summary}
          <p>{eval var=$entry->summary|strip_tags} <br /><span class='more'>{$entry->morelink} →</span></p>
    {else if $entry->content}
        <p>{eval var=$entry->content|strip_tags|summarize:20}
<br /><span class='more'>{$entry->morelink} →</span></p>
    {/if}
{else}
<a href='{$entry->moreurl}' title='{$entry->title|cms_escape:htmlall}'>
           <h2>{$entry->title|cms_escape}</h2></a>
{/if}
{/foreach}

<!-- .news-summary wrapper -->
</section>
 
{/strip}
Using the cms_selflink to create a link for category without the quotation properly links to the right page associated with that category. I realized the process covered more than the original topic. I can mark it as solved.