XF 2.2 parent details in template modification with PHP Callback?

Dannymh

Active member
Hi,

So I have a template modification on the thread_view, this modification uses the PHP Callback method.

In my original code, when It was a straight replacement, I was able to use certain variables and arrays from the parent thread.

Something like

Code:
$0
<xf:if is="{$thread.thread_id}>58086">
    <xf:if is="{$thread.prefix_id}=={$xf.options.stgame_prefix}">
        <xf:include template="st_gamethread"></xf:include>
    </xf:if>
</xf:if>

However now that I am using a callback as I am evaluating a lot more things, I wanted to still be able to check against the thread prefix
With something like

Code:
foreach($keyValues as $key => $value)
        {
            if($thread.thread_id>$options.stthreadid)
            {
                if($thread.prefix_id==$value)
                {
                    return $matches[0]."<xf:include template=".$key."></xf:include>";
                }
                else
                {
                    return $matches[0];
                }
            }
            else
            {
                return $matches[0];
            }
        }

However the callback doesn't seem to have access to $thread and as such I can't evaluate against that. I assume this is because the callback doesn't have any association with the thread its coming from.

In the short term I can solve for it by adding a long variable against each of the option array values and place them in the template mod, so the mod will have the same block of code stamped into the template but should evaluate false where it isnt needed, I just feel like perhaps its better to have the PHP callback handle the evaluation rather than the template code

Can anyone help me get thread in so I can evaluate against it?
 

Dannymh

Active member
I have in the short term gone for pushing out a more evaluations to the template and letting the template do the work, however I dont want this long term I have also noticed a new issue that is and odd one odd issue is the template modification needs to be disabled then re-enabled for it to work with a new template and prefix pair
 

Mike

XenForo developer
Staff member
I think you may be misinterpreting what is meant by a PHP callback and how it differs from the standard regex system. Ultimately, they aren't significantly different. Template modifications are always compile-time changes -- you are just changing the value of the template that's seen by the compiler.

Using a PHP callback simply allows you to use additional code to do further processing or to make a change that isn't necessarily regex based (if you need a stronger parsing language for example). For example, it could allow you to execute matches across multiple steps or to batch up multiple related modifications together.
 

Dannymh

Active member
I think you may be misinterpreting what is meant by a PHP callback and how it differs from the standard regex system. Ultimately, they aren't significantly different. Template modifications are always compile-time changes -- you are just changing the value of the template that's seen by the compiler.

Using a PHP callback simply allows you to use additional code to do further processing or to make a change that isn't necessarily regex based (if you need a stronger parsing language for example). For example, it could allow you to execute matches across multiple steps or to batch up multiple related modifications together.

That's exactly what I need to do. I need to evaluate and manipulate arrays built from some options and then compile a template based on that
 

Mike

XenForo developer
Staff member
The point I was roughly making that the PHP callback approach is really just an alternative method for doing string manipulation on the raw template code (like a regex). Your example PHP code that tries to access things like $thread are simply not possible as this system is executed when the template itself is compiled. This is done when the template changes or when XF is upgraded (for example), not when the thread is viewed. When the thread is viewed, the compiled version is what is executed.

All changes that you make should be based on the textual content of the template, outputting all the logic you might need in the XF template language. (In theory, you might be able to access things like options in the callback, but that is somewhat dangerous and you'd be responsible for recompiling the template when necessary. It's better to defer that behavior to run-time checks.)
 
Top