• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[1.0.0 b1] Finding Variables Available To Templates

Jake Bunce

Well-known member
"Global" Variables

Two variables that are available in all templates are:

$visitor - This is like $bbuserinfo in vB. It is an array that contains values for the current logged in user. It pulls from the fields of the xf_user table. For example, you can use these references in the templates and in template conditionals:

{$visitor.user_id}
{$visitor.username}
{$visitor.user_group_id}

$xenOptions - This is like $vboptions in vB. It contains settings from the xf_option table in the database. The indexes are the option_id values from each record in that table. For example, here are some references that work in the templates and in template conditionals:

{$xenOptions.attachmentExtensions}
{$xenOptions.boardActive}
{$xenOptions.contactEmailAddress}

How To Find Other Variables

In xF the variables that are available to each template are explicitly defined in the code. All front end templates are called from this directory:

library/XenForo/ControllerPublic

There is a section below to help you find the relevant ControllerPublic file. For now we are just looking at a specific example. For example, the thread view pages use the thread_view template which is called from this file:

library/XenForo/ControllerPublic/Thread.php

Code:
		$viewParams = array(
			'thread' => $thread,
			'forum' => $forum,
			'nodeBreadCrumbs' => $ftpHelper->getNodeBreadCrumbs($forum),

			'canReply' => $threadModel->canReplyToThread($thread, $forum),
			'canQuickReply' => $threadModel->canQuickReply($thread, $forum),
			'canEditThread' => $threadModel->canEditThread($thread, $forum),
			'canDeleteThread' => $threadModel->canDeleteThread($thread, $forum, 'soft'),
			'canMoveThread' => $threadModel->canMoveThread($thread, $forum),
			'canWatchThread' => $threadModel->canWatchThread($thread, $forum),

			'deletedPosts' => $deletedPosts,
			'moderatedPots' => $moderatedPosts,

			'inlineModOptions' => $inlineModOptions,

			'posts' => $posts,
			'page' => $page,
			'postsPerPage' => $postsPerPage,
			'totalPosts' => $thread['reply_count'] + 1,
			'postsRemaining' => max(0, $thread['reply_count'] + 1 - ($page * $postsPerPage)),

			'firstPost' => reset($posts),
			'lastPost' => end($posts),
			'unreadLink' => $unreadLink,

			'poll' => $poll,

			'attachmentParams' => $attachmentParams,
			'attachmentConstraints' => $this->getModelFromCache('XenForo_Model_Attachment')->getAttachmentConstraints(),
			'canViewAttachments' => $threadModel->canViewAttachmentsInThread($thread, $forum)
		);

		return $this->responseView('XenForo_ViewPublic_Thread_View', 'thread_view', $viewParams);
	}

Here you can see the $viewParams defined followed by the template call.

As an example, you can see thread and forum defined for use in this template, so you can use references like this in the thread_view template to access the current thread and forum records for the page:

{$thread.thread_id}
{$thread.title}
{$thread.view_count}
{$forum.node_id}
{$forum.title}
{$forum.message_count}

As you might expect, these records pull from the xf_thread and xf_forum tables in the database.

There are many other $viewParams defined here. If you don't know what all of the variables contain then you can debug them.

Debugging The $viewParams

To see what a particular variable contains you can use a template helper called dump.

Building on the previous example, we can debug $thread by adding this code to the thread_view template:

Code:
{xen:helper dump, $thread}

Once the template is saved you can reload the front end page to see all of the values contained within $thread. It will look like this:

Code:
array(30) {
  ["thread_id"] => int(10906)
  ["node_id"] => int(2)
  ["title"] => string(11) "asdfasdfads"
  ["reply_count"] => int(0)
  ["view_count"] => int(32)
  ["user_id"] => int(1)
  ["username"] => string(5) "admin"
  ["post_date"] => int(1286739559)
  ["sticky"] => int(0)
  ["discussion_state"] => string(7) "visible"
  ["discussion_open"] => int(1)
  ["discussion_type"] => string(0) ""
  ["first_post_id"] => int(133983)
  ["first_post_likes"] => int(0)
  ["last_post_date"] => int(1286739559)
  ["last_post_id"] => int(133983)
  ["last_post_user_id"] => int(1)
  ["last_post_username"] => string(5) "admin"
  ["thread_read_date"] => int(1286739559)
  ["thread_is_watched"] => int(0)
  ["lastPostInfo"] => array(4) {
    ["post_date"] => int(1286739559)
    ["post_id"] => int(133983)
    ["user_id"] => int(1)
    ["username"] => string(5) "admin"
  }
  ["canInlineMod"] => bool(true)
  ["canEditThread"] => bool(true)
  ["isNew"] => bool(false)
  ["hasPreview"] => bool(true)
  ["isRedirect"] => bool(false)
  ["isDeleted"] => bool(false)
  ["isModerated"] => bool(false)
  ["titleCensored"] => bool(true)
  ["lastPageNumbers"] => bool(false)
}

