Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-05-14 06:01:51

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

detect numeric variables

For some time now, I’ve been thinking of devising a method that would allow me to use a custom field that would either use article ids or website addresses of external sites.

<txp:variable name="alphanumeric"><txp:custom_field name="url" /></txp:variable>
<txp:php>
$tests = array(
    '<txp:variable name="alphanumeric" />',
);

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo var_export() . '<a rel="canonical" href="<txp:permlink id="<txp:variable name="alphanumeric">" />"><txp:title /></permlink>', PHP_EOL;
    } else {
        echo var_export() . '<a href="$element" rel="external">Website</a>', PHP_EOL;
    }
}
</txp:php>

I did not quite make it to work yet but I’m almost there. Posting here to see if anyone can see where I’m screwing up.


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

Online

#2 2022-05-14 07:37:04

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

Re: detect numeric variables

Hi Yiannis, IIRC you need to use parse('<txp:function />') in php blocks, or alternatively call the tags as php functions directly passing in the variables.

You can also get Textpattern variables in php by doing:

global $variable; 
$var= $variable['txp_var_name'];

which could make tag-in-tag variants easier.

But… I was wondering whether you could do this more txp-like with the help of rah_repeat? As far as I remember, you’ve been using rah_repeat on neme (and if not, it’s infinitely useful. Note: it’s been recently updated for PHP 8.1).

Try this:

<txp:rah_repeat value='<txp:custom_field name="url" />'>
    <txp:evaluate query='is_numeric("<txp:rah_repeat_value />")'>
        <txp:permlink id='<txp:rah_repeat_value />' />
    <txp:else />
        <a href="<txp:rah_repeat_value />" rel="external">Website</a>
    </txp:evaluate>
</txp:rah_repeat>

You also need to add is_numeric to the list of PHP functions enabled for use with txp:evaluate under Admin › Preferences › Advanced Options.

EDIT: just tested it and it does work.

EDIT 2: I thought txp:permlink automatically added the correct title, but I guess that is only within an article(_custom) loop, so to show the respective article’s titles (as opposed to the title of the article that contains the custom_field), you need to do:

<txp:article_custom id='<txp:rah_repeat_value />'><txp:permlink><txp:title /></txp:permlink></txp:article_custom>

TXP Builders – finely-crafted code, design and txp

Offline

#3 2022-05-14 08:23:50

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

Re: detect numeric variables

Hi Julian,

I do not understand why rah_repeat would be of use here.

Why would this

<txp:variable name="alphanumeric" value='<txp:custom_field name="url" />'>
    <txp:evaluate query='is_numeric("<txp:variable name="alphanumeric" />")'>
        <txp:permlink id='<txp:variable name="alphanumeric" />'><txp:title /></txp:permlink>
    <txp:else />
        <a href="<txp:variable name="alphanumeric />" rel="external">Website</a>
    </txp:evaluate>

not work?

jakob wrote #333270:

You also need to add is_numeric to the list of PHP functions enabled for use with txp:evaluate under Admin › Preferences › Advanced Options.

At the moment I have

[js]
mediatype="application/javascript"
...
[html]
mediatype="text/html"

How would I add that function?


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

Online

#4 2022-05-14 08:57:11

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

Re: detect numeric variables

colak wrote #333272:

I do not understand why rah_repeat would be of use here.

I was assuming from the use of an array and loop in your example code that you have a custom field that contains several mixed items. For example, I used this to test in the custom field:

2,https://www.neme.org,1

But if you have either just an article id or just a link, you can drop rah_repeat as you suggest and do:

<txp:evaluate query='is_numeric("<txp:custom_field name="url" />")'>
    <txp:article_custom id='<txp:custom_field name="url" />'><txp:permlink><txp:title /></txp:permlink></txp:article_custom>
<txp:else />
    <a href="<txp:custom_field name="url" />" rel="external">Website</a>
</txp:evaluate>

That’s almost the same as your code except that:

  • there’s no need for the extra variable here, you can go straight with the custom field, and
  • using the custom field for id attribute in permlink works for the link but txp:title will show you the title of the host article (that contains the custom field), not the destination article. So you need to plug the id into article_custom instead as a wrapper.

How would I add that function?

Above the custom form templates types you show, there’s another field called PHP functions enabled in txp:evaluate. Just place is_numeric in there. If you already have something in there, just add it after a comment, e.g. date, is_numeric.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2022-05-14 13:58:02

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

Re: detect numeric variables

I was assuming from the use of an array and loop in your example code that you have a custom field that contains several mixed items. For example, I used this to test in the custom field:

Whoops! Indeed, it used to and then tried to simplify it but left the array there. In my tests, your latest suggestion, works as expected.


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

Online

#6 2022-05-14 20:25:45

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

Re: detect numeric variables

colak wrote #333284:

… works as expected.

👍 excellent!

I suspect you’re using this yourself, so the following is unlikely to be an issue but…

… I often find when I provide an URL field for other people to use, invariably someone forgets to include the https:// and the link then gets tagged onto the end of your domain causing a 404. It’s quite common on other sites too.

One way around this is to check the user input for completeness and add the http if it is missing. However, not every link is an external url, one could provide a link to another page on the site (for example a landing page which has no article id). PHP has a function “parse_url” that dissects an url nicely into its parts but unfortunately it fails to recognise an url if the protocol (the http(s)) is missing, so a simple (if not entirely failsafe) method I’ve used is to check a) if :// is missing in the url and b) the url doesn’t start with an /.

The above code (second variant) looks like this with the additional check.

<txp:evaluate query='is_numeric("<txp:custom_field name="url" />")'>
    <txp:article_custom id='<txp:custom_field name="url" />'><txp:permlink><txp:title /></txp:permlink></txp:article_custom>
<txp:else />
<txp:variable name="url"><txp:custom_field name="url" /></txp:variable>
<txp:php>
global $variable;
if (strpos($variable['url'], "://") === false && substr($variable['url'], 0, 1) != "/" ) {
    $variable['url'] = "http://" . $variable['url'];
}
</txp:php>
    <a href="<txp:variable name="url" />" rel="external">Website</a>
</txp:evaluate>

The above then provides a working URL* for all of the following:

https://www.neme.org
www.neme.org
neme.org
/another-url-on-this-site

Perhaps that helps someone.

*It’s hard to know if the destination link will be http or https or whether it has a www or not, so one has to rely on the destination site to do its http->https and non-www to www redirecting responsibly.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB