* Peter Beardsley (pbe...@ap...) [011026 16:26]:
> Chris,
> I've been playing around with the news package, it's pretty cool. I
> just have a few questions:
Just so you know -- this will probably change quite a bit in the next
version of OI. It will use OpenInteract::CommonHandler and the
template widgets -- just like the 'classified' package does right now.
> OK, I see that the news package takes over the path /News/ and does
> it's thing from there. But if I wanted to grab news items from the
> news package and incoroporate them into other parts of my site,what
> would be the best way to accomplish that?
You'd create a component.
Here's an example -- it's simplistic and doesn't match up with what's
in the news object right now, but it should give you a good idea of
how things work.
Say you wanted to create a component to get the most recent stories
about a particular topic. The user of the component must specify a
topic, and can optionally specify the number of stories to display.
You'd define your component in the Action Table by adding something
like the following in a 'conf/action.perl' file:
----------------------------------------
$action = {
...,
'most_recent_news' => {
'class' => OpenInteract::Handler::MyNews',
'method' => 'latest_by_topic',
'security' => 'no',
},
};
----------------------------------------
Add an implementation for the action:
----------------------------------------
package OpenInteract::Handler::MyNews;
...
sub latest_by_topic {
my ( $class, $p ) = @_;
unless ( $p->{topic} ) {
return "<p>No topic given, so no news generated.</p>";
}
$p->{limit} ||= 10;
my $R = OpenInteract::Request->instance;
my %search_params = (
limit => $p->{limit},
where => 'topic = ?',
value => [ $p->{topic} ],
order => 'posted_date DESC',
);
my $news_iterator =
eval { $R->news->fetch_iterator( \%search_params ) };
if ( $@ ) {
return "Error retrieving news items.";
}
return $R->template->handler( {},
{ iterator => $news_iterator },
{ name => 'news::news_list' } );
}
----------------------------------------
And a template (pkg/news/template/news_list.tmpl):
----------------------------------------
[% WHILE ( news = iterator.get_next ) -%]
<p>
[% news.title %]<br>
by [% news.author %] on [% news.posted_date %]<br>
[% news.content %]
</p>
[% END %]
----------------------------------------
All done! You'd call it from the template like this:
----------------------------------------
[% OI.comp( 'most_recent_news',
topic = 'cryptography', limit = 5 ) -%]
----------------------------------------
And in place of the 'OI.comp...' call the five most recent news
stories about 'cryptography would be inserted. You could obviously
modify the template to put the stories into a table, or only display
headlines with a link to the full story, or whatever.
One note: even though we're defining a handler to work with news items
it *does not* need to be in the news package. You can create your own
package with all these items and access the news objects just
fine. This way you can update it independently of new OI releases and
not have to worry about merging new changes to yours.
> When I try to use the news package's templates in my own pages, it
> breaks my server in a really wierd way(in short OI's virtual host
> dies but the rest of apache keeps on living, but that's neither here
> nor there). Am I not supposed to be using the news package's
> templates/boxes?
That's very strange -- when you say 'my own pages' what do you mean?
And how exactly are you trying to use them?
Chris
--
Chris Winters (ch...@cw...)
Building enterprise-capable snack solutions since 1988.
|