XF 2.1 Make variable available in template from php file

Xiomera

Member
I would like to display an additional value for every thread in the thread view and the thread list (forum view), which i calculate with an php function.

My problem: what is the best way to make a variable useable in an tempalte from an php file? And How?

I found 2 solutions. The callback tag in template (which calls the php function) or the possiblity to add the variable to an object like $forum (in my case $thread) in php.
What solution is more efficient and advisable in my case?

The callback tag worked for me just fine. Although I am not sure if this is the perfect solution (cuz I have to call it every time I need the varaible - can't assign it to a local variable in the template).
HTML:
<xf:callback class="\XF\CustomFiles\MyFile" method="getFormatedScore" param="$thread"></xf:callback>

For the second solution I tryed to follow this thread, but there is no code event listener for a thread (or something suitable for my problem).
Is there a way to add a variable to the $thread object via php without the listener? I am thinking about a callback which adds the variable to $thread. in this case I only have to call it once and can then use it with the $thread object in the template.
Or is there a better solution?

Ps: I am takling about adding an variable to $thread, that can be accessed like this
HTML:
<strong>{$thread.myCustomScore}</strong>

Stricktly speaking which solution is more suitable for me? In case it is the second one a basic code example would be very needed.

Thank you for your time and help!

-Xiomera
 
Can you not just extend the function that calls the template you want and then pass your variable as one of the template parameters?
 
You can either consider to extend the thread entity via class extension if you need the variable to be available in a number of places, or create two code event listeners for templater_template_pre_render, one with public:forum_view and one with public:thread_view as event hint and pipe your variable into the templates as available parameter.
 
Hello @Lukas W. you might be able to help a bit in more detail. It would be great to get some more tutorials on how to create addons and how to handle data from templates to php and the other way round, but this will not be the question I have.

How can I execute a function and get a list of data back in the POST_MACROS template?
 
Additional question, would it be better to catch the information for all posts on the current thread at once, or to get the information for each post separately?

How would it be possible to save the result in a global var, which can then be accessed by the template?
Maybe @Earl or @au lait could help.
Thx
 
How would it be possible to save the result in a global var, which can then be accessed by the template?
One way to make data globally available is the listener with the associated name "templater_global_data".

Code:
 public static function templater_global_data (\XF\App $app, array &$data, $reply)
    {
        $data['VARIABLE'] = 'GLOBAL';
    }

In the template it is then {$xf.VARIBALE}
There are ways to do this. It is always a question of what kind of data and does it have to be global or is it sufficient in template X?!



Additional question, would it be better to catch the information for all posts on the current thread at once, or to get the information for each post separately?
The focus should be on keeping queries and effort low. Therefore, if all posts are needed anyway, it makes more sense to get them all at once instead of individually. You can then process each individual post accordingly.
 
Thanks for your answer.
Please could you tell me on how to trigger to fill the data into the variable, I guess I need to fill the data during the thread load, but do not know in detail how.

Maybe in future the documentation will be enhanced and some more examples will be added, so that it is easier to write own addons.

Thanks again for your help.
 
I (or others) can chew this out for you😎. It's better and more sustainable if you work it out for yourself.

Get some free addons here, for example, and study them, but please don't use AndyB addons to learn. Recommend XON etc.

A decent development environment is mandatory and then study. Also study XF itself and learn from it and many questions will be answered. XF has most of what you want to know on board.
 
Oooook...
An example... Very simple and not perfect...

screen_1612200457.png

screen_1612200469.png


This as an example, since you mentioned the posts...

In this example you pass the Post Object to your variable...

screen_1612200664.png
 
but please don't use AndyB add-ons to learn
Thanks, why not AndyB are they so bad or wrong code?

check out this post,
He checks the cookie to detect if the user has logged in or not.
But the recommended way is
PHP:
$visitor = \XF::visitor();

if ($visitor->user_id){

    //do things for logged users

}

Right?

and this method used everywhere in the XenForo system but seems like Andy is doing it in a different way.

Then again, in terms of the censoring part, He could do it only when the user is making a post/conversation/posting a VM, etc, but he chose App setup even listener? at least he could choose visitor guest setup, or even visitor setup, right? App setup is where the whole routing system and the whole XenForo engine starts, and that's not the best place to extend it, right?
Is that the reason that you don't recommend his add-ons to learn?
 
Back
Top Bottom