Recent searches
No recent searches
Showing the user name in a template
Answered
Posted Sep 22, 2016
Hi! According to this doc there's a user.name object that supposedly shows the name of the logged in user, but when I add {{user.name}} to the home page template I get "Unable to preview 'Home page' due to invalid reference: {{user.name}}"
So how do I print the user name in a template? I'm not having any luck with this site's search or google :(
Thanks!
0
5
5 comments
Tal Admon
Hi Diego,
You can do this with a bit of javascript: see example below.
The script: note that I set an empty span with the id "user_name". Then, I populate it according to the value of HelpCenter object.
I also set "stranger!" when this value is null (in case of anonymous user), and an exclamation mark after the name.
Let me know if you have any questions or if this does not work out for you.
<div class="knowledge-base">
<h1>{{help_center.name}}</h1><h3>Hello, <span id="user_name"></span></h3>
<script>
jQuery(document).ready(function(){var uname; if (HelpCenter.user.name===null){uname = "stranger!"} else {uname = HelpCenter.user.name+"!"};jQuery('#user_name').html(uname);});
</script>
--Tal
0
Diego Rod
Thanks Taf, but why if I try to use {{HelpCenter.user.name}} directly to print the username in the homepage template I get:
Unable to preview 'Home page' due to invalid reference: {{HelpCenter.user.name}}
Or can I use this object only in javascript? Also where can I read more about these rules? I couldn't find anywhere that I have to put "HelpCenter" before user.name for it to work.
Thanks!
0
Tal Admon
Hi Diego,
Not all objects are available in all template pages.
In the case of the Home page, the User object is not available.
Compare, for example the list of available properties of the Home page to the User Profile page:
https://developer.zendesk.com/apps/docs/help-center-templates/user_profile_page
You will see the user object in the last row of the Available Properties table.
So, for the homepage (or any other page) you will need to use the HelpCenter javascipt object which although I can't find about it in the official documentation it is referred to by many Zendesk users in the community.
Cheers,
Tal
0
Diego Rod
Ah thanks, it makes more sense now!
I was just grabbing the username from the user menu in the top left, but yeah the HelpCenter object seems like a more elegant solution.
Thanks again!
0
Jessie Schutz
Good to see you back, Tal!
@diego, let us know if you need anything else! :)
0