XF 2.1 Template modification that works for both XF 2.0 and 2.1?

Mr. Jinx

Well-known member
I would like to make some of my addons compatible with both XF 2.0 and XF 2.1
Therefore I need to do a few template modifications.
The "find" string will be the same, but the "replace" string will be different based on the version.
What would be the best way to do this, without making two separated addons? Is it possible to check the version while doing a replace?
 
To be honest, I'd generally avoid it. The expectation of supporting XF 2.0 pretty much ends when XF 2.1 is released (it isn't like XF 1.5 where there's an extended support period).

If you absolutely had to, at least while you want to support XF 2.0 and the 2.1 betas then you can do it in the template:

HTML:
<xf:if is="$xf.versionId > 2010010">
    // XF 2.1 code
<xf:else />
    // XF 2.0
</xf:if>

However...

Due to the way that templates are compiled, you may still struggle. For example, if you tried to use a brand new XF 2.1 template tag in the 2.1 portion, that will fail to compile in XF 2.0 because the tag is unknown.
 
You can also try to use callback. It will be called once during compilation, unlike <xf:if> that will be calculated during run time.

In template modification use this:
find:
Code:
/.+/siu
replace:
Code:
YourAddon\EventListener::templateCallback
then create YourAddon/EventListener.php with templateCallback function:
Code:
public static function templateCallback($code)
{
  $code = $code[0];
  // do replacements
  if (\XF::$versionId > 2010010)
  {
     // replacements for 2.1
  }
  else
  {
    // replacements for 2.0
  }
  return $code;
}
 
Awesome, thanks for the example!
Exactly what I was looking for. This should do the job without any extra conditionals in the real template.
 
Top Bottom