1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-06 12:13:16 +00:00
TiddlyWiki5/editions/tw5.com/tiddlers/filters/Introduction to Filters.tid

114 lines
3.8 KiB
Plaintext
Raw Normal View History

2014-04-10 18:56:51 +00:00
created: 20140410101941871
2014-10-09 19:51:58 +00:00
modified: 20141009180223576
2015-01-11 19:09:35 +00:00
tags: Learning Filters
title: Introduction to filter notation
2014-01-14 21:07:20 +00:00
type: text/vnd.tiddlywiki
2015-01-11 19:09:35 +00:00
: (This explains the basics of writing a [[filter|Filters]] to select a set of tiddlers. For a more technical summary, see [[Filter Syntax]].)
2014-03-04 21:17:01 +00:00
2015-01-11 19:09:35 +00:00
The simplest case is where you already know exactly which tiddlers you want. Type each title in double square brackets, with a space or linebreak between each one and the next:
2014-10-08 18:02:54 +00:00
```
2015-01-11 19:09:35 +00:00
[[Recipe book]] [[ScrambledEggs]] [[Mom's apple pie]]
2014-10-08 18:02:54 +00:00
```
2015-01-11 19:09:35 +00:00
You can omit the square brackets when a title doesn't contain any spaces:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[[Recipe book]] ScrambledEggs [[Mom's apple pie]]
2014-03-04 21:17:01 +00:00
2015-01-11 19:09:35 +00:00
```
2014-03-04 21:17:01 +00:00
2015-01-11 19:09:35 +00:00
The double square brackets are actually a shorthand for this:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[title[Scrambled eggs]]
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
… which gives us the <<doc-def "general model">> for any filter:
2012-05-20 17:47:11 +00:00
2015-01-11 19:09:35 +00:00
```
[method[parameter]]
```
2014-03-04 21:17:01 +00:00
2015-01-11 19:09:35 +00:00
For instance, here's how to select all the tiddlers that have been tagged <<doc-tag Recipe>>:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[tag[Recipe]]
2014-03-04 21:17:01 +00:00
```
2015-01-11 21:03:07 +00:00
We can reverse the meaning by adding an exclamation mark `!` just before the method. For example, we can select any tiddlers that do <<doc-em not>> have the <<doc-tag Recipe>> tag:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[!tag[Recipe]]
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
Tiddlers can be filtered by other fields than just title and tags:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[field:serving[4]]
2014-03-04 21:17:01 +00:00
```
2012-05-20 17:47:11 +00:00
2015-01-11 19:09:35 +00:00
That example will select any tiddlers that have <<doc-field-value 4>> in their <<doc-field serving>> field.
2013-05-31 12:32:34 +00:00
2015-01-11 19:09:35 +00:00
As the word "serving" isn't a standard filter method (and isn't likely to become one), you can safely omit the `field:` prefix:
2013-05-31 12:32:34 +00:00
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[serving[4]]
2014-03-04 21:17:01 +00:00
```
2013-05-31 12:32:34 +00:00
2015-01-11 19:09:35 +00:00
!Combinations
2015-01-11 19:09:35 +00:00
The filters we've looked at so far have involved just one step each. But you can <<doc-def run>> several steps together like this:
```
2015-01-11 19:09:35 +00:00
[tag[Vegetarian]!tag[soup]serving[4]]
```
2015-01-11 19:09:35 +00:00
Notice how the entire run is contained in a single pair of square brackets.
2015-01-11 21:03:07 +00:00
A tiddler has to match <<doc-em all>> of the steps in a run. So the example above retrieves vegetarian recipes (other than soups) for 4 people.
2015-01-11 21:03:07 +00:00
A <<doc-em sequence>> of runs will select the tiddlers that match <<doc-em any>> of the runs. We can use this to find recipes that serve either 3, 4 or 5 people:
2012-12-28 22:57:35 +00:00
```
2015-01-11 19:09:35 +00:00
[serving[3]] [serving[4]] [serving[5]]
2012-12-28 22:57:35 +00:00
```
2012-05-20 17:47:11 +00:00
2015-01-11 19:09:35 +00:00
If we want to ignore vegetarian recipes that serve 4, we can say this:
2014-10-09 19:51:58 +00:00
```
2015-01-11 19:09:35 +00:00
[serving[3]] [serving[4]!tag[vegetarian]] [serving[5]]
2014-10-09 19:51:58 +00:00
```
2015-01-11 19:09:35 +00:00
By default, each run considers every tiddler in the wiki. But we can use a `+` sign to force a run to consider only the tiddlers that were selected by the preceding runs:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[serving[3]] [serving[4]] [serving[5]] +[tag[Vegetarian]] +[sort[title]]
2014-03-04 21:17:01 +00:00
```
2015-01-11 21:03:07 +00:00
This selects recipes for 3, 4 or 5 people, then filters <<doc-em those>> to remove the vegetarian ones, and finally sorts any that are left into alphabetical order of title.
2015-01-11 19:09:35 +00:00
2015-01-11 21:03:07 +00:00
In a similar way, we can use a `-` sign to <<doc-em remove>> a run's tiddlers from the result so far. Here we select all vegetarian recipes apart from two:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[tag[Vegetarian]] -[title[ScrambledEggs]] -BeansOnToast
2014-03-04 21:17:01 +00:00
```
2012-05-20 17:47:11 +00:00
2015-01-11 19:09:35 +00:00
!Special parameters
2012-05-20 17:47:11 +00:00
2015-01-11 19:09:35 +00:00
The parameter of each step we've seen so far has been in square brackets, meaning that ~TiddlyWiki treats it literally. But two other kinds of bracket are possible:
2015-01-11 19:09:35 +00:00
<<doc-def "Curly brackets">> `{}` mean that the parameter is a TextReference, and that its value is to be looked up in a specified tiddler. For example, if we have a tiddler called <<tid Preference>> whose text happens to be the word <<doc-field-value Vegetarian>>, we can say
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[tag{Preference}]
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
as an alternative to `[tag[Vegetarian]]`. This allows the preference to change over time.
2014-03-04 21:17:01 +00:00
2015-01-11 19:09:35 +00:00
<<doc-def "Angle brackets">> `<>` mean that the parameter is the name of a [[variable|Variables]] whose value is to be used instead. Here we use the built-in <<doc-clink currentTiddler "WidgetVariable: currentTiddler">> variable in a filter that selects any tiddlers whose text contains the title of the current one:
2014-03-04 21:17:01 +00:00
```
2015-01-11 19:09:35 +00:00
[search<currentTiddler>]
```