• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Creating a page

bart

Active member
#1
There is a lot to find about creating a page, but I really can't get it going. Maybe because a lot of tips are focused on XF1 and I am working on XF2. Hope someone can help me out...

I want to create a page.
I want it to use a callback.
I want to be able to do some coding in this callback-script
And want to pass data to a template.

Sounds like it can be done. :)

But I get stuck on the callback-script. And how to pass the data... And I am dazzled by Controlers and Abstract things and more of that...

An example-script would be very welcome. Thanks!
 
#2
Create a page:
Login on the admin panel
Go to Forums --> Nodes
After that, at the top, right: click on "+Add node" and select "Page".
Now you can create a page.
 

bart

Active member
#3
Yeah, that part is clear. Sorry.

The problem is when I want to use a callback. I can't get that working. I need to be able to make a page through php and show it in a template.
 

bart

Active member
#4
To show you what I am stuck on. This is my code:

Code:
namespace test\home;

class homepage
{
    public static function doe()
    {
        $visitor = \XF::visitor();
        $viewParams = [
            'who' => $visitor['username'],
            'zone' => $visitor['timezone']
        ];
        $sound="yeah";
        return $viewParams;
    }
}
And the template might be something like:

Code:
{$who} is in {$zone}

well...

Someone said {$sound}
I've got the callback working, but it just gives me:

is in well... Someone said
 

Chris D

XenForo developer
Staff member
#5
The explanation of the callback field when editing/creating the page lists some callback arguments, you'll need them.
PHP:
<?php

namespace test\home;

class homepage
{
    public static function doe(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {

    }
}
Next, the callback itself doesn't actually need to return anything. The $reply object is the same reply that the page would give if you didn't have a callback, e.g. it's just going to render the template, and do nothing else. To add your who, zone and sound variables, you need to modify the $reply object:

PHP:
<?php

namespace test\home;

class homepage
{
    public static function doe(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        $visitor = \XF::visitor();
        $reply->setParam('who', $visitor->username);
        $reply->setParam('zone', $visitor->timezone);
        $reply->setParam('sound', 'yeah');
    }
}
And that should do it.
 

bart

Active member
#6
This works. Great! I have to dig into this constroller-stuff.

Now one small step further: to render the page I would like to use a template from the system. Like "page_view" or any template I define myself.

Thanks!
 

bart

Active member
#7
Just to ask it one more time.

How can I make it so that I can call something like:

return $this->renderer('my_template', $viewParams);

Well, what I really want to do is somthing like:

$html=render("my_template");
$reply->setParam('contents', $html);

Hope this makes sense...