One macro for several similar jobs

102 views
Skip to first unread message

Mohammad

unread,
Aug 21, 2018, 9:10:13 AM8/21/18
to tiddl...@googlegroups.com
I have developed several macros for creating alerts! The difference among them is  only the classes.
What is the shortest solution to implement these macros in Tiddlywiki?


I have used the below approach: A general macro and call it for specific purpose from another macro!


\define __myclsl(type) alert $type$

\define alert(text:"", type:"alert-primary", width:"100%")
<div class=<<__myclsl $type$>> style="width:$width$;">
$text$
</div>
\end

\define alert-primary(text, width:"100%")
<div class="alert alert-primary" style="width:$width$;">
$text$
</div>
\end

\define alert-secondary(text, width:"100%")
<<alert text:"$text$" type:"alert-secondary" width:"$width$">>
\end

\define alert-success(text, width:"100%")
<<alert text:"$text$" type:"alert-success" width:"$width$">>
\end

\define alert-danger(text, width:"100%")
<<alert text:"$text$" type:"alert-danger" width:"$width$">>
\end

\define alert-warning(text, width:"100%")
<<alert text:"$text$" type:"alert-warning" width:"$width$">>
\end


Mohammad

TonyM

unread,
Aug 21, 2018, 10:23:56 AM8/21/18
to TiddlyWiki
Mohammad,

a braindump before I go to bed.

With a little work you could add a parameter for the alert-class "primary" "Secondary" and "danger" then build the "alert $alert-class$" string.

Thus you could use additional ones by creating the matching class and calling the one macro with different class names. You could even default to primary and use a macrocall below

Another way to handle defaults
\define alert(alert-class.....
<$set name=alert-class value="$alert-class$" emptyValue="alert alert-primary">
....

Then use as follows

<<alert "primary" "text" [width]>> 

[]=[optional]

<$macrocall $name=alert text="Text" /> uses default
<$macrocall $name=alert alert-class="primary" text=<<avariableormacrooutput>> />
<$macrocall $name=alert alert-class="secondary" text={{!!afieldname}} />
<$macrocall $name=alert alert-class="danger" text="Text" />

Good night
See ya Tomorrow
Tony

Mark S.

unread,
Aug 21, 2018, 10:24:57 AM8/21/18
to TiddlyWiki
I'm not sure what you mean by "shortest". You could define each macro in it's own tiddler, and then have one macro that uses any of them, passing a selector string to indicate which one to use, perhaps.

-- Mark

Mohammad

unread,
Aug 21, 2018, 10:51:28 AM8/21/18
to TiddlyWiki
Mark!
 I mean shortest code but have all those macros! i.e I want to have alert-primary, alert-secondary, .... macros
but don't want to repeat all those common parts in separate macros!

One way would be just use the above macro and pass the alert-type, but then I cannot have alert-primary, ... macros!

I dont know if I could explain my purpose or not! but in short I dont like to write again and again those common lines of code in different macros

-Mohammad

PMario

unread,
Aug 21, 2018, 3:10:03 PM8/21/18
to TiddlyWiki
On Tuesday, August 21, 2018 at 3:10:13 PM UTC+2, Mohammad wrote:
I have developed several macros for creating alerts! The difference among them is  only the classes.
What is the shortest solution to implement these macros in Tiddlywiki?

In creating "programs" you have a 80/20 rule. .. You can solve 1 problem 80% right in 20% of the time, you'd need to do it a 100% right.

So you can basically solve 5 problems good enough, to get the job done, in the same time, you'd need to do 1 thing 100% right and having 4 open issues!

So I think it's important to get the right balance. If it works for you it's a good 1st, 2nd or 3rd iteration. ...
You can do more improvements later, if they are worth the effort!

-mario

Mohammad

unread,
Aug 21, 2018, 3:17:31 PM8/21/18
to TiddlyWiki
Thank you Mario for your comment!

Mohammad

unread,
Aug 21, 2018, 3:18:11 PM8/21/18
to TiddlyWiki
Hello Tony!
 Yes, I modified the code and now is shorter and modular!

- Mohammad

Mark S.

unread,
Aug 21, 2018, 3:52:30 PM8/21/18
to TiddlyWiki
The code is so short already ... are you wondering if it's possible to create the macros on the fly perhaps?

-- Mark

Mohammad

unread,
Aug 21, 2018, 4:28:57 PM8/21/18
to TiddlyWiki
Mark,
 No, as I told I mean minimizing the repeated parts!
 I just removed the __myclsl based on Tony comments!

Thanks

TonyM

unread,
Aug 21, 2018, 7:54:52 PM8/21/18
to TiddlyWiki
Mario,

The 80/20 rule is very well illistrated in your comment, especialy raising the outstanding issues. Thanks for sharing that.

I feel it is different if you make reusable components. Thus making something reusable may require 20% more effort thus 120% is required for the first, however the second use may demand 50% of the effort of creating again and subsequent uses, perhaps as low as 2% of the original effort.

For this reason most of what I do in tiddlywiki is reusable however making macros use almost all variables (in the true sense of the word) is not documented in examples and a bit tricky. Perhaps tiddlywiki reusability demands 200% of the original effort but is more rewarding in reuse.

I hope to share wikitext for reusability tips when I grasp them myself.

Regards
Tony

TonyM

unread,
Aug 21, 2018, 8:01:53 PM8/21/18
to TiddlyWiki
Mohammad,

You will see in my responce to Mario that rather than simplifying code being my main objective is reusability. However I am always simplfying as best I can.

If a macro is easy to use, but is applicable in more cases all the better. This is one form of reusability.

Another form of reusability is code you can copy and refactor into another solution.

And others are reusable code snipits, reusable algorithiums and even ideas.

Regards
Tony

Mohammad

unread,
Aug 22, 2018, 12:34:32 AM8/22/18
to TiddlyWiki
Thank you Tony!
Great points! I always try to have the same approach: simplicity, modular, reusable and easy to maintain!

Mohammad 
Reply all
Reply to author
Forward
0 new messages