Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-08-18 18:44:35

towndock
Member
From: Oriental, NC USA
Registered: 2007-04-06
Posts: 315
Website

Two forms, each display 50% of the time

I have a requirement to display the output of a Textpattern form 50% of the time. There are two forms – each would display half the time.

This may be an easy one, but an obvious Textpattern tag hasn’t yet occurred to me.

Ideas?

Offline

#2 2021-08-18 19:59:10

etc
Developer
Registered: 2010-11-11
Posts: 4,548
Website GitHub

Re: Two forms, each display 50% of the time

You should precise what half the time means: randomly with 1/2 probability (easy), or alternatively. In the latter case, should it be per user (more tricky)?

Offline

#3 2021-08-18 20:13:18

towndock
Member
From: Oriental, NC USA
Registered: 2007-04-06
Posts: 315
Website

Re: Two forms, each display 50% of the time

The “easy” path will be fine.

Offline

#4 2021-08-18 20:18:24

etc
Developer
Registered: 2010-11-11
Posts: 4,548
Website GitHub

Re: Two forms, each display 50% of the time

towndock wrote #331426:

The “easy” path will be fine.

Then you create, say, form1 and form2 and call

<txp:output_form form='form<txp:php>echo rand(1,2);</txp:php>' />

This should call each one half of time in average.

Edit: and if you need to name the forms differently, this should do:

<txp:variable name="form" trim>
<txp:php>
$forms = ['first_form', 'second_form'];
echo $forms[rand(0,1)];
</txp:php>
</txp:variable>

<txp:output_form form='<txp:variable name="form" />' />

Offline

#5 2021-08-18 21:14:26

towndock
Member
From: Oriental, NC USA
Registered: 2007-04-06
Posts: 315
Website

Re: Two forms, each display 50% of the time

Sweet. Thank you, I’ll be trying this out tomorrow.

Offline

#6 2021-08-19 08:09:05

etc
Developer
Registered: 2010-11-11
Posts: 4,548
Website GitHub

Re: Two forms, each display 50% of the time

Since pipes (|) are forbidden in form names, in 4.8.8 it will be possible to output random forms with

<txp:output_form form="some form|another form|yet another form" />

Offline

Board footer

Powered by FluxBB