Check for usernames (Template Level)
-
Hi there, I'm wondering if there is a way to check for usernames on the template level? We are currently using javascript to find and replace portion of each username to display certain image. This works, but there are times that it looks tacky when there is a delay on page load. When there is a delay, you would see the text before it gets replaced by js.
I hope this make sense.
-
This is how it works currently. We are matching specific string in usernames (in this case we are matching '_PSN') and replacing that with the PSN icon. This works fine, but as I mentioned in my OP, it looks tacky when there is a delay. It will flash the username with _PSN first then replace it with the image.
-
In that case you could use a server side hook
filter:topic.getPosts
this hook receives the posts being loaded in theposts
variable. In that hook you can check the username and add a new property to the post.user object likeplatformIcons
. Here is some sample code for the server side hookmyPlugin.filterTopicGetPosts = (hookData) => { hookData.posts.forEach((post) => { if (post && post.user) { post.user.platformIcons = []; if (post.user.username.endsWith('_PSN')) { post.user.truncatedUsername = post.user.username.replace(/_PSN$/, ''); post.user.platformIcons.push({ url: '', image: ''}); } // check other platforms like above } }); return hookData; };
Once you have the platformIcons populated you can just use it in your custom theme in the topic/post.tpl. You can use the truncatedUsername as well if you don't want to see _PSN in the username.
-