Page 1 of 1
[SOLVED] Filter News by date (month/year, etc)
Posted: Fri Jul 04, 2008 10:34 am
by stopsatgreen
I want to have a series of pages which will display only News Articles from a defined date period (monthly). I've seen there's a patch for the News module floating around but I don't really want to patch my site as it could break in future upgrades.
I came up with a solution that would display Articles based on the page creation date:
Code: Select all
{capture assign='newsletter_date'}{created_date}{/capture}
{foreach from=$items item=entry}
{if $entry->postdate|date_format:'%m' == $newsletter_date|date_format:'%m'}
<!-- Content -->
{/foreach}
But this is dependent on the page being created in the month you want to display; not ideal.
I think I'd have to create a UDT which would display Articles based on an input value (perhaps from a Content Block), but I don't really know where to begin. Can anyone help me out?
Re: Filter News by date (month/year, etc)
Posted: Fri Jul 04, 2008 12:12 pm
by cyberman
Why don't create a category/subcegory for the News-Articles and display it with a category parameter?
Re: Filter News by date (month/year, etc)
Posted: Fri Jul 04, 2008 12:51 pm
by stopsatgreen
Because there are already five possible subcategories for each Article, so I'd have to create five new subcategories every month.
I'm getting a bit closer to where I want to be; I've created a UDT which takes a string and returns it as a date:
Code: Select all
$input_date = $params['thismonth'];
echo date('d F Y',strtotime($input_date));
Which I can then input into my News template and filter by month:
Code: Select all
{capture assign='item_date'}{newsletter_date thismonth='June 2008'}{/capture}
{foreach from=$items item=entry}
{if $entry->postdate|date_format:'%m' == $item_date|date_format:'%m'}
<!-- Content -->
{/if}
{/foreach}
What I need now is a way to get the contents of a Content Block as a string and either pass it into the UDT instead of the parameter, or pass it into the News template as a parameter; neither of which I can work out how to do.
Also, with a lot of news stories on each page I feel that this could mean a lot of database queries. Could anyone suggest:
a) A way to pass the Content Block value to the UDT; or
b) A more elegant solution.
Re: Filter News by date (month/year, etc)
Posted: Fri Jul 04, 2008 2:14 pm
by cyberman
stopsatgreen wrote:
a) A way to pass the Content Block value to the UDT; or
Have you tried this?
Code: Select all
{content block='date' assign='item_date'}
A new variable $item_date is available in Smarty and can be read by udt
Code: Select all
$myVar = $smarty->get_template_vars('item_date');
Re: Filter News by date (month/year, etc)
Posted: Fri Jul 04, 2008 2:42 pm
by stopsatgreen
OK, so this is my UDT, called 'newsletter_date':
Code: Select all
global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
In my template I have this:
Code: Select all
{content block='Date' oneline='true' assign='item_date'}
In my page I have the value 'June 2008' in the Date block.
When I call {newsletter_date} in the News template, it returns 01 January 1970. What have I missed?
Re: Filter News by date (month/year, etc)
Posted: Sat Jul 05, 2008 8:52 am
by cyberman
stopsatgreen wrote:
echo date('d F Y',strtotime($myVar));
In my page I have the value 'June 2008' in the Date block.
Think PHP does not know how to format this ...
A better option for content block could be
Code: Select all
{content block='Date' oneline='true' wysiwyg='false' assign='item_date'}
So there will be no html tags or similar inside.
Re: Filter News by date (month/year, etc)
Posted: Sat Jul 05, 2008 10:47 am
by stopsatgreen
Think PHP does not know how to format this
It does; when I pass it as a variable in the UDT:
Code: Select all
{newletter_date thismonth='June 2008'}
I get the result I want. But that means creating a new News template every month, so what I need to do is pass the same value from the content block. I think I'm really close, I just want to know why my UDT isn't getting the value from the block:
Code: Select all
global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
Is there something missing or wrong?
Re: Filter News by date (month/year, etc)
Posted: Sat Jul 05, 2008 1:43 pm
by blast2007
stopsatgreen wrote:
I've seen there's a patch for the News module floating around but I don't really want to patch my site as it could break in future upgrades.
I can't understand why[url=http://'http://forum.cmsmadesimple.org/index.php/topic,20711.msg102501.html#msg102501']most asked, working[/url], and [url=http://'http://dev.cmsmadesimple.org/tracker/index.php?func=detail&aid=2175&group_id=8&atid=111']ready to apply patches[/url] are simply ignored by developers in new release ...
This is frustrating for contributors
Regards
blast
Re: Filter News by date (month/year, etc)
Posted: Sat Jul 05, 2008 3:26 pm
by cyberman
Maybe you should join the projectÂ

...
Re: Filter News by date (month/year, etc)
Posted: Sat Jul 05, 2008 3:37 pm
by stopsatgreen
Right now I don't really care whether the core is patched or not because it doesn't help me; I want to know if there's something wrong with my UDT which is stopping it from getting the variable from the content block:
Code: Select all
global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
The issue is not with the date format, because when I do this:
Code: Select all
global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo $myVar;
I get no output. Therefore, the UDT isn't getting the value from the content block. So what am I doing wrong?
Re: Filter News by date (month/year, etc)
Posted: Mon Jul 07, 2008 8:50 am
by stopsatgreen
Just giving this a bump, as I'd really appreciate a solution.
Re: Filter News by date (month/year, etc)
Posted: Mon Jul 07, 2008 12:36 pm
by cyberman
This works for me
Code: Select all
global $gCms;
$myVar = $smarty->get_template_vars('item_date');
echo $myVar;
But the problem is where you've called the second content block.
If it should be available in complete template (head and body section) I suggest to insert the call for the second block as the first line of template.
Re: Filter News by date (month/year, etc)
Posted: Mon Jul 07, 2008 1:44 pm
by stopsatgreen
That seems to have done the trick! Moving the {content block='name'} up to the top of the page resolved the issue. Not sure why that should be, but I'm not complaining!
Many thanks for your patience and help on this. When I get a chance, I'll look at adding it to the Wiki somewhere.