Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2018-03-31 21:29:28

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

Re: Attributes for txp:yield?

Please test this dev commit:

  • you don’t need to explicitly set yield="id, name, etc" in shortcode when mixing them with global attributes anymore, txp will try to be smart.
  • you can wrap (not empty) tags output between some prepend and append strings, setting wraptag="prepend<+>append" attribute. The split string <+> is temporary, awaiting a better proposal.

Thanks @Destry and @jacob for ideas.

Offline

#14 2018-04-01 08:25:26

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Attributes for txp:yield?

Awesome!

Some great fun can be had with this and the slightly tweaked example from the shortcodes page:

<figure<txp:yield name="class" wraptag=" class='<+>'" />>
   <txp:image id='<txp:yield name="id" />' />
   <txp:if_yield name="caption">
      <figcaption><txp:yield name="caption" escape="tidy,textile" /></figcaption>
   <txp:else />
      <txp:image_info id='<txp:yield name="id" />' wraptag="figcaption" escape="tidy,textile" />
   </txp:if_yield>
</figure>

Let’s try this:

<txp::figure id="129" caption="My overridden caption" />

Works and puts a custom caption in.

<txp::figure id="129" class="fred" caption="My overridden caption" />

Adds class='fred' to the <figure> tag. Single quotes, sure, but it’s easier than escaping them or incurring the penalty of using single quotes around the entire thing and forcing another parse for tag-in-attributes when there aren’t any.

Speaking of which, you can even go a bit more extreme:

<txp:variable name="mycap">This is my caption</txp:variable>
<txp::figure class="fred" id="129" caption='<txp:variable name="mycap" />' />

Or go nuts with the prepend and append:

<txp:variable name="mycap">This is my caption</txp:variable>
<txp::figure class="fred" id="129" caption='<txp:variable name="mycap" wraptag="Caption: <i><+></i>" />' />

Which renders:

<figcaption>Caption: <i>This is my caption</i></figcaption>

Wonderful! And it all works identically with the longhand <txp:output_form form="figure" yield id="129" />.

The only snag I can find so far is that if you don’t specify a caption it doesn’t pick up the default one. It seems that <txp:if_yield> always sees the (empty) caption attribute even if you don’t specify it. The plan with that figure shortcode was to use the caption assigned to the image itself if nothing was supplied. Thus, this:

<txp::figure id="129" />

Should output the <figcaption> for the image, if there is one. It’s not doing that and renders an empty <figcaption>.

Do we need to tweak the example to set a default or something? Or have the recent tweaks broken this functionality?


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

#15 2018-04-01 10:02:35

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

Re: Attributes for txp:yield?

Bloke wrote #310527:

if you don’t specify a caption it doesn’t pick up the default one. It seems that <txp:if_yield> always sees the (empty) caption attribute even if you don’t specify it … have the recent tweaks broken this functionality?

Yay, thanks, that is a bug (not recent though) that appears on successive form calls, will patch shortly. I would also like to find something more esthetic for <+> pattern, any suggestion?

Offline

#16 2018-04-01 10:14:37

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

Re: Attributes for txp:yield?

Destry wrote #310521:

Looking at this closer, I think there might be a discrepancy here against what I intended.

In my original example, the name="" attribute wasn’t pulling from a link data item, it was a free attribute to type in any name value a person wants for that given aside. If not used, nothing outputs.

I don’t think that’s how this integrated example of etc’s is designed, though. Am I wrong?

I have adapted the example to recent commits, it should work as you describe now.

Offline

#17 2018-04-01 11:48:51

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: Attributes for txp:yield?

etc wrote #310529:

…it should work as you describe now.

Thanks. I’ve updated the doc example

So, besides being a good excuse to demonstrate another way to juggle tag structure, the only real advantage with this example is being able to output multiple links in one place with a single tag. I can’t imagine ever doing that myself based on the purpose of this particular shortcode, but okay.

I’m not sure how that would impact using the free name attribute though — e.g. what if you needed three different names parsed to each link quote (name values are not pulled from the link data) — but it would work fine with one ID, as normal.

In any case, I made a relevant comment about that in the doc. People have to explore a bit and learn how to adapt. We can’t do cookie-cut code for every scenario, after all. ;)

Offline

#18 2018-04-01 12:27:07

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

Re: Attributes for txp:yield?

Bloke wrote #310527:

Awesome!

+1 – Excellent! Big thanks for adding this Oleg.

I like the way you have implemented it as an extension of the existing wraptag attribute for ‘freeform’ wrap scenarios with a placeholder for the content. It introduces no new attributes and is more succinct to write in code too. I take it this will work everywhere where wraptag is a valid attribute?

I don’t mind <+> as a choice of placeholder either. It’s not likely to conflict with an other needs like an @, #, or | might, and looks like a placeholder.


TXP Builders – finely-crafted code, design and txp

Offline

#19 2018-04-01 12:58:28

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: Attributes for txp:yield?

jakob wrote #310534:

I don’t mind <+> as a choice of placeholder either.

Agree. Anything else might be more confusing, in fact.

Offline

#20 2018-04-01 19:39:03

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

Re: Attributes for txp:yield?

Destry wrote #310531:

So, besides being a good excuse to demonstrate another way to juggle tag structure, the only real advantage with this example is being able to output multiple links in one place with a single tag.

Not really, that was already possible with your structure. The main difference is that if you provide a wrong (not existing) id, your snippet will output an empty <aside></aside>, and mine will output nothing. Plus the possibility to wrap linklist in other tags that aside. Both are ok depending on users needs.

jakob wrote #310534:

I take it this will work everywhere where wraptag is a valid attribute?

It will work with every tag processing wraptag via doWrap() (or rather tag()) function. This includes core and most plugins, but if a plugin author decides to treat wraptag differently, (s)he can.

Offline

#21 2018-04-01 20:53:25

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: Attributes for txp:yield?

etc wrote #310540:

main difference is that if you provide a wrong (not existing) id, your snippet will output an empty <aside></aside>, and mine will output nothing. Plus the possibility to wrap linklist in other tags that aside.

Good points! That first one is kind of funny. I can’t imagine anyone making that mistake and not catching it after a front-side verification. I’m a front-side verifier to a fault. Type a word, I must go check it! :)

The layers to this onion are too many for me to keep track of.

Thanks again.

Offline

#22 2018-04-02 08:38:23

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Attributes for txp:yield?

etc wrote #310528:

that is a bug (not recent though) that appears on successive form calls

Thanks for the patch. Working smoothly now. Rockin’.


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

Board footer

Powered by FluxBB