Skip to content
  • 0 Votes
    1 Posts
    583 Views
    dogsD
    This is just a little snippet.

    This is about the question How can I add custom data to a post? a small tutorial

    How to add custom data permanently to a post It will be saved into the object:

    You need the following hook:

    { "hook": "filter:post.create", "method": "postCreate" }

    This hook is fired, when a Post is created. So it contains RAW Data.

    A filter:hook like "hook": "filter:post.create" can be edited before beeing proceeded.

    plugin.postCreate = function(data, callback){ // Edit data here callback(null, data); }

    You can access all Data you can work with via the data object.

    All those filter-hooks needs callbacks. The callback contains the modified / added / removed data.

    Whats inside the data object?

    The data object contains two main objects.

    this.post this.data post: { pid: 259, uid: 2, tid: 111, content: '### This is just a little snippet.', timestamp: 1617150411838 }, data: { uuid: '7277s31-6d23-43sa-1284-ad27e42yx879', title: 'How to add custom data to post serverside - in a nutshell', content: '### This is just a little snippet.', thumb: '', cid: '2', tags: [], uid: 2, req: { [...] }, timestamp: 1617150411838, fromQueue: false, tid: 111, ip: '111.123.2123.21', isMain: true } }

    With all the data you can work with. But only this.post is directly effected (lol) to the post Object.
    If you add data here. It will be attached to the post forever and you can access it any time. Even after plugin disable.

    So now add custom data:

    plugin.postCreate = function(data, callback){ // adds data **PERMANENT** to post // before any other procession -> RAW DATA (markdown) //data.post.myData = "directly effected to post you can work with it in next step"; //data.data.myData = "other data for other things I don't know"; var myData = {}; myData.name = "Schmock"; myData.signature = "raz0rn"; // Stick myData to post object data.post.myData = myData; // Debug? // console.log("POST CREATE", data); // finish the process() - passes on modified data callback(null, data); }

    So myData has added to post:

    post: { pid: 259, uid: 2, tid: 111, content: '### This is just a little snippet.', timestamp: 1617150411838, myData: { name = "Schmock", signature = "raz0rn", } }

    The data is stored. Everybody is happy.

    dance minions

    How to add dynamic data before render a post in template It wont be saved to the object. You can use this for dynamic things or logic before rendering the page.:

    You need the following hook:

    { "hook": "filter:topics.addPostData", "method": "addPostData" }

    This hook is fired before engine renders the template.
    It also needs a callback because its a filter-hook.

    plugin.addPostData = function(data, callback){ // modify data temporarily before it gets passed on to next step callback(null, data); }

    Same thing as above. Only the hook is different. And the data we are changing is temporarily.
    Data contains all data we can use for the dynamicness lol: .

    plugin.addPostData = function(data, callback){ // You can work with pre-parsed and already saved data .... // or put in something very flexible without saving to db like jquery-like dynamic etc etc // Debug? // console.log("addPostData?", data) var _posts = data.posts; _posts.forEach(function(post){ if(post.myData){ // add data to post if "myData" is there post.content = "THIS POST HAS MY OWN DATA -> CONTENT IS OVERWRITTEN"; } // this here affects all posts post.user.signature = "Ihr seid alle Schmocks!"; }); // Overwrite data and pass modified data on to the template engine data.posts = _posts; callback(null, data); }

    Now you can work with posts like a boss.

    Bildschirmfoto 2021-03-31 um 03.10.32.png

    dab

    Thanks and bye

    Important Note:

    Remind, that myData is available via API.

    https://nodebb.development.fail/api/topic/114/new-post

    returns your added data

    "content": "THIS POST AS MY OWN DATA -> CONTENT IS OVERWRITTEN", "myData": { "name": "Schmock", "signature": "raz0rn" }
  • 0 Votes
    5 Posts
    895 Views
    dogsD

    @baris Thank you very much. This worked! 🙂

    €: I additionally use the hook

    $(window).on('action:ajaxify.start', function (ev, data) { $('div.topic-header-image').hide(); });

    so that the header container with the image is hiding immediatly and before ajaxify.end 👍

  • 0 Votes
    3 Posts
    666 Views
    magnusvhendinM

    Thanks @baris, I will have a look.

  • 0 Votes
    5 Posts
    573 Views
    A

    Thanks ! I had tried this before but something else was wrong.

    Now, everything is ok !!! Thanks again !

  • 0 Votes
    8 Posts
    2k Views
    PitaJP

    @loopynid here's a better resource: BenchpressJS docs

  • 0 Votes
    6 Posts
    3k Views
    K

    @Bri @pichalite
    I got it now 👍 , thank you guys 🙂

  • 1 Votes
    3 Posts
    2k Views
    P
    templates.render ('tpl/name', {}, function() { // templates.cache['tpl/name'] });

    I think so anyways, I'm writing this from my phone

  • 0 Votes
    1 Posts
    1k Views
    drewD

    I use a slightly modified version of lavender for my site, and up until now that's been quite a headache.

    I've had to maintain an entire theme which is just a fork of lavender and constantly merge in changes just so I can have one template file be slightly different.

    I was wondering if this is now something I can resolve with the baseTheme parameter?

    If MyTheme has a baseTheme of nodebb-theme-lavender, which in turn has a baseTheme of nodebb-theme-vanilla does that mean that I can change my repo to just be the one altered file?

    I remember in the past I tried inheritance and found it didn't work.

  • 0 Votes
    2 Posts
    2k Views
    N

    Good news, that we don't need to add filter, there is filter for user profile: filter:user.account
    But what is about static template?

  • 2 Votes
    10 Posts
    8k Views
    Yoann Aubry 0Y

    Does it still work? My helper functions don't seem to be recognize in my template files.

  • Visual plugins

    NodeBB Plugins
    4
    1 Votes
    4 Posts
    2k Views
    N

    @julian thanks 😉

  • 0 Votes
    3 Posts
    2k Views
    barisB

    Category data is not avaiable in the header, if you want to use it you will have to use the hook filter:middleware.renderHeader and add the data.

  • 0 Votes
    6 Posts
    3k Views
    barisB

    That should be fixed as well now, thanks.

  • 0 Votes
    10 Posts
    4k Views
    barisB

    It is loggedIn everywhere now. https://github.com/NodeBB/NodeBB/issues/1900

  • 0 Votes
    16 Posts
    7k Views
    P

    @a_5mith said:

    @psychobunny

    .username { font-size: 80%; }

    Well that works for me ... updated 😄