User object not available in partial



  • I'm really new to NodeBB and am struggling a bit to understand the templating system. I'm currently making a theme with is based on persona. I want to make a custom profile section on the left side of the main content. Kind of like adding a widget but always there. I need access to the user object to get the image etc but I cannot seem to access it. In menu.tplit is simply used as such (as an example):

    <label for="user-control-list-check" class="dropdown-toggle" data-toggle="dropdown" id="user_dropdown" title="[[global:header.profile]]" role="button">
      <!-- IF user.picture -->
      <img component="header/userpicture" src="{user.picture}" alt="{user.username}"/>
      <!-- ELSE -->
      <span component="header/usericon" class="user-icon" style="background-color: {user.icon:bgColor}; display: block;">{user.icon:text}</span>
      <!-- ENDIF user.picture -->
      <span id="user-header-name" class="visible-xs-inline">{user.username}</span>
    </label>
    

    I'm trying to use it in a partial called global-user-sidebar.tpl, like so:

    <div class="col-md-2 hidden-sm hidden-xs">
      <!-- IF user.picture -->
      <img component="global-user-sidebar/userpicture" src="{user.picture}" alt="{user.username}"/>
      <!-- ELSE -->
      <span component="global-user-sidebar/usericon" class="user-icon" style="background-color: {user.icon:bgColor}; display: block;">{user.icon:text}</span>
      <!-- ENDIF user.picture -->
    </div>
    

    I import the partial like this (in categories.tpl) <!-- IMPORT partials/custom/global-user-sidebar.tpl -->

    user is undefined so nothing gets printed. I can however access everything passed from /api/categories.

    What am I missing? How come I can't access the user object? How can the menu template do so? Any help is appreciated.

    And yes, I have searched the forum and read up on the documentation, but I believe I'm just doing something wrong.



  • Take a look at https://github.com/NodeBB/nodebb-theme-persona/blob/master/library.js#L103

    The user object is not available on all routes but you can add it via your theme. Then you can use it in your templates.



  • Thanks @baris, I will have a look.



Suggested Topics

| |