Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-11-10 12:54:08

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

Quoting lists in custom fields

I feel it must be possible to quote a list stored in a custom field so it can be included in a tag but the mechanism escapes me. Here’s an example of what I have now:

<p>Jump to:
<category::list categories='starters, mains, side-dishes, extras-homemade-sauces, desserts-drinks' break="|">
   <a href="#<txp:category />"><txp:category title /></a>
</category::list>
</p>
<txp:article_custom
   section='<txp:article_url_title />'
   sort="FIELD(category1, 'starters', 'mains', 'side-dishes', 'extras-homemade-sauces', 'desserts-drinks'), custom_1"
   class="menu-list"
   form="menu_listing"
   limit="999"
   wraptag="table" />

Pretty simple: it creates a menu of items in a table and adds a jumplist at the top so you can jump to each section.

But instead of repeating the list of categories, I want to store them somewhere, e.g. a custom field in the article, because each menu (article) might use a different categorisation scheme or order (lunchtime specials, a la carte, etc).

The menu items themselves are all in a (pageless) section that matches the article URL title (so the client can manage the menu via adi_matrix to update prices and dishes).

Note that the two lists are quoted differently. The first is a straight list, the second is quoted. If I store the list of categories in a custom field called CategoryOrder like this:

starters, mains, side-dishes, extras-homemade-sauces, desserts-drinks

Can I transform that into:

'starters', 'mains', 'side-dishes', 'extras-homemade-sauces', 'desserts-drinks'

?

<txp:custom_field name="CategoryOrder" break="', '" escape="quote" />

renders this:

concat('starters',"'",' mains',"'",' side-dishes',"'",' extras-homemade-sauces',"'",' desserts-drinks')

Without the break, I get the outer quotes but not inner ones between each item. Do I need to mess with breakform and <+> to get this to do what I want?

Further, can I embed this custom field construction into a sort attribute or will it just explode if I do something like this:

sort='FIELD(category1, <txp:custom_field name="CategoryOrder" escape="blah blah" />), custom_1'

Any thoughts welcome.


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 2021-11-10 15:23:47

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

Re: Quoting lists in custom fields

The quick-n-dirtiest option might be

<txp:custom_field name="CategoryOrder" trim="/\s*,\s*/" replace="','" wraptag="'" />

When using it in sort as above, you probably should duplicate '' quotes, I have not tested it.

FWIW, escape="quote" intent is to quote tags output for use in query attribute of <txp:evaluate />. It does not split/quote lists neither interplays with break etc. We could add escape="quotelist" or escape="dblist" if nesessary.

Offline

#3 2021-11-10 15:58:46

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

Re: Quoting lists in custom fields

Ah yes, find ‘n replace. Forgot about that, thanks. As you say, escaping the quotes for the sort attribute might be prudent. I’ll give it a whirl, thank you.

Not averse to some additional *list transforms if they make sense.


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 2021-11-10 16:12:11

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

Re: Quoting lists in custom fields

Actually, now that break and breakby are (really) global, making escape act on items level looks more clever. Then one could obtain what you need with

<!-- breaks the list, db-escapes its items and implodes it again -->
<txp:custom_field name="CategoryOrder" breakby break="," escape="db, quote" />

Not fully bwc, but the difference should be marginal.

Offline

#5 2021-11-10 17:13:18

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

Re: Quoting lists in custom fields

The find/replace worked a treat, that’s brilliant. The second one looks neater but just quoted the entire set. Unless I need to pull a later version of 4.8.8-dev down.

Final sort attribute with double apostrophes to escape the fact I’m using single quotes to surround the attribute for parsing:

sort='FIELD(category1, <custom::field name="CategoryOrder" trim="/\s*,\s*/" replace="'',''" wraptag="''" />), custom_1'

Nice!


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 2021-11-11 20:54:40

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

Re: Quoting lists in custom fields

Bloke wrote #331929:

The second one looks neater but just quoted the entire set. Unless I need to pull a later version of 4.8.8-dev down.

Just landed in 4.8.8, please try

sort='FIELD(category1, <custom::field name="CategoryOrder" break="," escape="db, quote" />), custom_1'

Offline

#7 2021-11-13 20:28:11

lazlo
Member
Registered: 2004-02-24
Posts: 110

Re: Quoting lists in custom fields

This looks settled but the rah-repeat plugin will do all that with sort, exclude and more.

https://rahforum.biz/plugins/rah_repeat
https://forum.textpattern.com/viewtopic.php?id=32384&p=5

<txp:rah_repeat <txp:custom_field name="CategoryOrder" wraptag="ul" break=",">
	'<txp:rah_repeat_value />'
</txp:rah_repeat>
<txp:rah_repeat value="starters, mains, side-dishes, extras-homemade-sauces, desserts-drinks" wraptag="ul" break=",">
	'<txp:rah_repeat_value />'
</txp:rah_repeat>

should return

<ul>
	'starters', 'mains', 'side-dishes', 'extras-homemade-sauces', 'desserts-drinks'
</ul>

Offline

Board footer

Powered by FluxBB