How to create button that allows you to choose between two templates

Hello.
I am modifying my Tw so that it generates certain types of tiddlers using a template in order to create them uniformly and always with all the fields and labels that I need.
Everything works fine for me but I have a specific case for which I cannot find a solution. This is the Sources category, in which I group: books, electronic books, newspaper articles, internet articles, etc…
Since I want each of these source classes to have different fields, I have three templates to create them. The way I do it I would need 3 buttons to get what I want.

My question is: can I use a single button to choose the template I want to use and have the tiddler generated?

Example: I want to generate a book tiddler within the sources category tab. Then I press the generate new tiddler button within the sources tab and it gives me the option to generate a book, and newspaper article, and another document.
I choose the one that interests me and the corresponding template opens so I can fill out the fields and save the tiddler.

This is the code I use for the button:

\import plugins/kookma/thinkup/templates/equipos

<$tiddler tiddler=<<currentTab>>>
<$let new-item-title={{{[{!!title}]}}}>
<div class=float-right style=margin-right:20px;>

<$button class="btn btn-sm" tooltip="New" entry=<<createNewItem>>> 
+ <$text text="New entry"/>
</$button>
</div>
</$let>

And this is one of the templates I usually use:

\define createNewItem()
  <$action-sendmessage 
    $message="tm-new-tiddler" 
    title=<<unusedtitle """New $(new-item-title)$""">> 
    tags="[[$(currentTiddler)$]]" 
    imagen="$:/plugins/kookma/thinkup/images/mr_avatar"
    caption="pié de foto"
    text=<<content>>
    keywords=""
	/>
\end

\define content()

<table class="table-borderless source">
<tr><th>Título</th><td></td></tr>
<tr><th>Autor</th><td></td></tr>
<tr><th>Año</th><td></td></tr>
<tr><th>Formato</th><td>pagina-web informe libro electrónico blog periodico podcast</td></tr>
<tr><th>Enlace</th><td></td></tr>
<tr><th>Valoración</th><td>malo normal bueno excelente</td></tr>
</table>

<$macrocall $name=image-pretty img={{!!imagen}} align="right" caption={{!!caption}} tooltip={{!!caption}} alt=""/>

;Descripción
: <<.lorem>>

;Observaciones
: <<.lorem>>

\end

Greetings.

You could use a select widget to select the template to use, then use the selects actions to trigger the create new tiddler with the selected template.

  • Alternativly select the template then have a button to click and create.

The issue is you have two functions here which you are asking to combine, select type, create. How will it look, will users understand, why do you want one not three buttons?

3 Likes

Hi @Xabrina

Is this the kind of UI you would like ?

image

I think it can be done with a <$button> and a <$reveal> widget in popup mode.

See the “Popup” example in the official TW documentation.

Fred

1 Like

Yes. Swapping it arround. Create button then select template. Still 2 clicks but behind a simple + button.

1 Like

Hello Anthony.

I understand what you’re telling me, but the fact is that I have no idea how I can put together those routines so that they do what I’m trying to do. It’s hard for me to get an idea of how these things work.
Normally I take something from an example of yours or a wiki that I find and I break it down until I know how it works. But from scratch I am not able to move forward.

Why do I want to make a button with several options? That is a good question! The fact is that I already have several categories of tiddlers and I don’t want to fill the sidebar with more… An example: I use the PAE category that comes in Zettelksten (People/Animals/Entities), I don’t want to have them separate even though each one of them They have slightly different fields, which is why you want to choose the template when creating the new entry.

The same thing happens to me with sources of information. I don’t have the same fields for a book as for an internet article, for example. But I group all those tiddlers in the font category.

All the best.

Hello Fred.

Yes, what you propose would work perfectly for me. I have been taking a look at the information you have indicated but I still don’t understand how I can do it. I don’t see an example of doing something similar.

I’ve tried the system’s More button and cloned it to play with it and understand what it does, but I haven’t been able to get it to show me a cloned H4 tiddler with a different label.

I’ve looked at the code and I understand some things, but most of them I have no idea what they do…

All the best.

This option seems the easiest to put into practice. Another way that I imagined could be done would be with Radio buttons, but it would be the same case that you told me before. You would first select the template and then run from the button.

All the best.

Hi @Xabrina

Here is an example for you: XabrinaExample.json (4.0 KB)

Please be aware that provided templates are all the same, only captions differ.

