Wednesday, February 22, 2012

[1.0.0 b1] Finding Variables Available To Templates

"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.

No comments:

Post a Comment