Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Using random numbers in Textpattern variables
Take the following code:
<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />
<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />
That creates a variable called test-rng
, and assigns the result of echo rand(1,999);
, which is a random integer between 1 and 999, and then outputs the variable test-rng
four times.
In each case, the variable test-rng
gives a different integer between 1 and 999 (for example: 437
, 21
, 993
, 487
).
What I would really like to do is set a variable with the result of echo rand(1,999);
, and lock that number in until it’s reset or unset later in the code.
Is such a thing possible? How would I achieve this?
Thank you in advance!
Offline
Re: Using random numbers in Textpattern variables
How about:
<txp:variable name="test-rng"><txp:php>
static $a_random_number = null;
if ($a_random_number === null) {
$a_random_number = rand(1,999);
}
echo $a_random_number;
</txp:php></txp:variable>
EDIT: That might not allow you to easily unset later in the code, though. Statics can be quite persistent.
Last edited by Bloke (2018-02-27 15:02:11)
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
Re: Using random numbers in Textpattern variables
Hi Pete,
I’m not good with php but I am just about Ok with txp:) Did you try the following?
<txp:variable name="test-rng" value='<txp:php>echo rand(1,999);</txp:php>' />
>Edit… Stef beat me to it:) Almost certainly with the proper solution.
Last edited by colak (2018-02-27 15:02:13)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Using random numbers in Textpattern variables
Hey colak – thank you, I had already tried the single and double quotes swap, no luck with that.
Thanks Stef – worked perfectly, and the static
can be unset easily enough:
<txp:variable name="test-rng"><txp:php>
static $a_random_number = null;
if ($a_random_number === null) {
$a_random_number = rand(1,999);
}
echo $a_random_number;
</txp:php></txp:variable>
<p>Blokemagic©</p>
<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />
<p>unset here</p>
<txp:variable name="test-rng" value=""/>
<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />
<p>echo rand() here</p>
<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>"/>
<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />
<txp:variable name="test-rng"><txp:php>
static $a_random_number = null;
if ($a_random_number === null) {
$a_random_number = rand(1,999);
}
echo $a_random_number;
</txp:php></txp:variable>
<p>reset to Blokemagic©</p>
<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />
…which outputs:
Blokemagic©
486 486 486
unset hereecho rand() here
282 821 933
reset to Blokemagic©462 462 462
Invoice in the post to the usual address, please and thanks.
Offline
Re: Using random numbers in Textpattern variables
gaekwad wrote #309472:
Hey colak – thank you, I had already tried the single and double quotes swap, no luck with that.
Yiannis code works for me:
<txp:variable name="test-rng" value='<txp:php>echo rand(1,999);</txp:php>' />
<txp:variable name="test-rng" />
<txp:variable name="test-rng" />
<txp:variable name="test-rng" />
And no need to complicate with statics, this works as well
<txp:variable name="test-rng">
<txp:php>echo rand(1,999);</txp:php>
</txp:variable>
For the record, with double quotes in
<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />
every <txp:variable name="test-rng" />
call will output the literal <txp:php>echo rand(1,999);</txp:php>
string, which will be processed on the second pass. A nice usecase, BTW.
Offline
Re: Using random numbers in Textpattern variables
etc wrote #309473:
Yiannis code works for me:
Yiannis – I apologise – your code does work. I must have mental wires crossed somewhere this afternoon. Very sorry!
etc wrote #309473:
And no need to complicate with statics, this works as well
<txp:variable name="test-rng">...
For the record, with double quotes in
<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />...
every
<txp:variable name="test-rng" />
call will output the literal<txp:php>echo rand(1,999);</txp:php>
string, which will be processed on the second pass. A nice usecase, BTW.
Thank you, Oleg – very much appreciated!
Offline
Re: Using random numbers in Textpattern variables
Nice! Didn’t realise that would occur, thanks Oleg. And the second pass feature is neat. I almost went with some clever use of second pass/output_form/evaluate to allow Pete to ‘reset’ the value without having to redeclare it.
I’m sure it’s possible to pass in an attribute that’ll “seed” it with a random value and then on subsequenet calls, pass in something else that will always “return” the set value until it’s seeded again.
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