Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-01-08 12:33:34

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

Alphabetical list of articles in a section

Hi gurus,

I need to make an alphabetical article list that also shows the corresponding first character (A, B, C, etc) as a title, kind of like the ‘Complete list of Textpattern tags’ page here does.

I don’t want to show a title character when there are no articles that begin with that character.

Pretty sure I can use <txp:if_different> tag within a <txp:custom_article> container tag to achieve this – but I’m having a memory blank.

Can someone enlighten me please!? I can also then include the solution on the documentation pages as a tag example.

Cheers!

Offline

#2 2020-01-08 12:57:24

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

Re: Alphabetical list of articles in a section

Something like

<txp:article_custom sort="UPPER(Title)">
    <txp:if_different>
        <h2><txp:evaluate query='substring(<txp:title escape="quote" />, 1, 1)' escape="upper" /></h2>
    </txp:if_different>
    <h3><txp:title /></h3>
</txp:article_custom>

Offline

#3 2020-01-08 12:59:50

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

Re: Alphabetical list of articles in a section

Maybe:

<txp:article_custom section="tags" limit="999" sort="Title asc" …>
    <txp:if_different>
        <txp:php>echo substr(parse('<txp:title />'), 0, 1);</txp:php>
    </txp:if_different>
    <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>

where substr chops down the string from the start char (0) to the next char (1). The other important aspect is getting the sort order right.

Instead of the txp:php block you could use txp:evaluate if you register substr (and maybe parse?) first. Or you could use smd_wrap or rvm_substr as plugins.

EDIT: Oleg was faster (and neater) again :-)


TXP Builders – finely-crafted code, design and txp

Offline

#4 2020-01-08 13:01:09

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

Re: Alphabetical list of articles in a section

You beat me to it as well, Oleg! I went for that, but started wondering if we could perhaps use the global trim attribute. Unfortunately my regex-fu failed me when trying to come up with the inverse of trim="^.{0,1}" to discard everything except the first character.


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

#5 2020-01-08 13:07:06

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

Re: Alphabetical list of articles in a section

Functions available in XPath 1.0. And for trim solution you’d need some look-behind pattern, but it looks overkill here.

Offline

#6 2020-01-08 13:10:14

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

Re: Alphabetical list of articles in a section

That’s probably why I couldn’t manage it :) I tried ?! and tried anchoring to $ then matching from {1,}, all to no avail. An evaluate query is the right solution in this case!


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

#7 2020-01-08 13:12:10

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

Re: Alphabetical list of articles in a section

Hi Oleg,

This is great and works as intended.

However, I have a further requirement that I neglected to mention! :) I want the HTML structure to be like:

<section>
    <h3>A</h3>
    <div class="layout-text4col">
        <ul>
            <li>Article 1</li>
            <li>Article 2</li>
        </ul>
    </div>
</section>
<section>
    <h3>C</h3>
    <div class="layout-text4col">
        <ul>
            <li>Article 3</li>
        </ul>
    </div>
</section>

Any solution? Sorry for the extra requirements!

Offline

#8 2020-01-08 13:25:15

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

Re: Alphabetical list of articles in a section

Hi Phil,

a good breakby exercise if you want to avoid if_first/last danse :-) Untested, but try to create two misc-type forms:

<!-- alphabreak -->
<txp:evaluate query='substring(<txp:title escape="quote" />, 1, 1)' escape="upper" />

and

<!-- alphaform -->
<section>
    <h3><txp:yield item="breakby" /></h3>
    <div class="layout-text4col">
        <ul><+></ul>
    </div>
</section>

Then call

<txp:article_custom sort="UPPER(Title)" breakby="alphabreak" breakform="alphaform">
    <li><txp:title /></li>
</txp:article_custom>

Offline

#9 2020-01-08 13:37:15

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

Re: Alphabetical list of articles in a section

Bloke wrote #320933:

I tried ?! and tried anchoring to $ then matching from {1,}, all to no avail. An evaluate query is the right solution in this case!

Stef, just in case, regex in trim must be enclosed: trim="/something/". Otherwise it acts like trim(), not as preg_replace().

Offline

#10 2020-01-08 13:46:34

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

Re: Alphabetical list of articles in a section

etc wrote #320936:

regex in trim must be enclosed: trim="/something/". Otherwise it acts like trim(), not as preg_replace().

Gotcha, thanks for the tip. I wasn’t doing that, but in any case, it doesn’t fix my general lack of regex skills.

I’ll mention that in the attribute docs. The slash-thing, not my lack of regex skills :)


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

#11 2020-01-08 14:15:06

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

Re: Alphabetical list of articles in a section

etc wrote #320932:

Functions available in XPath 1.0. And for trim solution you’d need some look-behind pattern, but it looks overkill here.

Thanks for the pointer. So:

  • substr($string, 0, 1) is the php function and would need registering in the advanced options in the Admin › Preferences.
  • substring(string, 1, 1) is an XPath function and, I guess, does not need registering.
  • And the UPPER in sort="UPPER(Title)" is MySQL.

I’m writing this down as much to help me (and others) remember 😳!

etc wrote #320935:

a good breakby exercise…

This, too, I will need to familiarise myself with as it looks very powerful and gets around an age-old problem with markup structures and if_different. You showed another example of that not all that long ago. It does requires quite a few forms, though you did mention in the past that simpler breakby expressions can go straight into the attribute.


TXP Builders – finely-crafted code, design and txp

Offline

#12 2020-01-08 14:16:11

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

Re: Alphabetical list of articles in a section

Thanks Oleg, works perfectly! That’s some pro level tag-fu there. 😀

I didn’t even know you could use a form as a breakby. And I don’t pretend to know how it all works, but it does.

Offline

Board footer

Powered by FluxBB