Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-09-27 11:06:49

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,271
GitHub

Best approach to build dynamic article body content

I would like an article to include static and dynamic content, with the dynamic content provided as an array. How would you approach this, with current Textpattern tooling in mind:

“Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tristique [tellus non OR purus accumsan], non [sodales OR neque OR blandit]. Nam fermentum magna sit amet sagittis fermentum.”

Content should be in the article body, not a form. I’m not worried about query overhead since it’s only me accessing the article, so function trumps form in this instance.

Last edited by gaekwad (2021-09-27 11:07:17)

Offline

#2 2021-09-27 12:58:02

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

Re: Best approach to build dynamic article body content

There are probably ways to do that with a shortcake and php’s array_rand function using txp:yield to pass in the strings, but you could also use adi_list and possibly also rah_repeat with random sort and limit="1".


TXP Builders – finely-crafted code, design and txp

Offline

#3 2021-09-27 13:10:38

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,271
GitHub

Re: Best approach to build dynamic article body content

Thank you, jakob – much appreciated! I especially like:

jakob wrote #331667:

There are probably ways to do that with a shortcake

Best typo of the month!

Offline

#4 2021-09-27 18:56:54

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

Re: Best approach to build dynamic article body content

Shortcake! Mmmmm.

There are also a couple of plugins that might help if you’re so inclined: cbe_rand_content and wet_lorem_ipsum. Both generate random content to varying degrees.

If you’re after something to build on the fly at install, you could maybe pre-populate a Txp instance with article content generated by those plugins, export it sans ID column and then randomly pluck the content via a script at build time. That would randomize the order as it built the new IDs each time.


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 2021-09-27 20:15:19

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,271
GitHub

Re: Best approach to build dynamic article body content

Thanks, Bloke!

Offline

#6 2021-09-28 08:03:25

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

Re: Best approach to build dynamic article body content

Let us put the question the other way: what would you consider the best core approach? We have 2 months yet before 4.8.8 release…

Offline

#7 2021-09-28 10:02:55

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,271
GitHub

Re: Best approach to build dynamic article body content

etc wrote #331671:

what would you consider the best core approach?

I realise the irony of me being the original poster and having this opinion…but…I don’t know.

The gist of adi_list looks pretty sensible to me (edit: and Jukka’s approach in rah_repeat looks just fine), but this is not something I’ve done in anger (yet), so I’ll defer to the folks here who far smarter at this.

Last edited by gaekwad (2021-09-28 10:04:08)

Offline

#8 2021-09-28 10:24:35

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

Re: Best approach to build dynamic article body content

Totally misread your OP, sorry. A combination of static and dynamic content is more challenging. But a shortcode is the way to go, imo, as a placeholder for dynamic data in your article flow.

The question is where to store the data that’s pulled in. If it was attached to the article (e.g. in a custom field) then there’s not much need for dynamicism. Just include the set in the article body. I do that with my own site using the smd_random_text plugin.

If you’re farming out the dynamic portions to someone else to keep updated then consider a pageless section called ‘snippets’ that you use to hold the content for each article. With a suitable naming convention to your articles, you can tie them together (by ID or by title or category or custom field or something).

You could quite simply list the options in the body field of each article in your snippets section. Then in your shortcode, use adi_list with newline as a separator to build the list and then grab an entry from that array. Or you could use a jot of PHP to do the same.

The tricky thing is if you want to use the same list to pull multiple bits of info and don’t want the same item to be pulled out twice. That means you might need some history, or somehow know how many you’re going to need in advance and pull them all out at once then iterate over them to inject them into your placeholders in the main article.

Non-trivial but doable.


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

#9 2021-09-28 13:21:13

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

Re: Best approach to build dynamic article body content

gaekwad wrote #331668:

Best typo of the month!

Freudian slip 😋

Bloke wrote #331676:

If it was attached to the article (e.g. in a custom field) then there’s not much need for dynamicism. Just include the set in the article body. I do that with my own site using the smd_random_text plugin.

Ha, I knew there had to be specially-made tool for this. Good stuff!


TXP Builders – finely-crafted code, design and txp

Offline

#10 2021-10-17 09:07:12

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

Re: Best approach to build dynamic article body content

Some related news.

Offline

Board footer

Powered by FluxBB