Use view widget in place on transclusion

In one of the macros, I have this:

<$action-setfield $tiddler="test" text={{{[{$:/state/list-dates/date1}addsuffix[ - ]addsuffix{$:/state/list-dates/date2}addsuffix<lbr>] +[addprefix{test}] }}} />

$:/state/list-dates/date1 and $:/state/list-dates/date2 are transcluded. But instead of transclusion, I want to use $view widget on the text with parameters such as format and template set appropriately.

How do I do that? I tried my hand at it. But got syntax error.

So you want to display your date in a specific format, is that correct? Then you can use the format Operator https://tiddlywiki.com/#format%20Operator.

It act like a view widget with the date format, but in filters.

@telumire Thanks. But I did not get you.

In place of {$:/state/list-dates/date1}, I want to use the output of <$view tiddler=" $:/state/list-dates/date1" format="date" template="YYYY-0MM-0DD ddd" />

After your suggestion, I tried:

<$action-setfield $tiddler="test" text={{{[{$:/state/list-dates/date1}format:date[]addsuffix[ - ]addsuffix{$:/state/list-dates/date2}format:date[]addsuffix<lbr>] +[addprefix{test}] }}} />

But that did not work. Am I making some syntax error? How do I correct it?

Ah, I think I see the problem, since the second format:date[] comes after the addsuffix, it’s trying to convert the whole of what’s been generated as a date, including your - separator etc.

Here’s my best guess as to what you’re looking for:

<$action-setfield $tiddler="test" text={{{ [{$:/state/list-dates/date1}format:date[YYYY-0MM-0DD ddd]] [[ - ]] [{$:/state/list-dates/date2}format:date[YYYY-0MM-0DD ddd]] [<lbr>] +[join[]addprefix{test}] }}} />

So you’re generating the dates and formatting them separately, and then joining them at the end. I can’t test this code, but hopefully it works.

2 Likes

@stobot Thanks. But that did not help, either.

Is there a way to assign the value of $view widget to a variable and including the value of that variable instead of transclusion? That would make life simpler.

Assuming that $:/state/list-date/date1 and $:/state/list-date/date2 each contain a standard TW date value (i.e., using 17-digit zero-padded datetime format, YYYYMMDDhhmmssXXX, then the filter syntax

[{$:/state/list-date/date1}format:date[YYYY-0MM-0DD ddd]]

is equivalent to

<$view tiddler="$:/state/list-date/date1" field="text" format="date" template="YYYY-0MM-0DD ddd"

and the syntax suggested by @stobot should produce the output you want.

One detail: the TWCore formatting assumes that the input is a UTC datetime value (with a timezone offset of “+00:00”), but both the format:date[...] filter syntax and the <$view ... format="date" ...> syntax produce output for the local timezone (i.e., adjusting the result by the timezone offset for your locale). To prevent this timezone offset from being applied, the template used for formatting the output needs to include [UTC] at the beginning (i.e., [UTC]YYYY-0MM-0DD ddd).

However, this presents a minor problem, because you can’t use literal square brackets within the format:date[...] filter syntax, since the syntax itself uses square brackets to surround the “datetime template” parameter value. Fortunately, there is a workaround by defining the template value as a variable that includes the [UTC] syntax, and then referencing that variable in the filter syntax.

Thus, to achieve the proper result, you could use wikitext code like this:

<$vars tid="test" template="[UTC]YYYY-0MM-0DD ddd" lbr="
">
<$button> click me!
<$action-setfield $tiddler="test" text={{{ [<tid>get[text]addsuffix<lbr>] [{test/list-dates/date1}format:date<template>] [[ - ]] [{test/list-dates/date2}format:date<template>] +[join[]] }}} />
</$button>

Notes:

  1. The first part of the filter, [<tid>get[text]addsuffix<lbr>], gets the current tiddler content and, if not blank, adds a linebreak.

  2. The next three parts of the filter, [{test/list-dates/date1}format:date<template>] [[ - ]] [{test/list-dates/date2}format:date<template>] formats each input date using the desired template, with a literal " - " in-between.

  3. The last part of the filter joins the previous parts together to produce the desired output.

That should do it. Let me know how it goes…

enjoy,
-e

2 Likes

@EricShulman Thanks. That was very insightful.