Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 2,776
Website

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: 6,202
Website

Re: Shortcodes (as seen in rah_beacon)

+1

Re the naming convention, what about: txpform.


Yiannis
——————————
neme.org | hblack.net | LABS | NeMe @ github

Offline

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

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 2,584
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: 2,286
Website

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.


etc_[ query | search | pagination | date | tree | cache ]

Offline

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

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 2,776
Website

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: 2,286
Website

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.


etc_[ query | search | pagination | date | tree | cache ]

Offline

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

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 2,776
Website

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,386
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: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 2,776
Website

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
Moderator
From: Germany
Registered: 2005-01-20
Posts: 2,584
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

Board footer

Powered by FluxBB