Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-10-04 15:50:46

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

[howto] Highlight random words in a sentence

Not sure if this a tip or a lesson in smd-sado-masochism but for those that hate putting a dab of PHP in pages I think I’ve stumbled upon the most convoluted method of highlighting words in a sentence using tags and smd_ plugins. The premise is this:

  • You want to make some kind of a pullquote in a sidebar to pimp another article on your site
  • The teaser text to display, if used, is put in custom1
  • The text that is returned requires formatting such that words over 3 characters in length take on a random <span> element with a class of either normal, big, bigger or biggest (yes, non-semantic class names, tut tut)
<!-- Store the current article ID so we don't grab the teaser from the current article -->
<txp:if_individual_article>
   <txp:variable name="thisart" value='<txp:article_id />' />
</txp:if_individual_article>

<!-- Grab one random article's teaser sentence -->
<txp:smd_query query="SELECT ID, custom_1 FROM textpattern WHERE Section='articles' AND ID != '?thisart' AND custom_1 IS NOT NULL ORDER BY rand() LIMIT 1">
   <!-- Make a link to the article -->
   <txp:permlink id="{ID}" class="pullquote">
      <span class="quote_char">&ldquo;</span>

         <!-- Loop over each word in the sentence -->
         <txp:smd_each type="fixed" include="quote {custom_1}" paramdelim=" " subset="2">

            <!-- Check if the current word is >3 chars in length -->
            <txp:smd_if field="{smd_var_value}" operator="gt:LEN" value="3">

               <!-- If it is, choose a random class name from one of the following -->
               <span class="<txp:smd_random_text source="biggest | bigger | big | normal" />">{smd_var_value}</span>
            <txp:else />
               <!-- If the word is too short, just output it as normal.
                    If you remove the newlines from the example you'll need a space after the closing smd_if tag -->
               {smd_var_value}
            </txp:smd_if> 
         </txp:smd_each>
      <span class="quote_char">&rdquo;</span>
   </txp:permlink>
</txp:smd_query>

How’s that for madness! With a sprinkling of CSS you can now make random words in your automatic quotes bigger to highlight them. Change smd_if’s value="3" to a higher number if you have more intellectual content ;-)


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-10-04 16:15:02

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: [howto] Highlight random words in a sentence

It would be nice to see your code in action :)

Offline

#3 2009-10-04 16:17:40

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: [howto] Highlight random words in a sentence

From the description, it might be total madness to see it in action. Not that that wouldn’t be nice…


Code is topiary

Offline

#4 2009-10-04 16:57:30

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

Re: [howto] Highlight random words in a sentence

Els wrote:

It would be nice to see your code in action :)

If I could have posted span tags with style rules in the forum I’d have given a nice display of it stepping up the font sizes on a sentence. As it happens it’s on a development site so I can’t link to it yet, sorry. Here’s a screenshot instead:


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

Board footer

Powered by FluxBB