Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-10-02 11:01:50

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

smd_random_text: Pull random items from the TXP environment

Since I’ve done it with images and other stuff, I figured I might as well tackle text as well.

Written with a cursory nod of appreciation to Drew McLellan and Manuel Ebert who have both made random text plugins before, this one takes it a step further. You can choose to grab your text from a pool given in a fixed string, a TXP field, a URL field, a TXP variable, a database or a file. And you’re not limited to just grabbing one item.

Each item you choose is optionally passed to a form/container where you can insert them into markup using the now-customary {replacement tag syntax}. Check the examples in the help for some gentle prods and ideas. I’m sure there are all sorts of wacky things you can randomize, like pulling an entry or two out of a sed_pcf.

There’s plenty of scope for extensions to this one if anyone has any ideas. Possible things to do:

  • Matching/excluding to pull out subsets of items
  • Define more than one list in a single file: like an old skool .ini file with interior sections
  • Fix the code if it proves that writing a plugin in 1 hour at 1:30am is a bad idea
  • Anything else?

Here is the latest version for your comfort and enjoyment: download
Any ideas or problems, you know where I live.

Revision history
————————

All available versions and changes are listed here. Each entry indexes the relevant post(s) in this thread to learn about the features.

Last edited by Bloke (2011-05-03 00:07:23)


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 2009-01-21 19:10:14

Liiint
Member
From: Tacoma, WA
Registered: 2004-10-01
Posts: 18
Website

Re: smd_random_text: Pull random items from the TXP environment

Is anybody else having problems with this plugin on Txp 4.07. It stopped working after the upgrade and I don’t if it’s something else I did, or the upgrade itself.

Thank you.

Offline

#3 2009-01-21 19:20:14

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_random_text: Pull random items from the TXP environment

Ops, I’ve installed it on a 4.0.7 but haven’t had time to test it yet! Also would like to know if it works!


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#4 2009-01-21 19:25:00

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

Re: smd_random_text: Pull random items from the TXP environment

Liiint wrote:

Is anybody else having problems with this plugin on Txp 4.07.

Congratulations on being the first person (besides myself) to use it!

But I wouldn’t have thought it was the plugin: it’s so dim and simple it should run on any TXP version! It is working for me under 4.0.7 but there are so many ways to use it, anything’s possible. Would you mind posting the tag you are using that is causing problems and any associated information, and I’ll see if I can work out if the plugin’s to blame. Also, when you say “stopped working”, are there any errors if you put your site in debugging mode?


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 2009-01-21 19:35:03

Liiint
Member
From: Tacoma, WA
Registered: 2004-10-01
Posts: 18
Website

Re: smd_random_text: Pull random items from the TXP environment

Okay. It is NOT the plugin. It’s working again. During our backup and restore process during said upgrade, a certain file went missing. It’s back now and the world is a better place.

Offline

#6 2009-01-27 04:44:56

hamsteralliance
Member
Registered: 2007-12-13
Posts: 10

Re: smd_random_text: Pull random items from the TXP environment

Make that two people. ;)

Here’s my newbish question:

I plan on adding a quote of the day type thing, but I’m not really grasping how to get the plugin to pull the quotes from a textpattern article, preferably one that’s not live and set to hidden or whatever other setting will stop the full list of quotes from appearing on my site. :P

Any help would be greatly appreciated!

Offline

#7 2009-01-27 10:13:15

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

Re: smd_random_text: Pull random items from the TXP environment

hamsteralliance wrote:

I plan on adding a quote of the day type thing, but I’m not really grasping how to get the plugin to pull the quotes from a textpattern article

Hi, if you want to use an article to hold the quotes there are a couple of ways to approach it. I believe if you “hide” the article TXP won’t be able to see it so you’ll need to make it visible. But we can hide it in other ways.

Method 1:

  • Make a Section to hold your quotes article and set it to not be searched/seen on front page/etc
  • Make an Article and put the quotes in, say, Excerpt for now like this:
"All that we see or seem is but a dream within a dream."
-- Edgar Allan Poe
|
"Assassination is the extreme form of censorship."
-- George Bernard Shaw 
|
...
  • Put your smd_random_text tag in the body so it will read the quotes from its own excerpt field:
<txp:smd_random_text type="field" source="excerpt" />
  • Assign the Article to your new Section and save it
  • To output your quote you can simply use <txp:article_custom section="quotes" limit="1" /> (or perhaps use id="ID_of_article"). You may have to specify a different form too — one that only outputs <txp:body />, but that depends on the layout of your default Form

Method 2 (slightly easier):

If you are going to the trouble of creating a form you might as well do it this way instead:

  • Create the same Section as above
  • Create a Form called “quotes” and put this in it:
<txp:smd_random_text type="field" source="body" />
  • Create your Article and put your quotes — in the same format as in Method 1 — in the Body field
  • Assign it to your Section
  • Under Advanced options, set Override form to quotes, and save the article
  • Wherever you want the quote to appear, use a <txp:article_custom /> tag as above with the ID of the Quotes article

Either of those methods (or a combination of forms/containers depending on your preferred way of working in TXP) will do the job. As long as you never link to the “hidden” section, nobody will be any wiser that it exists :-)

Does that help, or at least give you some ideas about how to approach it?

Last edited by Bloke (2009-01-27 10:15:30)


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

#8 2009-01-27 10:39:04

hamsteralliance
Member
Registered: 2007-12-13
Posts: 10

Re: smd_random_text: Pull random items from the TXP environment

Sounds good to me!

Now I just gotta figure out how to get it in the spot I want it in. Thanks! :D

Offline

#9 2009-01-27 11:17:03

hamsteralliance
Member
Registered: 2007-12-13
Posts: 10

Re: smd_random_text: Pull random items from the TXP environment

I’ve actually tried it out now and it’s not working as well as planned. :)

I created the section, the form and the article with a few lines of text in it.

Wherever I use the article_custom code you have up there it ends up posting the entire article and attaching the author name and timestamp to it too.

like this: http://www.hamsteralliance.com/upload/donotwant.gif

Any ideas? :D

Last edited by hamsteralliance (2009-01-27 11:17:20)

Offline

#10 2009-01-27 11:40:51

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

Re: smd_random_text: Pull random items from the TXP environment

hamsteralliance wrote:

Wherever I use the article_custom code you have up there it ends up posting the entire article and attaching the author name and timestamp to it too.

That’s because it’s rendering the article using the default Form, which tells it to display a title, the body, the author, the timestamp, the categories, etc etc etc. What you need is a form that displays a single thing: the body. In fact, if you’re using TXP 4.0.7 or higher, you can take advantage of the fact that the article tags can be containers. Try this:

<txp:article_custom id="blaaah">
  <txp:body />
</txp:article_custom>

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 2009-04-11 08:17:24

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

Re: smd_random_text: Pull random items from the TXP environment

Slight update to add trim and escape attributes. Download


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

#12 2009-05-04 20:05:20

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_random_text: Pull random items from the TXP environment

Have good idea. I want to use this plugin to output different “read more” texts: “read more about this article”, “try to get this”, “some facts about”.. and so on..

But bad thing is that every time page loads this text will change. And i want to make this random text to be stable :) So, i offer to add some attr, named, for example, unqizer :) And the example of using:

<txp:smd_random_text uniqizer='<txp:article_id' />
In this case, every article will have uniq and stable random text :)

How to make this? May be you can get md5() of uniqizer and then convert it in number of strings to offset in array of random strings.


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

Board footer

Powered by FluxBB