Conditional elements in theme based on tags



  • Hey all,
    I'm helping someone set up a new community for a not for profit and they'll need some custom functionality to hit their goals.

    Can anyone point me in the right direction on how to use a plugin etc to add in html/css if a post or reply has a specific tag.

    Short version is: People will be discussing different things and the org wants to allow certain conversations but to auto add in a disclaimer/change the post format a little for specific post tags.

    IE: If the post has the tag: "marvel" on it, it'd show a div/text at the bottom of the post/reply saying "This person loves marvel".

    New to NodeBB, not entirely new to everything else. 🙂

    1. Is there a template conditional I can use for this like "<!-- IF" that reads tag values?
    2. Or is there a way to use conditionals for a widget? Looks like a widget might be the perfect way to display it.

    We're using the hosted NodeBB, does that change anything?


  • Admin Staff

    NodeBB only allows tagging topics not posts so what you want isn't really doable out of the box.

    With that said it is possible to write a plugin/widget that can look in the content of posts and detect certain keywords and add additional content at the bottom of the post. For example you can add "this person loves <keyword>" at the end of a post if the post content contains <keyword>.


  • Community Rep

    @baris

    On a related note: Is there any reason why NodeBB could/does not facilitate per post tagging? Or maybe sport an option to flip 'twixt the two?

    One the one hand I often find such of potential use/value, as long running threads often drift a bit, while on the other I can see how it could lead to a proliferation of tags that become unmanageable and hence of minimal value for all but search engine indexing, i.e. the value of tags to live bodies becomes a bit thin once there's a few thousand of them. Yeah, some folks DO love to game features such as this. Hence after pondering a bit, I came down on the "not" side of this, as I have some "creative" users.

    Anways, something I have been curious about for a while now and this thread finally prompted the query.



  • @baris Thanks for that.
    Topics is what I meant by posts. A lot of the other platforms I've helped people build on use comments or replies for the second layer.

    Plugin sounds like the path to go for.
    Can you point me in the direction of any docs/notes on reading the tags of a topic and for the contents of a post?


  • Admin Staff

    You can find some info about the topic/post structures here https://github.com/NodeBB/NodeBB/wiki/Database-Structure.

    For plugin development info you can take a look at https://docs.nodebb.org/development/plugins/

    You can look into the hook filter:topic.build which already has the tags of the topic in the tags field. Then you can add the additional data in that hook for the template to use. Sample hook:

    myPlugin.filterTopicBuild = async function (hookData) {
       if (hookData.templateData.tags.includes('marvel') {
          hookData.templateData.posts[0].content += '<br/>this user likes marvel';
       }
    }
    

    This is just a simple example feel free to modiyy & improve.


Log in to reply
 

Suggested Topics

  • 5
  • 3
  • 1
  • 5
  • 1
| |