Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2017-04-21 09:09:21

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Shortcodes (as seen in rah_beacon)

It would be great to extend Textpattern with some kind of shortcode support, like the ideas proposed in unreleased rah_beacon (but probably namespaced somehow to avoid tag name clashes). That way we can build lots of custom things that Textpattern doesn’t support directly in core.

Consider this video form template called media_video:

<div class="videoplayer" itemprop="video" itemscope itemtype="https://schema.org/VideoObject">
    <video width="<txp:variable name="width" />" height="<txp:variable name="height" />"<txp:if_variable name="ratio"> data-ratio="<txp:variable name="ratio" />"</txp:if_variable><txp:if_variable name="poster-url"> poster="<txp:variable name="poster-url" />"</txp:if_variable>>
        <source itemprop="contentUrl" type="video/mp4" src="<txp:variable name="mp4-url" />">
        <source itemprop="contentUrl" type="video/webm" src="<txp:variable name="webm-url" />">
    </video>
    <txp:if_variable name="duration-seconds"><meta itemprop="duration" content="T<txp:variable name="duration-seconds" />S">
</div>

Which can be called with the following shortcode by an author:

<shortcode:media_video width="" height="" ratio="" mp4-url="" webm-url="" poster-url="" duration-seconds="" />

For example:

<shortcode:media_video width="640" height="480" ratio="0.5625" mp4-url="/video/video.mp4" webm-url="/video/video.webm" poster-url="/img/poster.png" duration-seconds="20" />

I don’t know what the correct naming convention would be (‘shortcode’ is a WordPress term and I wouldn’t want to use it here) – but it’d be a great addition I feel.

Offline

#2 2017-04-21 09:38:42

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,065
Website GitHub Mastodon Twitter

Re: Shortcodes (as seen in rah_beacon)

+1

Re the naming convention, what about: txpform.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2017-04-21 09:44:16

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,702
Website

Re: Shortcodes (as seen in rah_beacon)

+100 !

I already use rah_beacon on a regular basis* (it works fine) and just take care not to use form names that overlap with txp tags, so your suggestion of a safer prefix is great.

It makes it a ton easier for other users to include entire blocks – a gallery, slider, course block, person profile … – in their articles, and the attributes = variables method is way easier than any output_form and txp:yield variants. Some things like images with srcset are just impractical otherwise.

I have no problem with the word “shortcode:“ but obvious alternatives might be insert:, include: or output: (thinking from a normal user’s perspective).

*smd_macro is good for this too, but with rah_beacon, you treat your ‘macros’ like normal forms, and can thus manage them as flat files, so it’s just a tad more practical.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2017-04-21 10:44:51

etc
Developer
Registered: 2010-11-11
Posts: 5,133
Website GitHub

Re: Shortcodes (as seen in rah_beacon)

This looks like a good idea (and is easy to do), but I would rather use <txp:yield name="blah" /> than <txp:variable name="blah" />, for consistency (tried, working).

Re namespacing, it could be just <form:media_video /> or, since we have silently introduced short tags in 4.6, <txp::media_video />. I can amend the parser either way if a consensus is found.

Offline

#5 2017-04-21 10:48:45

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Shortcodes (as seen in rah_beacon)

OK, I’ve never understood what <txp:yield /> does but if that is your preferred route and works generally the same as my example then fine with me. Very excited to get this into the core if we can.

How does short tags work exactly? I need to get my head round that before I give an opinion on namespace for this.

If this does become a feature, I’d also be keen to see people post examples of their shortcode solutions. Maybe in a series blog posts on the forthcoming new Textpattern official site ;)

Offline

#6 2017-04-21 11:06:09

etc
Developer
Registered: 2010-11-11
Posts: 5,133
Website GitHub

Re: Shortcodes (as seen in rah_beacon)

philwareham wrote #305428:

OK, I’ve never understood what <txp:yield /> does but if that is your preferred route and works generally the same as my example then fine with me. Very excited to get this into the core if we can.

They would be like “local” variables restricted to forms, so don’t clash with global <txp:variable />. You’d use them this way:

<!-- a form called "my_link" -->
<a href="<txp:yield name="href" />"><txp:yield /></a>

