How can I add and retrieve a field depending on the occurence a tag?

Hi dear tiddloists,

I want to achieve the following, any ideas or hints around about it? I want to start some coding to improve my tw experience.

  • triggered by the creation of a certain tag in the tiddler (i.e. let us say a tag “abbreviation”) I want to insert a field or fields (i.e. “abbreviation_resolved”) programmatically. Adding template into the text would be helpful as well.
  • It would also be nice to evaluate have existing tiddlers against a constraint definitions like, for the given example, do all abbreviation tiddlers have that field.

Is anyone aware of existing pieces I could take as a starting point(s) to achieve something pointing into that direction? On the long run, I would be interested to achieve somthing like a way or form to handle general tiddler constraint definitions.

Critical comments are welcome, if you feel the approach is wrong, please let me know!

Thanks and greetings,
Frank

For your first question.
You can set up a template tiddler to automatically apply a template to a specific tag.

You can either set up your template in a tiddler named ‘what ever’ and then cal it in your target tiddler with
{{||$:/what ever}}

or tag your template tiddler

$:/tags/ViewTemplate

and then in the template filter for the tag you want with:

<$list filter=’[is[current]tag[what ever]]’>

Apologies if I’ve not got your question :slight_smile:

Before you go too far down the path of “tag conditional viewtemplates” consider using a field.

For example if a tiddler can be only one of a set of different types then use one field that can have only one value.
The following code would be in a tiddler tagged $:/tags/ViewTemplate

<$list filter="[all[current]has[object-type]get[object-type]]" variable=object-type>
   <$list filter="[<object-type>match[todo]]" variable=nul
	    what ever you want to appear on todo items eg: transclude a todo template
	 </$list>
	 <$list filter="[<object-type>match[todo]]" variable=nul
	    duplicate this list widget and change the test for each object type
	 </$list>
</$list>

TYhe advantages of the above method includes

  • Leave tags free for ad hoc use
  • The first test is does the current tiddler have an object-type field with a value otherwise it is all ignored
  • This by definition ends up only applying to tiddlers, not shadows.
  • Only when an object-type is available do the other tests take place
  • Only one of these tests will pass (if you keep the test to “[match[todo]]”

However lets say you want all “object-types” to use a template we can simplify this further.

<$list filter="[all[current]has[object-type]get[object-type]]" variable=object-type>
   <$list filter="[<object-type>addprefix[$:/templates/object-types/]has[title]]" variable=template-tiddler>
     <$transclude tiddler=<<template-tiddler>>/>
	 </$list>
</$list>

So now if the current tiddler has an object-type field with a value it will “attempt to transclude” a matching template tiddler eg: $:/templates/object-types/todo

So in closing rather than use and test a tag set the object-type field on a tiddler and compose its matching template tiddler.

I can package this with some additional features if asked.

@knarfix back to your original post.

If you follow this object-type method each of your viewTemplates could test for and offer to create any “constraint definitions” although I am not total sure I understand that term.

Many thanks @Ste_W and @TW_Tone for your answers.

I am not yet sure about my path. I think I will need templates will to display my content finally, in one or another way.

My apologies that I can not yet express my intention well, as I am just starting to learn to speak in tw-language :slight_smile:

I had intended to use tags as much as possible. As tags are available in standard tw directly from the edit menu, I planned to control the presentation by tag.

@TW_Tones I will consider your advice to better use a type field for this purpose.

But anyway, I am interested not to build a sophisticated interface with custom menus, but rather let “something” happen just when new field value or tag was entered.

I just found the fieldmangler(perhaps a widget not directly meant to be used by newbies), but maybe it could do what I meant: If a certain tag is added, add certain fields, or the like.

I will keep you posted.
Again, many thanks for the kind welcome and help here

sorry @TW_Tones I forgot to answer waht I meant by “contraints”. I thought of rules like that

if a tiddler is tagged “websource”, it needs a field “url”

I am building a solution you may use when complete however lets quickly look at your request. Note you have two questions add a field and retrieve a field and maybe you also want to edit the field.

  • The first trick is yo make a view template a tiddler tagged $:/tags/ViewTemplate and it’s content will appear on all tiddlers.
  • First we can make the content conditional based on some information on the current tiddler as per ste
<$list filter="[all[current]tag[what ever]]">
   content here
</$list>

Now inside the above you can add more to display only when your tag exists on the current tiddler

Eg in content here

<$list filter="[all[current]has[fieldname]get[fieldname]]" variable=field-value>
   fieldname <<field-value>> or {{!!fieldname}}
</$list>
<$list filter="[all[current]!has[fieldname]]">
    <$button tooltip="add fieldname to this tiddler">
       <$action-setfield $field="fieldname"  $value="defaultvalue" />
      Add Fieldname
  </$button>
</$list>

The above should be correct but I did not test it.

Minor edit

1 Like