How to "find and replace" when calling a field in a template?

57 views
Skip to first unread message

Ed

unread,
Jul 1, 2019, 8:40:54 PM7/1/19
to TiddlyWiki
Sorry for the poorly worded title. Hopefully my use case will make it clear what I am asking.

I want to quickly add YouTube videos to my wiki by copying a link into a text box.

I have a textbox/button that creates a tiddler and adds the text entered to a url field:

<$edit-text tiddler="$:/state/new-video-link" tag="input" default=""/>
<$button>
<$action-createtiddler $basetitle={{$:/state/new-video-link}} text="{{||YoutubeTemplate}}" url={{$:/state/new-video-link}}/>
Add
</$button>

The resulting tiddler calls a template (named "YoutubeTemplate") that will display the video from the URL:

<iframe width="560" height="315" src={{!!url}} frameborder="0" allowfullscreen></iframe>

The problem is that I need to use the 'embed' version of the URL.


Is there a method to 'find and replace' "watch?v=" with "embed/" when calling {{!!url}} in my template?

TonyM

unread,
Jul 1, 2019, 10:02:07 PM7/1/19
to TiddlyWiki
Ed,

Perhaps an alternate strategy is to just copy the code ICKToz7BLLA the generate a link or two
In this case you could use [<code>addprefix[https://www.youtube.com/embed/]]

But you can also take the link and [{!!url}removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]"

Regards
Tony

Ed

unread,
Jul 2, 2019, 7:37:58 AM7/2/19
to TiddlyWiki
Thank you Tony for your reply.

Perhaps an alternate strategy is to just copy the code ICKToz7BLLA the generate a link or two
In this case you could use [<code>addprefix[https://www.youtube.com/embed/]]

I want to use the full link simply because it is easier to copy and paste on mobile.

But you can also take the link and [{!!url}removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]
 
I have tried both

<iframe width="560" height="315" src="[{!!url}removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]" frameborder="0" allowfullscreen></iframe>

and

<$action-createtiddler $basetitle={{$:/state/new-video-link}} text="{{||YoutubeTemplate}}" url=[{!!url}removeprefix[https://www.youtube.com/watch?v=]addprefix[https://www.youtube.com/embed/]]/

But neither of those work. I don't think I understand how to include a filter within a widget or html?

TonyM

unread,
Jul 2, 2019, 9:20:56 AM7/2/19
to TiddlyWiki
Ed,

The quick answer is to place the filter inside triple braces without surrounding quotes

Name={{{ filter }}}

I am answering from my mobile and its not so easy to test you code. Have a go and let me know.

Regards
Tony

Mat

unread,
Jul 2, 2019, 9:41:27 AM7/2/19
to TiddlyWiki
For the record, when 5.1.20 is released (...aaaaaaany day now) it will be very simple to "search and replace" in a string by means of the split and join filter operators. See this doc, at bottom.

<:-)

Ed

unread,
Jul 2, 2019, 10:08:26 AM7/2/19
to TiddlyWiki
Thanks a lot Tony that works. I really appreciate you taking the time to help me.

For the record, when 5.1.20 is released (...aaaaaaany day now) it will be very simple to "search and replace" in a string by means of the split and join filter operators. See this doc, at bottom.

Thanks that's good to know.
Reply all
Reply to author
Forward
0 new messages