Custom question variables

This shows how to add a custom question variable. It requires ThumbList version 2023-01-18 or newer.

Add new function to config.php:

We define a new function that will be called with different pieces of information about a picture, and it can modify some of the question variables or add new ones.

$ThumbList['CustVarFunction'] = 'MyQuestionVars';

function MyQuestionVars($filepath, $opt, &$QVars0, &$QVars){
  # filename without the extension .jpg
  $basename = $QVars['?i'];

  # replace dots and underscores with spaces
  $mycaption = preg_replace('/[._]+/', ' ', $basename);

  # space CamelCase
  $mycaption = AsSpacedUTF8($mycaption);

  # Uppercase Words
  $mycaption = mb_convert_case($mycaption, MB_CASE_TITLE, "UTF-8");

  # add new question variable
  $QVars['?x_mycaption'] = $mycaption;

  # instead, we could rewrite ?i
  # $QVars['?i'] = $mycaption;
}

The function will be called with the following arguments:

  1. $filepath: the path to the file on the server (e.g. uploads/Group/pic.jpg)
  2. $opt: the arguments from the (:thumblist ...:) directive
  3. $QVars0: an array with the question-variables ?1, ?2, ?3 and ?4, see documentation.
  4. $QVars: an array with the question-variables except ?1, ?2, ?3 or ?4, see documentation.

You can add new variables, it is recommended to add custom variables prefixed by "?x_" for like in the example "?x_mycaption". If in the future the Thumblist core adds new variables, they will never start with "?x" so there is a smaller risk of things breaking then.

The new question variable can be used in a wiki page, or in one of the configuration variables, and also as page variables in ImageTemplate (see documentation).

In a wiki page

Simply write your (new) question-variables as usual.

In the example below, we show both the "?i" variable, the base name of the picture, and our new "?x_mycaption" variable that is derived from the base name:

(:thumblist count=16..19 cols=2 px=200 captionfmt="?i?n?x_mycaption":)
DuplexScaling.jpg: 800x600, 140k (2009-02-13 12:30)
DuplexScaling
Duplex Scaling
Edmund_blair_leighton_accolade.jpg: 449x640, 80k (2009-02-13 12:30)
Edmund_blair_leighton_accolade
Edmund Blair Leighton Accolade
Esztergom.bazilika.lights.jpg: 800x600, 71k (2009-02-13 12:30)
Esztergom.bazilika.lights
Esztergom Bazilika Lights
FebruarPatent.jpg: 453x640, 85k (2009-02-13 12:30)
FebruarPatent
Februar Patent