XF 2.2 Spaces matter

Paul B

XenForo moderator
Staff member
Some time ago I was stuck on an issue and couldn't resolve it.

It was this:
Code:
{$banner.{{ ($xf.time % 5) + 1 }} }

No matter what I tried I couldn't get that to save so ended up going with a different approach, which was:
Code:
<xf:set var="$int" value="{{ ($xf.time % 5) + 1 }}" />
{$banner.{$int}}

Since then it has bugged me why I couldn't get that to work in a single line of code in XF2, when it was possible in XF1.

Fast forward several months and today I was trying out some other stuff and to my surprise this worked:
Code:
{$banner.{{ rand(1,5) }}}

I didn't understand why at first and then noticed a very subtle difference between the two lines of code.
The original one has a space in the end group of brackets - }} } versus }}}.

So the original line of code does of course work if the space is removed like so: {$banner.{{ ($xf.time % 5) + 1 }}}.

A bit late but at least now I can finally sleep at night.

🤦‍♂️
 
That exact same thing got me just a few days ago. A template modification wouldn't save and I couldn't see the error, ended up there was a space similar to yours.
 
In hindsight, I don't know why I didn't try without the space - probably because it just didn't look/feel right.

Pointless fact - there isn't a single instance of 3 braces grouped together like that in the core templates.
 
This is mostly because single brackets as a whole don't allow whitespaces. Even {$myVar } is a syntax error already. If you wrap everything that is not a simple variable expression into double quotes, you can use whitespace as you want, so {{ $banner.{{ ($xf.time % 5) + 1 }} }} works too. It surprises me a bit that double-bracket expressions are allowed in single-bracket expressions though.
 
It shouldn't be just white spaces. This is 2020. All spaces matter. So we need separate campaigns for Black Spaces Matter, Indigenous Spaces Matter, Asian Spaces Matter, etc., as well as White Spaces Matter.

You're opening up a whole new can of worms here. Just sayin...
 
{{ $banner.{{ ($xf.time % 5) + 1 }} }} works too.
Yes, I should have mentioned that after I realised the issue, I also tested that.
This also works (unsurprisingly): {{ $banner.{{ ($xf.time % 5) + 1 }}}}

Not sure why I didn't try that at the time either ...

Oh well, never too old to discover new stuff.
 
Top Bottom