Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

plugin exercise.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Exitium
Smarty Rookie


Joined: 23 Mar 2006
Posts: 12

PostPosted: Thu Mar 23, 2006 3:44 pm    Post subject: plugin exercise. Reply with quote

Im trying to familiarize myself more with smarty and specifically creating plugings.

With that in mind, Im having difficulty wrapping my mind around using php classes within a smarty function plugin.

Just to preface the question, Im not having issues using already made plugins, just looking for some guidance on creating my own. I have a handle on passing parameters to the function and such Im just not having much luck using a php class insode the smarty plugin.

With that in mind a simple example of what I am looking for would be how to make a plugin that uses pears XML_RSS. Here is an example of how its used directly in php, but what do I need to do to "functionize" it?

Code:

require_once "XML/RSS.php";

$rss =& new XML_RSS("http://slashdot.org/slashdot.rdf");
$rss->parse();

echo "<h1>Headlines from <a href=\"http://slashdot.org\">Slashdot</a></h1>\n";
echo "<ul>\n";

foreach ($rss->getItems() as $item) {
    echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] . "</a></li>\n";
}

echo "</ul>\n";


Thanks for any and all guidance.
Back to top
View user's profile Send private message
demental
Smarty Rookie


Joined: 11 Jan 2006
Posts: 20
Location: Lost in the French deeeeep countryside

PostPosted: Thu Mar 23, 2006 7:06 pm    Post subject: Reply with quote

You would like to get a syntax like this I guess :
Code:

{rssfeed from="http://www.domain.com/rss.php" item=i}
<li><a href="{$i.link}">{$i.title}</a></li>
{/rssfeed}




I've never made such a plugin (which is a block plugin), but I think I have kindof an idea for this :
- At the first call of a block function, the second parameter $content is set to null. You know that you can instanciate your XML_RSS class this time
- I guess you have to call $smarty->register_object() to store your XML_RSS instance so that it can fetch the next records of the feed in the next loop cycles
- You can use the third parameter &$smarty of the block function (to assign to the "item" parameter the getItems() result
- If getItems() returns false, the fourth parameter of the block function (&$repeat) should be false too, to end up the loop

Voilą, let me know if this was helpful (hopefully my english is clear enough) and send the resulting plugin, I would be interested !
Back to top
View user's profile Send private message Visit poster's website
Exitium
Smarty Rookie


Joined: 23 Mar 2006
Posts: 12

PostPosted: Thu Mar 23, 2006 7:49 pm    Post subject: Reply with quote

demental wrote:
You would like to get a syntax like this I guess....
Thanks for your input demental. The example above simply makes a html list but I guess I was thinking more along the lines of a function to to not only parse the rss feed but also take additional params. The params would be used for building an html table, specifying xml file, table/cell attributes to use, limiting the amount of feed items etc.

Im not sure what I was doing wrong t begin with but the following seems to work. Granted it has no error checking or anything, but as I said im just trying to learn more about smarty plugins etc.

in template...
Code:

{XMLDisplay XML_file="http://slashdot.org/slashdot.rdf"}


and here is a down and very dirty function made from the above code but creating a table instead of a list.

Code:

function smarty_function_XMLDisplay($params, &$smarty)
{
         
   require_once "XML/RSS.php";
      
   $rss =& new XML_RSS($params["XML_file"]);
   $rss->parse();
         
   $output = "<table>";
   $output .= "<tr><th>Headlines from <a href=\"http://slashdot.org\">Slashdot</a></th></tr>";
   
   foreach ($rss->getItems() as $item) {
       $output .= "<tr><td><a href=\"" . $item['link'] . "\">" . $item['title'] . "</a></td></tr>";
   }

   $output .= "</table>";
   return $output;
}
Back to top
View user's profile Send private message
demental
Smarty Rookie


Joined: 11 Jan 2006
Posts: 20
Location: Lost in the French deeeeep countryside

PostPosted: Thu Mar 23, 2006 8:16 pm    Post subject: Reply with quote

At your convenience. My own way would be to make a block function, so that I can control the visual aspect of the list using html, not an eventually endless list of parameters.
Sounds like both methods have their pros and cons.
For example it would be rather difficult to have the channel information (as it has to be printed once before the loop) using a block function

Alternatively you could add some 'assigninfo' and 'assignlist' parameters to your function, so that you can do the following then :

Code:

{XMLDisplay XML_file="http://slashdot.org/slashdot.rdf" assigninfo=rssinfo assignlist=rsslist numitems=10}
<h3>Headlines from <a href="{$rssinfo.link}">{$rssinfo.title}</a></h3>
<ul>
  {foreach from=$rsslist item=post}
  <li>$post.title - <a href="{$post.link}">Read more</a></li>
  {/foreach}
</ul>
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP