Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-04-23 14:10:36

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

etc_query: all things Textpattern

Description

This multipurpose plugin allows you to pick some data, transform it and display the result the way you like. It can query HTML/XML/JSON/LDAP documents, databases, arrays or global variables, and can be useful for

  • importing data from external sites/feeds
  • querying databases
  • retrieving values of global variables
  • performing all kind of calculations
  • iterating over arrays/ranges
  • cleaning poor article markup
  • customizing the output of Textpattern tags and other plugins
  • creating navigation links, pagination, and much more.

It is in many points inspired by smd_xml and smd_query, but uses XPath to access the desired nodes.

Requirements

  • PHP 5.3.10 with DOM extension (enabled by default)
  • Basic knowledge of XPath

Syntax

The workflow is: import some data, query it for some nodes with XPath, do some {replace}ments and display the result. For example,

<txp:etc_query data='<txp:body />' replace="//@style" />

in an article form will display the body of the article with all style attributes removed.

<txp:etc_query url="http://textpattern.com" query="//div[@id='accoladesBlock']//img" replace="@width=128">
  {.} : {@alt?}
<txp:else />
  <p>Nobody here.</p>
</txp:etc_query>

will display all img elements (scaled to width=128) with their alt text found in the div#accoladeBlock on textpattern.com.

<txp:etc_query data="SELECT * FROM txp_users WHERE 1" wraptag="ul" break="li">
  {$strtoupper({RealName?})}
</txp:etc_query>

will display an uppercase list of all users of your site.

<txp:etc_query name="sqrt" globals="_GET" functions="sqrt" query="sqrt({?num|0|intval})" />

will merely calculate the square root of HTML GET num variable, sanitized as integer, and assign it to <txp:variable name="sqrt" />.

More help and examples in plugin’s help, on TXP Forum thread or plugin home.

Plugin code

Download the latest version.

Last edited by etc (2015-09-11 17:40:08)

Offline

#2 2012-04-23 14:22:54

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: etc_query: all things Textpattern

Hi etc,

I’ve upgraded your status as plugin author and moved the topic to the Plugin Author Support forum.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2012-04-23 14:33:11

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

Re: etc_query: all things Textpattern

Thank you colak, nice to join the club!

Some snippets:

Prefetching and TOC

The article form

<txp:if_individual_article>
  <txp:etc_query data="{?body}" query="//h2|//h3|//h4|//h5" label="Table of Contents" labeltag="h1" />
  <txp:body />
<txp:else />
  <txp:excerpt />
  <txp:etc_query data="{?body}" query="//img">
    <link rel="prefetch" href="{@src?}" />
  </txp:etc_query>
</txp:if_individual_article>

will pre-load the images found in articles’ bodies while viewing the excerpts, and produce a table of contents in the individual article view.

Customizing hard-coded txp tags
<txp:etc_query data='<txp:comments_form />' query="//textarea[@id='message']" replace="@@required=required@placeholder=Drop us a word" />

will add HTML5 required and placeholder attributes to the message field of the comments form.

Importing RSS feeds
<txp:etc_query url="https://textpattern.com/rss/"
  markup="xml"
  query="//channel/item[position()<6]"
  wraptag="dl"
  label="News from Textpattern Forum" labeltag="h4">
    <dt><a href="{link?}">{title?}</a></dt>
    <dd>{description?}</dd>
</txp:etc_query>
Multiplication table :-)
<txp:etc_query data="[1..9]" markup="json" break="tr" wraptag="table">
<txp:etc_query data="[1..9]" markup="json" break="td">
{{$*{?}}}
</txp:etc_query>
</txp:etc_query>

More examples: here or here or here.

Last edited by etc (2012-09-09 13:49:34)

Offline

#4 2012-04-23 18:49:07

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Re: etc_query: all things Textpattern

Yes! Thank you very much for this plugin.

Offline

#5 2012-04-24 10:58:06

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

Re: etc_query: all things Textpattern

maruchan wrote:

Yes! Thank you very much for this plugin.

Nice to know you find it useful, thank you.

Version 0.4: added remove and separator attributes.

Last edited by etc (2012-04-24 11:25:34)

Offline

#6 2012-04-26 09:15:31

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

Re: etc_query: all things Textpattern

Version 0.5: fixed html markup, enhanced replace attribute.

Offline

#7 2012-04-29 22:09:39

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

Re: etc_query: all things Textpattern

Version 0.6 (see history)

Offline

#8 2012-04-29 22:16:55

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

Re: etc_query: all things Textpattern

Not had a chance to try this yet, but it sounds very intriguing and you’ve given lots of examples in different threads of how it could be of use. Thanks for writing it – it looks like it could be a swiss-army-knife type of plugin.

One thing with the latest change, I’m not sure whether dropping “dom” from the name might lead to misunderstandings, though. There is already smd_query which builds mysql queries. Both plugins now have near identical names but do different things.

Last edited by jakob (2012-04-29 22:21:05)


TXP Builders – finely-crafted code, design and txp

Offline

#9 2012-04-30 08:56:02

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

Re: etc_query: all things Textpattern

Version 0.61: minor tweaks to improve usability.

jakob wrote:

Not had a chance to try this yet, but it sounds very intriguing and you’ve given lots of examples in different threads of how it could be of use. Thanks for writing it – it looks like it could be a swiss-army-knife type of plugin.

One thing with the latest change, I’m not sure whether dropping “dom” from the name might lead to misunderstandings, though. There is already smd_query which builds mysql queries. Both plugins now have near identical names but do different things.

Hi jakob, and thank you for the comment. I have hesitated before changing the name (and I don’t like doing it, really), but v0.6 was not fully compatible with v0.5 (still usable, but depreciated). I apologize if it hurts the users, but I don’t believe there are many, the plugin is brand new.

And, concerning smd_query, who knows if one day we couldn’t query a db with xml?

Offline

#10 2012-05-02 16:03:56

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

Re: etc_query: all things Textpattern

Version 0.7: added functions, limit and offset, enhanced replace attribute.

Offline

#11 2012-05-02 16:18:07

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: etc_query: all things Textpattern

supremely useful.

Offline

#12 2012-05-02 21:53:31

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

Re: etc_query: all things Textpattern

Supremely flattered!:) I was amazed by the many things one can do with xpath, hope it will be used. Thank you!

Offline

Board footer

Powered by FluxBB