Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-10-09 16:42:00

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 992
Website

How to access Textpattern's API in PHP?

Hi, everyone!

For years, I have found edge cases where I need to access Textpattern’s API in PHP: usually merely a $thisarticle['title'] or $variable here and there.

For one template I’m currently working on, it would really help if I could use Textpattern tags and their output without hopping in and out of txp:php tags.

Equivalent programmatic names

All Textpattern tags have equivalent programmatic names which are exactly the same as the tag names. For example, <txp:recent_articles /> is recent_articles().

Arrays must be passed to all functions

You must pass an array to all tag functions, even if there are no attributes to set. For tags that require no attributes or those that you do not wish to modify the defaults, pass an empty array, e.g. category1(array());.

Source: https://docs.textpattern.com/tags/php#equivalent-programmatic-names

That sounds great, but I immediately run into a puzzle. Say I want to iterate through the file downloads, what is the equivalent to using the tag as a container?

file_download_list([ 'limit' => 10]) // …?

And more generally, is there a way to inspect the state of variables available within any given function?

Thanks in advance for any guidance you can offer!

Last edited by johnstephens (2020-10-09 16:43:35)

Offline

#2 2020-10-09 17:33:25

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,776
Website

Re: How to access Textpattern's API in PHP?

If you give us a bit of detail about what you’re trying to achieve, we may be able to furnish you with ideas on ways to do it with native tags.

Especially with the advent of txp:evaluate, and some pretty insane flexibility in txp:variable and txp:if:variable, as well as shortcodes and txp:yield, there is usually little need to drop into PHP.

Generally, operating on container tags in PHP is… fun. Not recommended!


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

#3 2020-10-09 18:08:34

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 992
Website

Re: How to access Textpattern's API in PHP?

Thank you, @Bloke!

In this case, what I’m trying to do is a basic reduction function. I want to get the download counts of certain files whose IDs are specified in custom fields for the current article, and create a variable with their sum. The purpose is to display a cumulative download count that includes various file formats.

In the past, I created a variable for the sum, like so:

<txp:variable name="pax_article_download_counter" value="0"/>

Then, I check if the custom field has data; if so, I used the native file_download tag to summon up the file info. From there, I was using the adi_calc plugin to combine the download count with the current sum:

<txp:if_custom_field name="pdf_fileid">
    <txp:file_download id='<txp:custom_field name="pdf_fileid"/>'>
        <txp:adi_calc name="pax_article_download_counter" add='<txp:file_download_downloads/>'/>
    </txp:file_download>
</txp:if_custom_field>

…repeating that block for each custom field containing a file ID.

Looking back, if feels clunky—especially now that I understand the concept of array reduce.

Also, I want to drastically reduce my dependence on 3rd-party plugins if possible. Upgrading my older Textpattern sites has gone from a few shell commands and logging in, taking about 5-minutes, to about a week of testing every single plugin and template file to make sure that nothing has gone awry. I know that getting rid of adi_calc isn’t much, but it’s one fewer thing I have to check at upgrade-time.

At this point, I have replaced the above block with the following:

<txp:if_custom_field name="pdf_fileid">

    <txp:file_download id='<txp:custom_field name="pdf_fileid"/>'>

      <txp:variable name='this_download_count'><txp:file_download_downloads/></txp:variable>
      <txp:php>
        global $variable;
        $variable['pax_article_download_counter'] = $variable['pax_article_download_counter'] + $variable['this_download_count'];
      </txp:php>

    </txp:file_download>

</txp:if_custom_field>

Last edited by johnstephens (2020-10-09 18:11:20)

Offline

#4 2020-10-09 19:37:19

etc
Developer
Registered: 2010-11-11
Posts: 3,934
Website

Re: How to access Textpattern's API in PHP?

That should be easy in 4.8:

<txp:if_custom_field name="pdf_fileid">
    <txp:file_download_list id='<txp:custom_field name="pdf_fileid"/>'>
      <txp:variable name='pax_article_download_counter' add='<txp:file_download_downloads/>' />
    </txp:file_download_list>
</txp:if_custom_field>

Offline

#5 2020-10-10 09:27:00

etc
Developer
Registered: 2010-11-11
Posts: 3,934
Website

Re: How to access Textpattern's API in PHP?

And it will be even easier in 4.8.4, something like

<txp:if_custom_field name="pdf_fileid">
    <txp:file_download_list id='<txp:custom_field name="pdf_fileid"/>' fields="sum(downloads)">
      <txp:file_download_downloads />
    </txp:file_download_list>
</txp:if_custom_field>

Offline

Board footer

Powered by FluxBB