<!-- call -->
<txp::my_link href="#">Click me!</txp::my_link>

How does short tags work exactly? I need to get my head round that before I give an opinion on namespace for this.

Currently (4.6) like this:

<etc::query query="2*2" />

but that’s unofficial and could change.

Offline

#7 2017-04-21 11:14:58

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Shortcodes (as seen in rah_beacon)

Ah, OK. Yield makes sense in this instance, yes.

I’d rather not rely on short tag directly for this, so output sounds OK I guess. Although this feature has some shared functionality with <txp:output_form />. Hmmm, decisions.

Offline

#8 2017-04-21 11:35:14

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Shortcodes (as seen in rah_beacon)

philwareham wrote #305428:

OK, I’ve never understood what <txp:yield /> does..

Not many people do ;)

If this does become a feature, I’d also be keen to see people post examples of their shortcode solutions. Maybe in a series blog posts on the forthcoming new Textpattern official site ;)

Sounds great, but also post some examples on Textpattern Tips so the site doesn’t become obsolete..

Offline

#9 2017-04-21 11:37:21

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Shortcodes (as seen in rah_beacon)

jstubbs wrote #305431:

Sounds great, but also post some examples on Textpattern Tips so the site doesn’t become obsolete..

Of course!

Offline

#10 2017-04-21 11:40:25

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,702
Website

Re: Shortcodes (as seen in rah_beacon)

yield would be okay if you can use the name attribute, and the end user (admin writer) doesn’t have to use it (as totally non-intuitive for non-techs).

Also important is the ability to define defaults for attributes at the beginning of one’s form.

Just an info: Jukka’s version does already remove the variables you use as attributes at the end of each form, but there is, of course, potential for problems where an attribute name clashes with an existing variable.

And one thing I’ve found one need’s to watch out for is when using a variable of one’s own making inside a form, you need to to reset (or unset) it again at the end of the form or else it will carry over into the next instance. That problem may become less common with oleg’s forthcoming better evaluate options (i.e. obviating the need for using txp:variable to test for certain “has-this”, “is-that” cases), but I’ve yet to fully explore that.


TXP Builders – finely-crafted code, design and txp

Offline

#11 2017-04-21 13:07:55

etc
Developer
Registered: 2010-11-11
Posts: 5,133
Website GitHub

Re: Shortcodes (as seen in rah_beacon)

jakob wrote #305433:

yield would be okay if you can use the name attribute, and the end user (admin writer) doesn’t have to use it (as totally non-intuitive for non-techs).

Sure, end users would just need to say

<txp::media_video width="640" height="480" ratio="0.5625" mp4-url="/video/video.mp4" webm-url="/video/video.webm" poster-url="/img/poster.png" duration-seconds="20" />

or simply

<txp:output_form form="media_video" width="640" height="480" ratio="0.5625" mp4-url="/video/video.mp4" webm-url="/video/video.webm" poster-url="/img/poster.png" duration-seconds="20" />

Also important is the ability to define defaults for attributes at the beginning of one’s form.

Like this, rewritten if width attribute is set?

<txp:yield name="width">640</txp:yield>

And one thing I’ve found one need’s to watch out for is when using a variable of one’s own making inside a form, you need to to reset (or unset) it again at the end of the form or else it will carry over into the next instance. That problem may become less common with oleg’s forthcoming better evaluate options (i.e. obviating the need for using txp:variable to test for certain “has-this”, “is-that” cases), but I’ve yet to fully explore that.

Here I would like an example…

Re namespace again, I prefer short-tag <txp::some_form /> to potentially XML-clashing <output:some_form />. One of the reasons is also that namespacing implies some (small) performance penalty, and short tags (in 4.7) are optional.

Offline

#12 2017-04-21 13:09:50

etc
Developer
Registered: 2010-11-11
Posts: 5,133
Website GitHub

Re: Shortcodes (as seen in rah_beacon)

philwareham wrote #305430:

… this feature has some shared functionality with <txp:output_form />. Hmmm, decisions.

It is disguised <txp:output_form />, actually.

Offline

Board footer

Powered by FluxBB