Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: Variable tweaks
etc wrote #337033:
This is because the valid symbols set is largely extended in dev (e.g.
<txp:©1€ />
is a valid tag name). But/
should definitely not be part of it, fixed.
Now all works fine, both with and without a space() before the closing /
in the txp:tag. TY.
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Variable tweaks
etc wrote #337042:
This implies though that concerned variable names must be alphanumeric.
Interesting (I am a bad person):
<txp:date format="%m" escape="integer" variable="a月b" />
<txp:evaluate query="a月b >= 4 and $a月b <= 8" alias="a月b">
Some lovely weather wraps around a month of heavy rains
</txp:evaluate>
Works just fine, but name the variable as variable="月"
(omitting the ASCII characters) fails with Warning: DOMXPath::evaluate(): Undefined variable Issue detected while parsing form p_test
.
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Variable tweaks
This might be PHP. Its definition of a variable is one that begins with an ASCII alpha character. You can’t even have a variable called “1var” in PHP. It’s illegal, afaik.
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: Variable tweaks
Bloke wrote #337046:
This might be PHP. Its definition of a variable is one that begins with an ASCII alpha character. You can’t even have a variable called “1var” in PHP. It’s illegal, afaik.
Correct about starting with a digit, that fails. But I wish it was that simple :-(
variable="£"
is all peachy, but €
or ¥
? Don’t go there…, even variable="a€b"
throws errors:
Warning: DOMXPath::evaluate(): Invalid expression Issue detected
That inconsistency annoys me, as it makes it more difficult to define what characters you can use.
Rule of thumb: a-zA-Z0-9
(that includes the hyphen (hypen-minus, U+002D). The underscore might pay as well!
Last edited by phiw13 (2024-04-08 06:10:26)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Variable tweaks
Well, must (in ‘be alphanumeric’) actually means should. The true name restrictions are
- not contain
,
(comma) or|
(pipe) - end with
a-zA-Z0-9_
(to match\b
regex pattern)
The reason is the regex patterns used in replacements. Blindly replacing $doll
with its value in 20 $dollars $doll
would be hazardous, and I don’t feel like writing a full-power XPath syntax parser. But if you say it’s ok (for blind replacing), it’s easily doable.
I vaguely hope to ultimately transform these $varname
into true XPath variables and leave the hand to DOM parser, but it means $varname
must be valid XPath-wise. So we’d better cope with it from the start.
Offline
Re: Variable tweaks
etc wrote #337048:
Well, must (in ‘be alphanumeric’) actually means should. The true name restrictions are
- not contain
,
(comma) or|
(pipe)- end with
a-zA-Z0-9_
(to match\b
regex pattern)
Is it? I see many failures (post above, variable="a€b"
), but also, starting with a digit seem so work (sometimes?).
The reason is the regex patterns used in replacements. Blindly replacing
$doll
with its value in20 $dollars $doll
would be hazardous, and I don’t feel like writing a full-power XPath syntax parser. But if you say it’s ok (for blind replacing), it’s easily doable.
It is not for me to decide what is OK or not, and you certainly have a better view of what is possible, what the limits are and what you think you’ll be doing with all this in the future.
I vaguely hope to ultimately transform these
$varname
into true XPath variables and leave the hand to DOM parser, but it means$varname
must be valid XPath-wise. So we’d better cope with it from the start.
So from the start a good definition or description of allowed characters
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Variable tweaks
phiw13 wrote #337049:
Is it? I see many failures (post above,
variable="a€b"
), but also, starting with a digit seem so work (sometimes?).
What is your code? This works for me:
<txp:variable name="a€b" value="abc" />
<txp:evaluate query="concat($a€b, 'a', $a€b)" alias="a€b" />
It is not for me to decide what is OK or not, and you certainly have a better view of what is possible, what the limits are and what you think you’ll be doing with all this in the future.
Nobody owns txp, we all just expose our reasons and make a decision. But we devs also have to care about (future) bwc issues, for our own mental sanity :-)
So from the start a good definition or description of allowed characters
Yep, alphanumeric means matching \w
pattern (Latin Unicode letters, digits and _
), sorry for not being clear here.
Edit: I have just extended ‘letter’ to unicode, since they are allowed in XPath variable names.
Offline
Re: Variable tweaks
etc wrote #337050:
What is your code? This works for me:
<txp:variable name="a€b" value="abc" />…
Yours works without problems, directly in an article or inserted in a form then loaded in the page template.
The code block I have been playing with is your original sample, changing the name of the variable, using currency symbols and other Unicode characters
<txp:date format="%m" escape="integer" variable="abcd" />
<txp:evaluate query="$abcd >= 4 and $abcd <= 8" alias="abcd">
Some lovely weather wraps around weeks of heavy rains
</txp:evaluate>
Yep, alphanumeric means matching
\w
pattern (LatinUnicode letters, digits and_
), sorry for not being clear here.Edit: I have just extended ‘letter’ to unicode, since they are allowed in XPath variable names.
For some reason this makes reason this makes my test work better I think. Unicode characters do make sense, I don’t expect someone to name the variable with CJK / Hindi / Arabic … characters, but some extended Latin or Roman ”looking” characters – I was just thinking about the Turkish dotless ‘i‘ (ı
U+0131) for an unrelated reason – might easily creep in (Turkic languages, Vietnamese, transliteration of CJK, etc).
For documentation purposes, there is still a need for mentioning that the closing character must be one of “end with a-zA-Z0-9_ (to match \b regex pattern)” as you noted above. (Sorry about insisting about this, but given that Xpatch can be a little picky, it is better to have it documented well from the start.)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline