cbe_if_variable

29/06/2011 - plugin, textpattern

Offre la possibilité de tester la valeur d’une variable parmi plusieurs possibles (tandis que le tag <txp:if_variable /> n’en permet qu’une seule) et de traiter une variable comme une liste de valeurs.

Le séparateur par défaut est la virgule mais on peut en préciser un autre.

Selon l’utilisation qui en est faite, cbe_if_variable se comporte comme les standards if_variable ou if_custom_field.

Deux autres tags cbe_if_variable_exists et cbe_if_variable_empty sont fournis.

Ce que vous en pensez [2]

Aide du plugin

if_variable for a list of values

cbe_if_variable

Evaluate your variables in a if_custom_fields manner.

Provide two additionnal tags: cbe_if_variable_exists and cbe_if_variable_empty

Table of contents

Download, installation, support

Download from the plugin page.

Copy/paste in the Admin > Plugins tab to install or uninstall, activate or desactivate.

Visit the forum thread for support.

Features

  • verify the variable among a separated list of values (example) ...
  • ... or a single value like <txp:if_variable />
  • the delimiter can be changed (example)
  • the variable can also hold a separated list, thus operates like <txp:if_custom_field />
  • supports <txp:else />

Tags and attributes

<txp:cbe_if_variable_exists> ... </txp:cbe_if_variable_exists>

Used as a container. Checks for existence only.

Attribute (required) Description and values Example
name variable name name="myvar"

<txp:cbe_if_variable_empty> ... </txp:cbe_if_variable_empty>

Used as a container. Checks for emptiness. For a relevant result the variable must exist though the plugin won't warn if not: it will just return false.

Attribute (required) Description and values Example
name variable name name="myvar"

<txp:cbe_if_variable> ... </txp:cbe_if_variable>

Used as a container. Checks existence, non emptiness, and values.

Attribute (bold if required) Description and values Example
name variable name name="myvar"
separator separator used in the variable if you want to consider it as a list of values. Defaults to delim. separator="/"
value list of values to look into.
If ommitted, ignores all other parameters and returns true if the variable is defined and has a value (i.e. returns false if not defined or is empty).
value="val1,val2,val3"
delim delimiter for the list of values, defaults to comma (,) delim="|"
match how to compare, defaults to any
any: checks that at least one of the variable's term is present at any place in value
all: checks that each variable's term appears at any place in value (this is nonreciprocical)
pattern: try to match the variable's value with the regular expression given in value. delim and separator are ignored.
match="all"

Examples

Basic usage

<txp:cbe_if_variable name="myvar" value="one,two,three">
  <p>Variable contains one, or two, or three</p>
<txp:else />
  <p>Variable contains another value</p>
</txp:cbe_if_variable>

... should be self explanatory.

Change the delimiter

<txp:cbe_if_variable name="myvar" value="four|five|six" delim="|">
  <p>Variable contains four, or five, or six</p>
<txp:else />
  <p>Variable contains another value</p>
</txp:cbe_if_variable>

... should be obvious too.

Practical application

<txp:variable name="public-sections" value=",about,contact" />
<txp:article>
  <txp:variable name="article-section" value='<txp:section />' />
  <txp:cbe_if_variable name="article-section" value='<txp:variable name="public-sections" />'>
    ... display the article ...
  <txp:else />
    ... display something else, or do nothing ...
  </txp:cbe_if_variable>
</txp:article>

Display an article if it belongs to an authorized section.

Summary

Here is what the three tags return in various situations (simplified syntax) :

(nothing)
cbe_if_variable_exists name="myvar" .. FALSE
cbe_if_variable_empty name="myvar" ... FALSE
cbe_if_variable name="myvar" ......... FALSE

===================

<txp:variable name="myvar"></txp:variable> or <txp:variable name="myvar" value="" />
cbe_if_variable_exists name="myvar" ............. TRUE
cbe_if_variable_empty name="myvar" .............. TRUE
cbe_if_variable name="myvar" .................... FALSE
cbe_if_variable name="myvar" value="" ........... TRUE
cbe_if_variable name="myvar" value="whatever" ... FALSE
cbe_if_variable name="myvar" value=",whatever" .. TRUE