As another example, we can debug the $visitor array by adding this code to any template (since $visitor is available to every template):

Code:
{xen:helper dump, $visitor}

Once the template is saved you can reload the front end page to see all of the values contained within $visitor.

Using Debug Mode To Find The Relevant ControllerPublic File

Enable debug mode on your forum by adding this line to your library/config.php file:

Code:
$config['debug'] = 1;

Now load the forum page that uses the template in question and click the debug link on the bottom:

Timing: 0.2365 seconds Memory: 9.117 MB DB Queries: 11

Scroll to the bottom of the debug output where included files are listed. Look for the library/XenForo/ControllerPublic file.

Related Links
Here is a guide for identifying the root template of a page if you don't know the name of the template you are looking for.
 
I added the following section above:

Debugging The $viewParams

To see what a particular variable contains you can use a template helper called dump.

Building on the previous example, we can debug $thread by adding this code to the thread_view template:

Code:
{xen:helper dump, $thread}

Once the template is saved you can reload the front end page to see all of the values contained within $thread. It will look like this:

Code:
array(30) {
  ["thread_id"] => int(10906)
  ["node_id"] => int(2)
  ["title"] => string(11) "asdfasdfads"
  ["reply_count"] => int(0)
  ["view_count"] => int(32)
  ["user_id"] => int(1)
  ["username"] => string(5) "admin"
  ["post_date"] => int(1286739559)
  ["sticky"] => int(0)
  ["discussion_state"] => string(7) "visible"
  ["discussion_open"] => int(1)
  ["discussion_type"] => string(0) ""
  ["first_post_id"] => int(133983)
  ["first_post_likes"] => int(0)
  ["last_post_date"] => int(1286739559)
  ["last_post_id"] => int(133983)
  ["last_post_user_id"] => int(1)
  ["last_post_username"] => string(5) "admin"
  ["thread_read_date"] => int(1286739559)
  ["thread_is_watched"] => int(0)
  ["lastPostInfo"] => array(4) {
    ["post_date"] => int(1286739559)
    ["post_id"] => int(133983)
    ["user_id"] => int(1)
    ["username"] => string(5) "admin"
  }
  ["canInlineMod"] => bool(true)
  ["canEditThread"] => bool(true)
  ["isNew"] => bool(false)
  ["hasPreview"] => bool(true)
  ["isRedirect"] => bool(false)
  ["isDeleted"] => bool(false)
  ["isModerated"] => bool(false)
  ["titleCensored"] => bool(true)
  ["lastPageNumbers"] => bool(false)
}

As another example, we can debug the $visitor array by adding this code to any template (since $visitor is available to every template):

Code:
{xen:helper dump, $visitor}

Once the template is saved you can reload the front end page to see all of the values contained within $visitor.
 
This thread is amazingly helpful! Thank you Jake for taking the time to share. I am not clear on everything, but I have a much better understanding now.

A few follow up questions I have:

- Using the $thread example, I can see every field in the table is included. Does that happen automatically for each table? Or are the fields defined somewhere else? For example, I installed the XenMoods add-on which added a mood_id field to the xf_user table. Would that field now be accessible via $user?

- If I wish to access the $thread parameters from a custom template, how would you get access? Simply xen:include thread_view template? or do you need to build your own controller?

- This is a quickly created scenario. Let's say I have a project involving polls. I take a look and find the xf_poll table, and see that I want the question field. I add {$poll.question} to a template and the data does not appear. I would presume the parameter is available, but I do not have access to it. How can I determine where I need to look to access this parameter's values?
 
Thanks ragtek. I read the link, but I don't see how to create the DEFAULT template. I tried creating a template in the ACP, named it DEFAULT, but that didn't help.
 
Thanks ragtek. I read the link, but I don't see how to create the DEFAULT template. I tried creating a template in the ACP, named it DEFAULT, but that didn't help.
You need to render it via PHP using the code provided:
PHP:
return $this->responseView('Example_ViewPublic_Thingy', 'DEFAULT', $viewParams);
Example_ViewPublic_Thingy -> library/Example/ViewPublic/Thingy.php (create it yourself), then render the template as you would normally.
 
ah, ok. Time for me to grab my first PHP lesson online. It will be a bit before I can put this to good use. Thanks for explaining James.
 
When including another template via a template hook ($contents .= $template->create('mytemplate')), the global variables $visitor and $xenOptions return NULL when I dump them. Any ideas?

I can, however, use the variable ($user) as it's exposed to the hook via the hookParams.
 
When including another template via a template hook ($contents .= $template->create('mytemplate')), the global variables $visitor and $xenOptions return NULL when I dump them. Any ideas?

I can, however, use the variable ($user) as it's exposed to the hook via the hookParams.

You can always manually pass them as params:

Code:
$contents .= $template->create('mytemplate', array(
	'xenOptions' => XenForo_Application::get('options')->getOptions(),
	'visitor' => XenForo_Visitor::getInstance()->toArray()
))->render();

That should work.
 
Top Bottom