1
sostool
need help move my code to template
  • 2012/8/6 22:59

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


hi i am new to xoops

i just made simple module as start

it working fine but no template


can someone help me move it to template

my code is

include '../../mainfile.php';
$xoopsOption['template_main'] = 'yahoo_feeds.html';
include 
XOOPS_ROOT_PATH . '/header.php';

require_once(
'class/simplepie/autoloader.php');


                    
// We'll process this feed with all of the default options.
                    
$feed = new SimplePie();
                    
                    
// Set which feed to process.
                    
$feed->set_feed_url('http://news.yahoo.com/rss');
                    
                    
// Set the maximum number of items
                    
$items_per_feed = 15;
                    
                    
// Run SimplePie.
                    
$feed->init();
                     
                    
// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
                    
$feed->handle_content_type();
                    
                    
                    
$c=0;
                    foreach (
$feed->get_items() as $item):
                    
$feed = $item->get_feed();
                        if(
$c<$items_per_feed)
                        {
                            echo 
'<hr>';
                            echo 
'<a href="'.$item->get_permalink().'" target="_blank">'.$item->get_title().'</a><br>';
                            echo 
'<b>'.$item->get_date().'</b><br>';
                            echo 
'<p>'.$item->get_description().'</p>';
                            
                        }
                        
$c++;
                        
                    endforeach;

include_once 
XOOPS_ROOT_PATH . '/footer.php';

I tried to move the my output the yahoo_feeds.html but nothing worked with me

can you help me pleas

Thanks in advance


2
zyspec
Re: need help move my code to template
  • 2012/8/7 2:34

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


There's several steps you need to follow to begin using Smarty templates within XOOPS. A quick
word of warning. I did not install and/or try this code so there may be a bug, so use this at your
own risk - but the general idea is right...

First, change your code to something like this:

include '../../mainfile.php'; 
$xoopsOption['template_main'] = 'yahoo_feeds.html'; 
include 
XOOPS_ROOT_PATH . '/header.php'; 

require_once(
'class/simplepie/autoloader.php'); 

// We'll process this feed with all of the default options. 
$feed = new SimplePie(); 

// Set which feed to process. 
$feed->set_feed_url('http://news.yahoo.com/rss'); 
 
// Set the maximum number of items 
$items_per_feed = 15; 
 
// Run SimplePie. 
$feed->init(); 
  
// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
 
$feed->handle_content_type(); 
 
 
$c=0; 
$feeditems = array();
foreach (
$feed->get_items() as $item) {
    
$feed = $item->get_feed(); 
    if(
$c < $items_per_feed) 
    { 
        
$feeditems[] = array('permalink' => $item->get_permalink(),
                                 
'title' => $item->get_title(),
                                 
'date'  => $item->get_date(),
                                 
'desc'  => $item->get_description()
                            );
    } else {
        
// break out of feed loop if reached maximum number of feeds
        
break;
    }
    
$c++;
}
    
$xoopsTpl->assign('feed', $feeditems);
include_once 
XOOPS_ROOT_PATH . '/footer.php';

Then in /{modulename}/xoops_version.php you need to add:

// Templates
$modversion['templates'][] = array('file' => 'yahoo_feeds.html',
                                   
'description' => "Display Yahoo Feeds Template");

And finally you need to create the template file (./{modulename}/templates/yahoo_feeds.html)

<{foreach item=feed from=$feeds}>
    <
hr />
    <
a href="<{$feed.permalink}>" target="_blank"><{$feed.title}></a><br />
    <
b><{$feed.date}></b><br /> 
    <
p><{$feed.desc}></p>
<{/foreach}>

Then copy this to your module directory and make sure you update the module using the Admin control panel.

3
sostool
Re: need help move my code to template
  • 2012/8/7 4:18

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


Thanks for your help

I tried your code


no error and nothing to show!


you can try it very simply one file module and simplepie code i got it from here
http://simplepie.org/wiki/setup/sample_page

or i can send you the module please

4
zyspec
Re: need help move my code to template
  • 2012/8/7 13:10

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


There's an error in the code I posted above....

You'll need to change the line near the bottom of the code from:

$xoopsTpl->assign('feed', $feeditems);

to
$xoopsTpl->assign('feeds', $feeditems);

Try to see if this works for you...

5
sostool
Re: need help move my code to template
  • 2012/8/7 18:44

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


That work just fine

Thank you very much appreciate


6
sostool
Re: need help move my code to template
  • 2012/8/8 22:58

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


I like to go more advanced to use multiple feeds
http://simplepie.org/wiki/tutorial/sort_multiple_feeds_by_time_and_date


change

$feed->set_feed_url('http://news.yahoo.com/rss');

to

$feed->set_feed_url(array(
                                                
'http://digg.com/rss/index.xml',
                                                
'http://feeds.tuaw.com/weblogsinc/tuaw',
                                                
'http://feeds.uneasysilence.com/uneasysilence/blog'
                                        
));

i tested it and worked fine

so I make table in database and insert some data

module sql file#
CREATE TABLE myfeeds_links (
  
lid int(11) unsigned NOT NULL auto_increment,
  
title varchar(100) NOT NULL default '',
  
url varchar(250) NOT NULL default '',
  
logourl varchar(60) NOT NULL default '',
  
status tinyint(2) NOT NULL default '0',
  
PRIMARY KEY  (lid),
  
KEY status (status),
  
KEY title (title(40))
) 
ENGINE=MyISAM;


in my module index.php after that you see before

after line
require_once('class/simplepie/autoloader.php');

I add
$query = $xoopsDB->query(' SELECT url FROM ' . $xoopsDB->prefix('myfeeds_links' . ' where status="1" '));

    while(
$myrow = $xoopsDB->fetchArray($query) )
    {
        
$url = $myrow['url'];
        
        echo 
$url.'<br>';
    }

The output is same links I inserted:
Quote:

I want to not to show the output but to move it to:
$feed->set_feed_url(array($url));
or
$feed->set_feed_url($url);

but not to work at all

thanks in advanced

7
sostool
Re: need help move my code to template
  • 2012/8/9 1:18

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


OK I learned this

$query = $xoopsDB->query(' SELECT url FROM ' . $xoopsDB->prefix('myfeeds_links' . ' where status="1" '));
 
$urls = array();

 while(
$myrow = $xoopsDB->fetchArray($query) )
 {
 
$url = $myrow['url'];
$urls[] = $url;
 
 
//echo $url.'<br>';
 
}

 
print_r($urls);

output for $urls is:

Quote:

and I am stuck here!

help me please

8
zyspec
Re: need help move my code to template
  • 2012/8/9 2:53

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


now, instead of
print_r($urls)
try
$feed->set_feed_url($urls);

9
sostool
Re: need help move my code to template
  • 2012/8/9 4:07

  • sostool

  • Just popping in

  • Posts: 6

  • Since: 2012/8/6 2


WOW

That was so cool


Thanks a lot