Using an Alternative Page Container

Kier

XenForo developer
Staff member
Add-on developers will likely have noticed that (almost) all XenForo output for the public-facing side of the application is contained within a single template, called PAGE_CONTAINER.

Individual page templates only allow you to build the HTML within the content area, though there are various XenForo template syntax systems that allow you to pass commands and data to the container, the most obvious of which is the ubiquitous <xen:title>Page Title Here</xen:title> here construction.

However, as time goes on and add-on applications grow more complex, you are likely to find that you want to output your data into your own custom container. The good news is, it's easy to do so. All you need to do it pass a containerTemplate variable to your view layer.

Here's an example of how to do that:
PHP:
class MyAddOn_ControllerPublic_Something extends XenForo_ControllerPublic_Abstract
{
	public function actionIndex()
	{
		// do stuff here to build $viewParams array and then...

		return $this->responseView(
			'MyAddOn_ViewPublic_Something_Index', // view name
			'some_template', // content template name
			$viewParams, //  data to be passed to content template
			array('containerTemplate' =>
				'MyAddOn_Page_Container_Template') // your own container template
		);
	}
}
The only thing to note here is how we pass containerTemplate as a value of the array that is the fourth argument to responseView().

Of course, if you decide to use your own container template, you need to handle everything yourself. Breadcrumbs, headers and footers, sidebars... all that stuff is handled by the container template and if you don't implement something in your own container template, it won't be there when content templates call for it. Study the default PAGE_CONTAINER template for everything you need to know.
 
*lonely bump* :p

This is great to hear, and helps me customize my style in a unique enough way so I do not have to worry about upgrades just because I have a few unique sections on the sites. And I can leave page_container basically alone.
 
^ That post is getting very lonely all by itself, so here is a reply.
Spammer:p


thx for the post^^
I've done this some time ago very ugly because i hadn't seen
PHP:
$templateName = (!empty($params['containerTemplate']) ? $params['containerTemplate'] : 'PAGE_CONTAINER');

(created own dependencies where i overwrote the getViewRenderer...:D )
 
@Kier: I'm a bit lost as for how to choose among the offered listeners. What would be a typical code event listener that would trigger your class in an add-on?
 
We're not talking about the normal xf pages (node type)

It's about the output from controllers.
 
....
Here's an example of how to do that:
PHP:
class MyAddOn_ControllerPublic_Something extends XenForo_ControllerPublic_Abstract
{
    public function actionIndex()
    {
        // do stuff here to build $viewParams array and then...

        return $this->responseView(
            'MyAddOn_ViewPublic_Something_Index', // view name
            'some_template', // content template name
            $viewParams, //  data to be passed to content template
            array('containerTemplate' =>
                'MyAddOn_Page_Container_Template') // your own container template
        );
    }
}
The only thing to note here is how we pass containerTemplate as a value of the array that is the fourth argument to responseView().

Of course, if you decide to use your own container template, you need to handle everything yourself. Breadcrumbs, headers and footers, sidebars... all that stuff is handled by the container template and if you don't implement something in your own container template, it won't be there when content templates call for it. Study the default PAGE_CONTAINER template for everything you need to know.
I'm interested in doing this.
In your post, is 'MyAddOn_Page_Container_Template' a PHP or HTML file? I'm thinking PHP but would like confirmation. Thanks....
 
I'm interested in doing this.
In your post, is 'MyAddOn_Page_Container_Template' a PHP or HTML file? I'm thinking PHP but would like confirmation. Thanks....
Or I guess it could be another template with the name: "MyAddOn_Page_Container_Template".
I was thinking it was a physical file named "Template" in the MyAddOn\Page\Container directory.
 
public function actionIndex()
{
// do stuff here to build $viewParams array and then...

return $this->responseView(
'MyAddOn_ViewPublic_Something_Index', // view name
'some_template', // content template name
$viewParams, // data to be passed to content template
array('containerTemplate' =>
'MyAddOn_Page_Container_Template') // your own container template
);
}
}[/php]The only thing to note here is how we pass containerTemplate as a value of the array that is the fourth argument to responseView().

I tried this today, with limited success. I created a new template for my custom container and to prove it was working I copied the contents of the template "PAGE_CONTAINER" to my new template. The resulting page was close to the original, but it was missing a few things. The background gif was missing as well as the little icon for the background picker.

Anyone have any idea why that would be? I would really like to get it working properly, but can't figure out the background issue. :(
 
Back
Top Bottom