Casting variables?

Hi. I’m trying to use a variable for the number in a limit statement in a filter expression. I cannot get it to work, and I wonder, is it because it is being interpreted as a string instead of an integer.

<$set name="pagecount" filter="{{$:/state/pagination}}">
<ol>

Pagecount: <b><<pagecount>></b>

<$list filter="[!is[system]!tag[journal-entry]!tag[journal]!tag[picture-post]!tag[Tags]!sort[created]limit[<<pagecount>>]rest[1]]" >
<li><$link to={{!!title}}>{{!!title}}</$link>
<span class='tc-subtitle'><$view format="date" template="MM/DD" field="created"/></span></li>
</$list>

</ol>
</$set>

just use single angle-brackets instead of double & straight, try this out…

<$set name="pagecount" filter="{{$:/state/pagination}}">
<ol>

Pagecount: <b><<pagecount>></b>

<$list filter="[!is[system]!tag[journal-entry]!tag[journal]!tag[picture-post]!tag[Tags]!sort[created]limit<pagecount>rest[1]]" >
<li><$link to={{!!title}}>{{!!title}}</$link>
<span class='tc-subtitle'><$view format="date" template="MM/DD" field="created"/></span></li>
</$list>

</ol>
</$set>
1 Like

I just now got it to work by using a macro. I had tried running it through a macro before, but when I called the macro via double angle brackets, it failed to return what I wanted. When I call it via the $macrocall widget, it does work! I don’t know why.

\define build-link(number)
<ol>
<$list filter="[!is[system]!tag[journal-entry]!tag[journal]!tag[picture-post]!tag[Tags]!sort[created]limit[$number$]rest[1]]" >
<li><$link to={{!!title}}>{{!!title}}</$link>
<span class='tc-subtitle'><$view format="date" template="MM/DD" field="created"/></span></li>
</$list>
</ol>
\end

<$macrocall $name="build-link" number={{$:/state/pagination}} />

This page helped a bunch: Notes on handling variables in TiddlyWiki | chrisnicoll.net

Brian, I think I tried single brackets as well as double brackets, but still failed to get the field interpreted as a number, not a string. I could be wrong!

In the macro, you keep the standard straight brackets [ ] and just stick the parameter call $number$ inside them

When using the variable from the Set widget, you want to replace the straight brackets [ ] with single angle brackets < >

Try the single bracket thing and also remove the quotation marks around the filter value in the set widget

This can be as simple as <$link/>

1 Like

I’m not 100% sure, what you want to achieve, but based on your code example in the OP it may look like this:

<$set name="pagecount" filter="[{$:/state/pagination}]">
  <ol>
  Pagecount: <b><<pagecount>></b>
  <$list filter="[!is[system]!tag[journal-entry]!tag[journal]!tag[picture-post]!tag[Tags]!sort[created]!is[draft]limit<pagecount>]" >
    <li>
      <$link to={{!!title}}>{{!!title}}</$link>
      <span class='tc-subtitle'><$view format="date" template="MM/DD" field="created"/></span>
    </li>
  </$list>
  </ol>
</$set>

I did use a filter expression for the set–filter widget. [{$:/state/pagination}] … You used a transclusion syntax. … The best way to test a filter is in the $:/AdvancedSearch : Fiter tab. If it shows something there is also should work as a filter parameter.

Have a closer look at: https://tiddlywiki.com/#Introduction%20to%20filter%20notation at the Special Parameters section

I did change the limit[<<pagecount>>] to limit<pagecount> Also see Special Parameters.

I did remove the rest[1] because I don’t know why it was there.

I did add !is[draft] … So draft tiddlers are not shown in the list. … You may remove this, if you want to see them


In general. It is much easier for us to work with a “plain text” description of what you want to achieve than with some code that doesn’t work. … We are able to fix the syntax problems, but we still need to guess, if the end result is something you actually wanted.

That’s right because the link-widget uses the “currentTiddler” variable as the default values. See: https://tiddlywiki.com/#LinkWidget

1 Like

Thank you for your patience and attention. I had not thought of using the search as a way to debug filters. I’m working on a simple pagination routine.

I’m wondering if the “number” parameter I specify below is handled the same whether I write:

<$macrocall $name=“build-link” number=‘1’ />

or

<<build-link 1>>

I think I was having trouble getting a filter to treat the number parameter as a 1, rather than text.

Here’s a concrete example.

I’ve tried to get the value of “totaltidds” into the second filter statement below using single brackets, double brackets, dollar signs + parenthesis and just dollar signs. I don’t think any of those are being interpreted as a number.

<$set name='totaltidds' filter='[!is[system]!tag[journal-entry]!tag[journal]!tag[picture-post]!tag[Tags]count[]]'>
<$set name='tiddspan' filter='[{$:/state/pagination}subtract[19]]'>

<$list filter="[field:title[$:/state/pagination]!text[20]]">
<$button>
<<previous-button 20>>
</$button>
</$list>
<$list filter="[field:title[$:/state/pagination]get[text]compare:number:lt[556]]">
<$button>
<<next-button 20>>
</$button>
</$list>

<div>Viewing: <<tiddspan>>-{{$:/state/pagination}} of <<totaltidds>></div>

</$set>
</$set>

@JenniferS
You may learn from Simple Alphanumeric Pagination - Tips & Tricks - Talk TW (tiddlywiki.org)

Think of the brackets in filter syntax as part of the operand value that is used to indicate the type of the operand:

  • square brackets are for literal values (i.e. constants)
  • angle brackets are for references to variables
  • curly braces are for references to tiddler fields

There is no explicit type casting for numbers vs. text… if a filter operand requires a number (such as in compare:number:lt..., then it should contain only numeric digits (and possibly a leading “-” for negative numbers). If the operand contains any non-numeric characters, it defaults to having a numeric value of 0.

Thus, in the second filter, you want to write:

<$list filter="[field:title[$:/state/pagination]get[text]compare:number:lt<totaltidds>]">

Note also that since the tiddler title in the above filter is just a constant text string, it is not necessary to use field:title[$:/state/pagination]get[text]. You can just write {$:/state/pagination}, which retrieves the contents of the text field from the named tiddler. Thus, your filter can be simplified to:

<$list filter="[{$:/state/pagination}compare:number:lt<totaltidds>]">

enjoy,
-e

3 Likes

I have struggled with this repeatedly because I was thinking of those square brackets as immutable, just simple delimiters. Now I see where they are information too. Wow.

Thank you so much!

1 Like