UNSOLVED Widget code works on one NodeBB instance but not another


  • I'm using this code in a widget in https://hostrisk.com which works fine as standard html in the global header widget

    <!-- IF !loggedIn -->
    <div class="hello">Hello, guest</div>
    <!-- ELSE -->
    <div class="hello">Welcome back, <a href="/me"><span class="username">{user.username}</span></a></div>
    <!-- ENDIF !loggedIn -->
    

    However, the exact same snippet doesn't work on my other site https://sudonix.com. I wondered if any particular plugin is required for this to work ?

    Both instances are 1.18.3

    Thanks

  • Global Moderator Plugin & Theme Dev

    Do you have different themes? Have you tried placing the widget in different locations?


  • @pitaj the themes are identical - both persona. Doesn't seem to matter where the code is placed either in terms of widget areas

  • NodeBB

    a7a95b4a-4940-42a5-beef-baa328ea32a0-image.png

    There is something funky going on with that widget, it is supposed to be inside the header-widget area but it is outside. Check the markup of other widgets maybe there is some issue there.

    I tested on persona with core with a single widget and it worked fine.


  • @baris the widget code has been deleted on Sudonix, but it's there and functional on hostrisk.com.

    On Sudonix, I do have modified templates via the customizer plugin so that could be the cause.


  • @baris said in Widget code works on one NodeBB instance but not another:

    There is something funky going on with that widget, it is supposed to be inside the header-widget area but it is outside

    It's placement is deliberate in the sense that I wanted a "secondary header" for the banner you see on most pages. It's essentially added manually using custom templates and the customizer plugin.


  • @baris I've removed the manual placement and tried again this time with a global widget so it appears at the top of all pages - this still doesn't work.

    I've simplified the banner, so the code now look like this

    <div id="welcome">
    <div class="welcome">
    <!-- IF !loggedIn -->
    Hello, guest. New here ? Read <a href="https://sudonix.com/welcome">this</a> to learn how this platform can help you
    <!-- ELSE -->
    <div class="hello">Welcome back, <a href="/me"><span class="username">{user.username}</span></a></div>
    <!-- ENDIF !loggedIn -->
    </div>
    </div>
    

    Any thoughts ?


  • @baris and interesting development. It seems (for my install at least) that I cannot use placeholders in the "stock" widgets, but can in any custom pages I create. I even disabled the Customizer plugin to see if this makes a difference, and it doesn't.


  • My further testing shows that the other site I have is also affected by the same issue. As soon as you move the widget code out of any custom page widget and into the stock templates such as recent, unread, popular, etc, it does not work.


  • Anyone have any thoughts on this please ?

    Thanks 👍

  • NodeBB

    You should post what changes you are making to the core templates. I tested the widget code and it works fine with a default persona and core. Except the {user.username} part doesn't work because the user object isn't passed to the widgets.

    9b964df3-05e7-429d-8851-71ec8585a12b-image.png

    I put

    <div id="welcome">
    <div class="welcome">
    <!-- IF !loggedIn -->
    Hello, guest. New here ? Read <a href="https://sudonix.com/welcome">this</a> to learn how this platform can help you
    <!-- ELSE -->
    <div class="hello">Welcome back, <a href="/me"><span class="username">{user.username}</span></a></div>
    <!-- ENDIF !loggedIn -->
    </div>
    </div>
    

    In a html widget and placed it in the global header.

    8db290ab-1d22-424f-96e0-d4ed2e66d226-image.png

    If it's not working it's likely because of some modification you make to the templates because the widget wasn't in the correct place in your html.


  • @baris Thanks - sorry - I should clarify here.

    The code does work and the widget appears exactly where I expect in all cases, so the template modifications are not the issue here 🙂 What IS the issue is that {user.username} returns a blank value in all stock templates from core, but works in any custom template I create.

    Disabling the Customizer plugin means all templates return to their defaults, but {user.username} is affected by the same issue.

  • NodeBB

    Yeah {user.username} is blank because it is not being provided by the backend. If you want to be able to use it on any template you need to make it available with the hook 'filter:middleware.render' like this.

    myPlugin.filterMiddlewarRender = async (hookData) => {
        hookData.templateData.user = await user.getUserData(hookData.req.uid);
        return hookData;
    };
    

    This would add user property to all pages.


  • @baris Thanks. Can I execute this using Custom JS ?

    Tried it, and got

    12da9523-6231-4269-a122-c169380682c5-image.png

  • NodeBB

    No this needs to go in a plugin for the server side. The JS tab in the ACP is for client side scripts.


  • @baris So I'd need to create a plugin I guess ?

  • NodeBB

    If you don't have a custom plugin already then yes you need to create one. You can test it out by adding it to persona's library.js and plugin.json.


  • @baris Great. Thanks. Is there any possibility this could be added to persona at all ?

  • NodeBB

    No plans to add it to persona.


  • @baris I'm looking to implement a plugin to do this, but ideally am looking for some sort of reference guide or skeleton plugin to use - any advice ?

    EDIT - found https://docs.nodebb.org/development/plugins/ so reading this first

    Thanks

Suggested Topics

| |