Page 1 of 1
Classes for category plugin
Posted: Mon Dec 03, 2007 7:09 am
by Don Chambers
Any chance there could be classes for categories by depth in the sidebar category plugin?? I just put together a custom tpl for the categories, but would rather see s9y crank out classes as follows:
a) category_depth0 (parent), category_depth1 (first child), etc...
b) a last and first item class, possibly for each depth level (margin and padding issue, particularly for child classes).
I did the above with smarty in a tpl, but thought it might be nice to have this generated by s9y so I do get the performance hit from the tpl.
You ARE looking for ideas for 1.3, right Garvin??!!

Re: Classes for category plugin
Posted: Mon Dec 03, 2007 9:33 am
by garvinhicking
Hi!
IMHO this just unnecessarily bumps the HTML volume. I don't see a huge usability inrease for that - if templates need it, they could specify their own categories smarty template file?
What would be the benfit of it? I'm willing to debate
Regards,
Garvin
Posted: Mon Dec 03, 2007 3:21 pm
by Don Chambers
Because I want to do something like this:
Parent0 Category
Child 1 category
Child 2 category
Child 3 category
//space - accomplished with bottom margin or padding on last Child item
Parent1 Category
Child 1 category
Child 2 category
Child 3 category
I didn't think it would be that big of a deal - the depth is already being calculated as it then sets left padding to 6px for each increment in depth. So left padding is 0 for parent, 6px for Child 1, 12px for child 2, etc. "All categories" already has its own class.
Posted: Mon Dec 03, 2007 4:14 pm
by garvinhicking
Hi!
Would you maybe have the time and motivation to provide a patch for this to the include/plugin_internal.inc.php file? You can commit that to the 1.2 and 1.3 branch, I'd appreciate it a lot. And yes, I do now see the importance of it.
Regards,
Garvin
Posted: Mon Dec 03, 2007 5:25 pm
by Don Chambers
I have the category depth worked out in the plugin as follows:
Code: Select all
$html .= '<li class="category_depth'. $cat['depth']. '" style="display: block;">';
No big mystery there.
I also added this:
Code: Select all
$categories[$cid]['catdepth'] = $cat['depth'];
So I can do this in the tpl:
Code: Select all
<li class="category_depth{$plugin_category.catdepth}" style="display: block;">
What I cannot seem to figure out is the last item for the child categories so I can emit a second class, like category_depth1_last. I would also need a smarty {if $plugin_category.last_item}.....
Ideas?
Posted: Mon Dec 03, 2007 5:47 pm
by Don Chambers
It dawned on me that isolating a specific category might be handy in some instances too, and for maximum understanding if styled in css, I used the category name (not the id). So the php and tpl codes, respectively, look like this now:
Code: Select all
$html .= '<li class="category_depth'. $cat['depth']. ' category_' . $cat['category_name'] .'" style="display: block;">';
<li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.category_name}" style="display: block;">
Still have not come up with a solution for the last <li> per depth..... assuming that idea even has relevance.
Posted: Mon Dec 03, 2007 6:01 pm
by garvinhicking
Hi!
Fetching the last depth in PHP is hard, because you would need to iterate over the resultset all over again. If you do that, you have nearly the same performance impact as when using Smarty templating. So this cannot be done...
Code: Select all
$html .= '<li class="category_depth'. $cat['depth']. ' category_' . $cat['category_name'] .'" style="display: block;">';[/quote]
You can't use that because category_name can contain invalid characters. You would need to strip out all invalid characters like , . " ! in the category name. I can't really recommend that, so you should use categoryid instead.
Regards,
Garvin
Posted: Mon Dec 03, 2007 6:15 pm
by Don Chambers
What about htmlspecialchars($cat['category_name'] ???
or do you think the id is still better for some reason, and if so, am I correct in assuming I would need to add something like this to use it in the tpl?
$categories[$cid]['category_id'] = $cat['categoryid'], or can I already fetch it in smarty without adding that?
Posted: Mon Dec 03, 2007 7:40 pm
by garvinhicking
HI!
Don Chambers wrote:What about htmlspecialchars($cat['category_name'] ???
That would introduce blanks and % signs, both invalid for CSS classnames.
BTW: The foreach loop wouldn't work for category childs, because "islast" is only set for the last category of all categories. There are no subloops for parent categories!
or do you think the id is still better for some reason, and if so, am I correct in assuming I would need to add something like this to use it in the tpl?
The ID would always only be a number
$categories[$cid]['category_id'] = $cat['categoryid'], or can I already fetch it in smarty without adding that?
you should be able to retrieve that in smarty already, yes. Check <pre>{$category|@print_r}</pre>
Regards,
Garvin
Posted: Mon Dec 03, 2007 10:04 pm
by Don Chambers
It also occurs to me that if 2 otherwise different category names each had the same positioned character stripped, they would return the same name after htmlspecialchars.....
Anyway, I committed it to both 1.2 branch and trunk.
Posted: Mon Dec 03, 2007 10:29 pm
by Don Chambers
As long as we are on the subject, how could I select, from a drop down list, a category id# and set it to a template option variable?
Posted: Tue Dec 04, 2007 10:21 am
by garvinhicking
Hi Don!
Don Chambers wrote:As long as we are on the subject, how could I select, from a drop down list, a category id# and set it to a template option variable?
This should work something like that (config.inc.php snippet):
Code: Select all
...
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$categories = serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
foreach($all_cats AS $cat) {
$catsel[$cat['categoryid']] = str_repeat(' ', $cat['depth']) . $cat['category_name'];
}
}
...
$template_config = array(
...
array(
'var' => 'specialcat',
'name' => 'Category',
'type' => 'select',
'default' => '',
'select_values' => $catsel
)
...
);
...
Then you should have a {$template_option.specialcat} smarty variable.
Regards,
Garvin
Posted: Tue Dec 04, 2007 3:22 pm
by Don Chambers
Thanks Garvin - and if I want to set 3 different template variables to cagegories, I only need the GET once, correct?
Posted: Tue Dec 04, 2007 3:23 pm
by garvinhicking
Hi!
Yes, you can simply put $catsel to the other config variable items where you want a selector.
Regards,
Garvin