Application

  • Créer un fichier _public.php dans le thème actif
<?php
if (!defined('DC_RC_PATH')) { return; }

l10n::set(dirname(__FILE__).'/locales/'.$_lang.'/public');
# ---> Ajouter un class .current à la catégorie parent même quand on se trouver sur une catégorie enfant (2 niveaux)

$core->addBehavior('templateBeforeBlock',array('ShineBehavior','block'));
$core->tpl->addValue('CategoryIfCurrent',array('ShineTpl','CategoryIfCurrent'));
$core->tpl->addValue('CategorySiblings',array('ShineTpl','CategorySiblings'));

class ShineBehavior
{
	public static function block()
	{
		$args = func_get_args();
		array_shift($args);

		if ($args[0] == 'Categories') {
			$p =
			'<?php if ($core->url->type != "home") { '.
				'if ($_ctx->exists("categories")) { '.
					'$_ctx->current_cat_id = $_ctx->categories->cat_id; '.
					'$cat_id = $_ctx->categories->cat_id; '.
					'$rs = $core->blog->getCategoryParents($cat_id); '.
					'$_ctx->current_cat_parent_id = $rs->isEmpty() ? 0 : (integer) $rs->cat_id;'. 
				'} elseif ($core->url->type != "home" && $_ctx->exists("posts")) { '.
					'$_ctx->current_cat_id = $_ctx->posts->cat_id; '.
					'$cat_id = $_ctx->posts->cat_id; '.
					'$rs = $core->blog->getCategoryParents($cat_id); '.
					'$_ctx->current_cat_parent_id = $rs->isEmpty() ? 0 : (integer) $rs->cat_id;'.
				'}'.
			"} ?>\n";
			return $p;
		}
	}
}

class ShineTpl {	
	public static function CategoryIfCurrent($attr)
	{
		$ret = isset($attr['return']) ? $attr['return'] : 'current';
		$ret = html::escapeHTML($ret);
		$p =
		'<?php if ($_ctx->exists("current_cat_id")) { '.
		'if ($_ctx->categories->cat_id == $_ctx->current_cat_id || $_ctx->categories->cat_id == $_ctx->current_cat_parent_id) { '.
		"echo ' class="".addslashes($ret).""'; } ".
		'} ?>';
		
		return $p;
	}
	public static function CategorySiblings()
	{
		$p =
		'<?php  '.
		'$current_cat_id = $_ctx->current_cat_id; '.
		'$cat_parent_id = $_ctx->current_cat_parent_id; '.
		'if ($_ctx->exists("current_cat_id")) { '.
			'if ($cat_parent_id != "0") { '.
				'$rs = $core->blog->getCategoryFirstChildren($cat_parent_id); '.
				'$p = \'\'; '.
				'while ($rs->fetch()) { '.
					'if ($rs->cat_id == $current_cat_id) { '.
						'$p .= \'<a class="current" href="\'.$core->blog->url.$core->url->getBase(\'category\').\'/\'.$rs->cat_url.\'">\'.$rs->cat_title.\'</a>\'; '.
					'} else { '.
						'$p .= \'<a href="\'.$core->blog->url.$core->url->getBase(\'category\').\'/\'.$rs->cat_url.\'">\'.$rs->cat_title.\'</a>\'; '.
					'} '.
				'} '.
			'} else {'.
				'$rsp = $core->blog->getCategoryFirstChildren($cat_id); '.
				'$p = \'\'; '.
				'while ($rsp->fetch()) { '.
					'$p .= \'<a href="\'.$core->blog->url.$core->url->getBase(\'category\').\'/\'.$rsp->cat_url.\'">\'.$rsp->cat_title.\'</a>\'; '.
				'} '.
			'} '.
		'} '.
		'if ($current_cat_id == false) {$p = \'&nbsp;\';} '.
		'echo $p; '.
		' ?>';	
		return $p;
	}
}
?>
  • Pour l'appliquer sur une liste, j'utilise le code suivant :
<ul>
      <li{{tpl:CategoryIfCurrent}}><p class="fr"><a href="{{tpl:CategoryURL}}">{{tpl:CategoryTitle encode_html="1"}}</a></p> <p class="en">{{tpl:CategoryDescription}}</p></li>
</ul>