Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-05-21 18:34:37

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,282
Website

Is there a blueprint for conditional shortcodes?

Can’t figure out how to make a short code accept <txp:else />, e.g. work like this:

<has::something_good>
   Yeah :)
<txp:else />
   Nope :(
</has::something_good>

All my attempts so far have failed. Any clues on how to wire up a short code this way? Thank you for any tips.

EDIT: ack, mixed-up syntax. I meant <txp::has_something_good>

Last edited by Bloke (2020-05-21 21:27:30)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 2020-05-21 21:10:09

etc
Developer
Registered: 2010-11-11
Posts: 3,650
Website

Re: Is there a blueprint for conditional shortcodes?

Ahem.. it translates as

<txp:has_something_good>
   Yeah :)
<txp:else />
   Nope :(
</txp:has_something_good>

so it’s up to has_something_good plugin author to manage. As to

<txp::something_good>
   Yeah :)
<txp:else />
   Nope :(
</txp::something_good>

it calls something_good form via <txp:output_form />, storing the true part in <txp:yield /> for forms consumption. Do you mean the parser should output the false part if the forms output is empty? An example is welcome.

Offline

#3 2020-05-21 21:26:29

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,282
Website

Re: Is there a blueprint for conditional shortcodes?

Yeah, sorry. Poor syntax example on my behalf. I meant txp:: to call a form.

I was thinking of something like Michael’s is-newer example. In this case he’s not creating shortcodes for others to use in articles, more as a convenience tag to use in his templates that augments existing functionality.

In that example, sure, it’s a block of PHP but it doesn’t have to be. It could be a bunch of existing tags used in combination. I dunno, maybe a shortcode to check if an article contains only one article so the admin can use the tag in templates to decide whether to hyperlink the title or not on list pages.

I realise this is probably a stretch so if the answer is no, that’s fine. We have to place limits on shortcodes somewhere!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2020-05-21 22:11:22

etc
Developer
Registered: 2010-11-11
Posts: 3,650
Website

Re: Is there a blueprint for conditional shortcodes?

No worries, I agree ‘else’ feature is missing in shortcodes, though the problem can be circumvented. There are at least two potential solutions:

  1. either add some condition attribute to <txp:yield /> so shortcode authors can decide which part to use inside the form via <txp:yield condition="0|1" ... />
  2. or make <txp:output_form /> output ‘false’ part of its $thing if the ‘normal’ result is empty.

The latter is easier to implement but looks less flexible. It is, however, more predictable, since shortcodes themselves don’t have to care about ‘false’ part. We need more examples to decide.

Offline

#5 2020-05-21 22:24:01

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,282
Website

Re: Is there a blueprint for conditional shortcodes?

Sounds like a plan.

Until more concrete examples surface to help us formulate an opinion either way, my initial query is now satisfied, thank you. Pleased it’s not my dimwitted attempts at employing <txp:else /> in a shortcode that are failing: it’s not actually supported right now :)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 2020-05-22 05:25:41

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 7,875
Website

Re: Is there a blueprint for conditional shortcodes?

Bloke wrote #323115:

Can’t figure out how to make a short code accept <txp:else />, e.g. work like this:

<has::something_good>...

All my attempts so far have failed. Any clues on how to wire up a short code this way? Thank you for any tips.

EDIT: ack, mixed-up syntax. I meant <txp::has_something_good>

I do not think that this is /or should be working with short codes. This is how I think about them

<txp:if_yield name="yes">
Yeah :)
<txp:else />
Nope :(
</txp:if_yield>

where the values can be reached with <txp::something_good yes="x" />


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#7 2020-05-23 13:15:18

etc
Developer
Registered: 2010-11-11
Posts: 3,650
Website

Re: Is there a blueprint for conditional shortcodes?

Tentative else in shortcodes, adding else attribute to <txp:yield />. Now a if_newer shortcode might look like

<txp:yield else='<txp:evaluate query=''<txp:custom_field name="posted" /> + <txp:yield name="hour" escape="integer" default="0" />*3600 < time()'' />' />

(time must be registered for use in <txp:evaluate />). One calls it like this:

<txp::if_newer hour="1">
newer than <txp:yield name="hour" /> hours
<txp:else />
older than <txp:yield name="hour" /> hours
</txp::if_newer>

Let me know if it works for you.

Offline

Board footer

Powered by FluxBB