remihackert.net

Plugin MyTagIf pour Dotclear 2

Ce plugin pour Dotclear ajoute une balise-aiguillage du type <tpl:MyTagIf url="mon_tag"> fais ci, fais ça </tpl:MyTagIf> pour produire un comportement fonction de la présence ou non d'un certain Tag dans la liste des Tags associés à un billet.

Créez un répertoire myTagIf dans le répertoire plugins. et créez deux fichiers.
Le premier fichier pour le référencement du plugin par Dotclear est nommé _define.php

aiguillage.jpg


# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2008 Olivier Meunier and contributors
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# [url]http://www.gnu.org/licenses/old-licenses/gpl-2.0.html[/url]
#
# -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')) { return; }
$this--->registerModule(
/* Name */ "MyTagIf",
/* Description*/ "Si la liste des Tags contient, alors ....",
/* Author */ "Rémi Hackert",
/* Version */ '0.1',
/* Permissions */ 'usage,contentadmin'
);
?>


Le second fichier nommé  _public.php contient le code proprement dit:

<?php
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2008 Olivier Meunier and contributors
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# [url]http://www.gnu.org/licenses/old-licenses/gpl-2.0.html[/url]
#
# -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')) { return; }
# Add tp: MyTagIf
$GLOBALS['core']->tpl->addBlock('MyTagIf',array('tplMyTagIf','MyTagIf'));
class tplMyTagIf
{
/*dtd
<!--ELEMENT tpl:MyTagIf - - -- tests on current entry ---->
<!--ATTLIST tpl:MyTagIf
url CDATA #IMPLIED -- category has given url

*/
public static function MyTagIf($attr,$content)
{
$if = array();
$operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
if (isset($attr['url'])) {
$cats_if = array();
$compar_cat_op = '==';
$cats_if_op = '||';
$url = trim($attr['url']);
if (substr($url,0,1) == '!') {
$url = substr($url,1);
$compar_cat_op = '!=';
$cats_if_op = '&&';
}
$urls = explode(",", $url);
foreach ($urls as $k => $url)
{
$cats_if[] = '($_ctx->meta->meta_id '.$compar_cat_op.' "'.$url.'")';
}
$if[] = '('.implode(' '.$cats_if_op.' ',$cats_if).')';
}
if (!empty($if)) {
return '<!--?php if('.implode(' '.$operator.' ',$if).') : ?-->'.$content.'<!--?php endif; ?-->';
} else {
return $content;
}
}
protected function getOperator($op)
{
switch (strtolower($op))
{
case 'or':
case '||':
return '||';
case 'and':
case '&&':
default:
return '&&';
}
}
}
?>

 

Utilisation

Ensuite on utilise cette balise au sein d'une boucle EntryTags qui itère sur tous les tags associé à un post :

<tpl:Entrytags>     // boucle sur tous les Tag de ce post
   <tpl:myTagIf url="mon_tag">  // si le tag vaut mon_tag
     alors afficher mon texte si le tag courant de la boucle devient égal à "mon_tag".
   </tpl:mytTagIf>

   <tpl:myTagIf url="autre_tag">  // si le tag vaut autre_tag
     alors afficher mon texte ceci.. le tag courant de la boucle égal à "autre_tag".
   </tpl:mytTagIf>

</tpl:Entrytags>

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

La discussion continue ailleurs

URL de rétrolien : https://www.animals-in-motion.com/remihackert/index.php?trackback/16

Fil des commentaires de ce billet