Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-05-27 14:46:12

Myusername
Member
Registered: 2019-12-12
Posts: 162

How to check if multiple parameters have been defined?

I need to check that several URL parameters have been set, is there a cleaner way to do this than what is shown below? In the example I only tested title and body, but I will use many other parameters, and it will be a long code, I wanted to avoid so many ifs, if possible

<txp:if_request type="get" name="title">
  <txp:if_request type="get" name="body">
    true
  </txp:if_request>
</txp:if_request>

Offline

#2 2022-05-27 21:02:34

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

Re: How to check if multiple parameters have been defined?

Myusername wrote #333448:

I need to check that several URL parameters have been set, is there a cleaner way to do this than what is shown below?

Yes, according to the docs you can use the attribute match="any" to test if one of a list of comma-separated values is matched, or match="all" to test for multiple values presence at once.

EDIT: Hmm, maybe not after all. Maybe you can just check against multiple values, not multiples names. If you have a lot of values, perhaps try using rah_repeat to loop over the list and test instead for the not exists case to trigger some output into a variable when the condition doesn’t apply. Check that variable to see if it is empty to determine if you have match for all names.

EDIT II: You might be able to use txp:php and the gpsa function to test multiple get parameters at once, and then test if false is in your array with php. It also loops over the individual get parameters, so I’m not sure whether it’s that much faster.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2022-05-28 06:24:21

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,529
Website GitHub Twitter

Re: How to check if multiple parameters have been defined?

Hi
For this kind of task i use smd_if plugin you can check multiple values in one test with different logic as you want.
Cheers.

Offline

#4 2022-05-28 12:15:22

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

Re: How to check if multiple parameters have been defined?

I concur, the only way for checking multiple request variables seems to be a loop, though we could amend it if necessary. This said, in 4.8.8 loops are not difficult. Create a form, say ifrequest:

<txp:if_request not type="get" name='<txp:yield item />' />

Now you can save the result of request tests in a variable and then use it as needed:

<txp:variable name="norequest" value="title, body" breakby breakform="ifrequest" />

<txp:if_variable name="norequest">
    false
</txp:if_variable>

Offline

#5 2022-05-29 06:00:54

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

Re: How to check if multiple parameters have been defined?

That is simply genius.


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

Online

Board footer

Powered by FluxBB