===================

<txp:variable name="myvar" value="something" />
cbe_if_variable_exists name="myvar" .. TRUE
cbe_if_variable_empty name="myvar" ... FALSE
cbe_if_variable name="myvar" ......... TRUE
                                                match = "any"     "all"
cbe_if_variable name="myvar" value="" ................. FALSE ... FALSE
cbe_if_variable name="myvar" value="something" ........ TRUE .... TRUE
cbe_if_variable name="myvar" value="something,other" .. TRUE .... TRUE
cbe_if_variable name="myvar" value="anything,other" ... FALSE ... FALSE

===================

<txp:variable name="myvar" value="other, something" />
                                                match = "any"     "all"
cbe_if_variable name="myvar" value="" ................. FALSE ... FALSE
cbe_if_variable name="myvar" value="something" ........ TRUE .... FALSE
but : cbe_if_variable name="myvar" value="something" separator="|" is FALSE
cbe_if_variable name="myvar" value="something,other" .. TRUE .... TRUE
cbe_if_variable name="myvar" value="other" ............ TRUE .... FALSE
cbe_if_variable name="myvar" value="anything,other" ... TRUE .... FALSE

===================

<txp:variable name="phrase" value="here, there and everywhere" />
cbe_if_variable name="phrase" value="here, there and everywhere" ........... TRUE
cbe_if_variable name="phrase" value="here, there and everywhere" delim="" .. TRUE
cbe_if_variable name="phrase" value="here there and everywhere" ............ FALSE
cbe_if_variable name="phrase" value="here there and everywhere" delim="" ... FALSE
cbe_if_variable name="phrase" value="here there and everywhere" delim="/" .. FALSE
cbe_if_variable name="phrase" value="there" match="pattern" ................ TRUE
cbe_if_variable name="phrase" value="^there" match="pattern" ............... FALSE

Changelog

  • 01 Jul 11 - v0.2 -
    "match" attribute added, thanks sacripant !
    don't reinvent the wheel and use existing code
    added examples
  • 30 Jun 11 - v0.1.1 - Example fix
  • 29 Jun 11 - v0.1 - Initial release

Sur les mêmes thèmes

cbe_swap_body_excerpt | cbe_if_prod_status | cbe_output_form | Exemples pour cbe_members | cbe_members | Exemples pour cbe_frontauth | cbe_frontauth | cbe_keywords | cbe_helpful | MonEcharpe

Ce que vous en pensez

sacripant, le 29/06/2011 12:21 (#)

Hmmm, pratique. Merci Claire. Si j’ai bien compris ton plug’in ajoute l’opérateur OR aux variable de Textpattern.
Un plug’in léger qui évite d’installer smd_if pour ce type de besoin.
Je vais le tester rapidement…

Ce qui serait géniale c’est que t’y ajoute l’attribut “match” existant du tag “if_custom_field” (qui ajouterait du coup l’opérateur AND et les pattern).

Claire Brione, le 29/06/2011 12:44 (#)

Voilà, c’est tout-à-fait ça : pouvoir utiliser simplement l’opérateur OR.

Pour ta proposition, je regarde dès que j’ai un moment.

Edit : ok pour les patterns, par contre l’opérateur AND ne me semble pas avoir de sens. et finalement pour un comportement équivalent à if_custom_field.

Rechercher

Vous aimez...

... ce que je fais, ce que je suis. Ça vous est utile ou juste agréable. Vous voulez dire merci, alors

dites-le avec un livre

Tags

À propos

Je suis développeur web : je crée des sites Internet et en développe les fonctionnalités. Suite →

Mentions légales

Claire Brione
6 Chemin de l’Eglise14170 Vendeuvre – (Caen – Calvados)
SIRET : 412 079 550 00023 Suite →