Fred

2 Likes

Hello Fred.

It’s just what I was trying to do!!! :partying_face:

I will take a good look at the code to understand it well and be able to use it on other occasions. It was a great detail that you commented on the code, it helps people like me a lot. :sweat_smile:

Thank you so much!!!

All the best.

Nice work @tw-FRed, You have inspired me to write some generic solutions for popups,

  • I will share one back here to meet the Original Request but that can also be reused in many ways. And with an explanation.
  • This suggests to me a library of code solutions for the intermediate level user so they can save time but also learn from the example would make a good resource.
2 Likes

Thank you, clever solution !
Is it possible to modify it a bit to have the same look for the popup menu than the one used for the tags ?
Capture d’écran 2023-10-16 à 10.01.58

1 Like

Keep in mind it may be best not to have such “buttons” look like tag pills, a partial use of this class goes someway to your request. I found this in $:/core/macros/tag

  • Thats the thing about tiddlywiki, if you can see it already in tiddlywiki, its only a matter of looking for it.

class="tc-tag-label"

Snag_19fc66c5

1 Like

I was just asking for the pop up menu to look like the one of the tag button but without affecting the look of the button itself.

It is just how you layout the contents of the dropdown inside the reveal widget.

Title
<hr>
<$list ...
1 Like

The HTML structure for the tags popup menu is quite involved: each menu item is a div containing a span containing a link. But <$link> widgets don’t provide an actions attribute, so if you want to use the action widgets of my example you have to wrap all this inside an event catcher, maybe a <$linkcatcher> widget.
I’m not skilled enough to write this code, maybe someone else can help here?

Fred

1 Like

Fred, you can also uses buttons that can look like links to trigger actions on list items as well as navigate like a link.

Thanks Tones! I had tried with tag="div" to no avail, but with tag="a" I’ve got a working example for @Thomas_Chuffart :tada:
XabrinaExample2.json (4.1 KB)

There are still CSS issues with the pointer and I guess the original tag popup code is more elegant, but at least this example is a starting point…

Fred

1 Like

thank you @tw-FRed this example is more what I looked for.

I managed also to modify it using the .tc-btn-invisible class…

1 Like

@tw-FRed et al, perhaps I should have detailed;

  • There is arguably a simpler way which reduces the code complexity.
  • To make a link that allows actions to be assigned;
    • Rather than use alternative html tags you can just use the following class’s class="tc-btn-invisible tc-tiddlylink" as mentioned on the very last line of the ButtonWidget doco’
<$button to=<<currentTiddler>> class="tc-btn-invisible tc-tiddlylink">
Button name
</$button>
  • In this example it navigates to <<currentTiddler>> perhaps used inside a list.
    • But your actions could trigger new from template named in the <<currentTiddler>>
  • We are so used to using the link widget, or [[tiddlername]] or tiddlername with a link catcher, that we sometimes forget most links are in many ways just buttons. Then we get all the advantages of the button widget.
  • @Xabrina I believe you are content with the solutions you now have?
  • I have made a generic select-to-navigate macro and working on a generic select to action, where you could supply an action to create a tiddler using the selected template
  • However as is common for me I am going down a perfectionist rabbit hole trying to apply every trick in the book to make a very adaptable solution however that takes more time. eg
    • Provide any filter to generate the list
    • Configure title, label and tooltip
    • A display-filter to choose when to display the button at all
    • Use alternate classes as discussed
    • A set of actions to set fields or trigger new tiddler actions etc…
  • I will publish separately once I have something, unless asked here and particularly if asked by @Xabrina

Keep in mind I am always doing this kind of thing for myself so adding features to tiddlywiki’s are quick and easy, I have developed tiddlywiki into what I call a “Rapid software development environment”, yes it is possible :nerd_face:

2 Likes

Hello @TW_Tones.

Of course I’m happy!!! Instead of one solution I have two. :star_struck:

Just yesterday I was trying to use the code that @tw-FRed gave me along with what I already had previously to create tiddlers from pre-defined templates, and in the end I got everything working correctly.
With the last modification proposed by @Thomas_Chuffart and again made by @tw-FRed it even fits me much better.

Please, when you have something that you think is ready to share with us I would be happy to try it and see how it works. :nerd_face:

Thank you very much to everyone who has helped me directly or indirectly with this problem, this is the best thing this community has.

Thanks again and have a good day.

1 Like