Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 10:40:33

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,246
Website GitHub

How do I change the processing order of shortcodes and wrapper tags?

Over in another thread about post-processing the body field before saving I mentioned a use case of splitting the body into sections. I haven’t (yet) implemented that as plugin that acts on article_submit so currently I use this as a wrapper plugin around txp:body that takes the html produced by textile and splits it into sections, i.e.:

<txp:jcr_sectionize>
    <txp:body />
</txp:jcr_sectionize>

This is the simplified idea:

User input as regular Textile in the body field. The user doesn’t need to concern themselves with presentational markup:

h2. My title

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.
 
h2. Another title
 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren

and so on. Regular textiled html output:

<h2>My title</h2>

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.</p>

<h2>Another title</h2>

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren</p>

The jcr_sectionize plugin splits that into sections (using DOMdocument to split into nodes, insert the wrappers and reoutput as html) that I can format in CSS into two columns with sticky headers. This is the post-processed html output:

<section class="body-section">
    <h2>My title</h2>
    <div class="body-text">
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.</p>
    </div>
</section>

<section class="body-section">
    <h2>Another title</h2>
    <div class="body-text">
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.</p>
    </div>
</section>

So far so good. That all works fine. With a smattering of css, you get sticky section titles on the left and body text on the right in a roughly 1/3-2/3 column layout. Putting a shortcode embed inside body text blocks is no problem.

Now I want to add a shortcode to embed some other content that spans both columns, for example a location map or an image gallery. I currently have my shortcode like this so that it breaks out of the current text block, creates a new section of its own, and then goes back to the text flow:

</div>
</section>
<section class="body-section embed-item">

<!-- the shortcode does its stuff here -->

</section>
<section class="body-section">
<div class="body-text">

The problem is my wrapper plugin ruins the code produced by the shortcode, presumably because it’s not producing clean nodes.

My question is, can I control the processing order of the wrapper plugin and shortcode in the body field. I tried something like this (simplified version).

<txp:jcr_sectionize[1]>
    <txp:body />
</txp:jcr_sectionize>

and then in the body field of the article:

… text …

<txp::my_shortcode[2] />

… more text …

… but no luck.

Is this possible? Is there perhaps a better way? Would post-processing the body_html on article_submit (instead of as a wrapper tag) get around this as shortcodes are not translated into html output in the body_html field?


TXP Builders – finely-crafted code, design and txp

Offline

#2 Yesterday 14:58:18

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,246
Website GitHub

Re: How do I change the processing order of shortcodes and wrapper tags?

Current workaround: I expanded on the plugin to also split cleanly at a certain class, i.e. embed-item that I should be able to use in different txp::shortcodes if I want to break out of the columnized layout.


TXP Builders – finely-crafted code, design and txp

Offline

#3 Yesterday 17:01:52

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

Re: How do I change the processing order of shortcodes and wrapper tags?

I wonder if adding the breakby attribute to the plugin can do the job. It will however limit as to where the gallery/map/video/whatever will appear and might even produce unwanted layout results.


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

Offline

#4 Yesterday 17:51:32

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,320

Re: How do I change the processing order of shortcodes and wrapper tags?

Reminds me of an ancient plugin: jcb_columnize (still available for download.)
from its help text:

Modes of operation
1 Split by Tag
This is the default mode for the plugin. Arguments specific to this mode are:

splitontag What tag to use as breaking points for the columns. Defaults to ‘p’. Do not include any angle brackets- only the name of the tag.

columns The number of columns to generate. Two or three seem to work best with shorter articles. Defaults to 2.

minsplit This is a minimum number of break points needed to columnize. Defaults to columns. This is a little buggy.

2 Split by String
This mode allows for creating column divisions wherever a selected string occurs. To activate this mode, set the attribute splitonstring equal to something other than “”.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#5 Yesterday 18:12:20

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

Re: How do I change the processing order of shortcodes and wrapper tags?

jakob wrote #342948:

My question is, can I control the processing order of the wrapper plugin and shortcode in the body field. I tried something like this (simplified version).

<txp:jcr_sectionize[1]>...

and then in the body field of the article:

… <txp::my_shortcode[2] /> …...

Not this way. Recall that the bracket syntax can only alter the processing order of siblings, i.e. tags that are direct children of a common parent tag. Here, <txp::my_shortcode /> is inside <txp:jcr_sectionize />, not its sibling.

Is this possible? Is there perhaps a better way? Would post-processing the body_html on article_submit (instead of as a wrapper tag) get around this as shortcodes are not translated into html output in the body_html field?

Processing on submit is clearly better, since it is done only once, not on each page visit. If you can, I would advice this way. Otherwise, provided your plugin handles it nicely, you can try postponing shortcodes inside the body to the second pass:

...
<txp:hide process="2">
    <txp::my_shortcode />
</txp:hide>
...

There should certainly be a less hacky solution, challenge open.

Offline

#6 Yesterday 21:56:53

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,246
Website GitHub

Re: How do I change the processing order of shortcodes and wrapper tags?

Thanks all for the tips.

colak wrote #342950:

I wonder if adding the breakby attribute

Yes, I don’t think breakby will accommodate different dynamic heading titles.

In reply to uli #342951:

Reminds me of an ancient plugin: jcb_columnize…

jcb_columnize I remember using in the past. You’re right that it will split at a string. Thankfully you don’t need that for column layout anymore now that CSS can produce columns. My situation needs a bit more than a simple div split.

etc wrote #342952:

Not this way. Recall that the bracket syntax can only alter the processing order of siblings, i.e. tags that are direct children of a common parent tag. Here, <txp::my_shortcode /> is inside <txp:jcr_sectionize />, not its sibling.

Hmm, yes. I should have remembered that. I tripped up on that before.

Processing on submit is clearly better, since it is done only once, not on each page visit. If you can, I would advice this way.

I may well switch to this method if my modified plugin creates problems down the line. I’d need to decide on some criteria for deciding when the body_html needs post-processing and when not (it’s not the same for all articles in the site).

Otherwise, provided your plugin handles it nicely, you can try postponing shortcodes inside the body to the second pass <txp:hide process="2"><txp::my_shortcode /></txp:hide>

This did something I’ve not seen before. The public side produces no article output, only this in the source:

<sandbox_69b726f3517976_56861454 id="13">a2ed561deb200f8baa0ccbbe14cd378d</sandbox_69b726f3517976_56861454>

There should certainly be a less hacky solution, challenge open.

I agree, various aspects of this whole thing are a bit hacky. Asking the user to enter complex tag-foo was what I was trying to avoid.

For now, my modified plugin that can also split on a defined class name seems to work.


TXP Builders – finely-crafted code, design and txp

Offline

#7 Yesterday 22:19:11

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,519
Website GitHub

Re: How do I change the processing order of shortcodes and wrapper tags?

jakob wrote #342957:

Asking the user to enter complex tag-foo was what I was trying to avoid.

Is there any way to leverage Override Forms? E.g. shove some dedicated forms in a group and nominate it in prefs, then tell the client to pick the one they want for special effects. E.g. with-map, with-gallery, etc could pre-render elements in a known location. Or split the body at display time on known simple replacement sequences and pass each chunk to sub-forms using <txp:yield /> or <+>.

Alternatively, the gallery could pick up the images from the article image field or a dedicated Gallery CF (same for the map coords?), and your regular form could render those elements as discrete blocks. Then some other on-page wrapper signal, like a class name, could dictate which order the blocks are displayed using CSS, essentially rewiring the page layout based on which Override Form is chosen.

Not sure if that kind of approach would work.


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

Hire Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB