How can I get categories from within a template file

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
jabeavers
Regular
Posts: 22
Joined: Thu Oct 07, 2010 4:31 pm

How can I get categories from within a template file

Post by jabeavers »

In my website I needed to get a list of s9y top-level categories to create links to them form my page to the blog. I used the following code to accomplish it.

// Switch to the Serendipity path. We need to use chdir so that the s9y framework can use its relative calls.
$oldDir = getcwd();
chdir('blog');

// Start the Serendipity API
require 'serendipity_config.inc.php';

// Get the categories
$cats = serendipity_fetchCategories(null, null);

// Switch back to original directory
chdir($oldDir);

I then parsed the array and outputted the info to make links. This all worked great!

In my template file in s9y, however, this doesn't work. How can I call serendipity_fetchCategories from my template file? This is used in a navigation bar across the top with drop down menus for each category. I want the same nav bar to function the same from my website or from s9y. However, I can't figure out how to call this function from my template file.

Thanks,
John

PS: website is http://www.gravleyenterprises.com/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How can I get categories from within a template file

Post by Timbalu »

Hi John

Did you ever try to have a look into all set s9y template vars?
Like

Code: Select all

        // view all smarty template vars
        echo '<pre>';
        print_r( $serendipity['smarty']->get_template_vars() );
        echo '</pre>';
Maybe, the variable you are searching for is set already and you don't need the other code?

or u use something like this
{serendipity_showPlugin class="serendipity_categories_plugin" side="*"}

Ian
jabeavers
Regular
Posts: 22
Joined: Thu Oct 07, 2010 4:31 pm

Re: How can I get categories from within a template file

Post by jabeavers »

Hey Timbalu,

I did look at that and it works great on the index page, but when one clicks on one of the categories, it doesn't have the list of categories in the data. I use a plugin for the categories for the sidebar, but I need to have control over the display of the info, and serendipity_fetchCategories() gives me the data so I can format it how I need, whereas the plugins just output the data with css as your only formating options.

Thansk.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: How can I get categories from within a template file

Post by garvinhicking »

Hi!

The PHP code calls are not all available in s9y smarty templates. Do utilize that, you have two options.

1: You edit/create a template's config.inc.php file, and use $serendipity['smarty']->register_function(...) calls for each s9y API function you want to wrap, and add a wrapper function call that uses the smarty function PHP syntax. This is the "cleaner" way, but also much more work to code.

2: You edit/create your template's config.inc.php and set $serendipity['smarty']->security = false;. This allows you to use Smarty's {php}...{/php} calls, where you can simply code your PHP parts into. This is a bit dirty, but has the quickest access. :)

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How can I get categories from within a template file

Post by Timbalu »

garvinhicking wrote:1: You edit/create a template's config.inc.php file, and use $serendipity['smarty']->register_function(...) calls for each s9y API function you want to wrap, and add a wrapper function call that uses the smarty function PHP syntax. This is the "cleaner" way, but also much more work to code.
Hi Garvin

Is it possible to register NON-API functions in config.inc.php to smarty with this too? Like

Code: Select all

function foo() { //something }
$serendipity['smarty']->register_function('isfoo', 'foo');
And second question, since which S9y version is it possible to use a template/config.inc.php file?

Ian
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: How can I get categories from within a template file

Post by garvinhicking »

Hi!

This is possible as long as your function has the (&$smarty, $params) function header; if not you need to have a wrapper function, like the s9y api does for usual code calls. Since this is much overhead, it's not done for every PHP function.
And second question, since which S9y version is it possible to use a template/config.inc.php file?
docs/NEWS says since 1.1-alpha5.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How can I get categories from within a template file

Post by Timbalu »

molto bene! ;-)

Code: Select all

function foo($params, &$smarty) { $b = 'something'; return $b; }
$serendipity['smarty']->register_function('isfoo', 'foo');

{isfoo} no $
Danke
Ian
Post Reply