Working on a new theme but I need your Help Please
-
Hello,
I start working on a theme that should display each topic content below topic title and details in "Recet Topics" and "Category" pages. The theme make posts displayed like on facebook and twitter walls.
My problem is that I cannot intill now understand how node.js server side passes datas variables to template and how it works on nodebb. Is some one here can give me some tutorials or something that can help me..
For example I added this to category.tpl to display topic content post but nothing is displayed, it seems that the variable {topics.posts.content} is null :
<div class="content">
{topics.posts.content}
</div>but if I write :
<div class="content">
{topics.title}
</div>I can see the title.
How to passe topic post content to the category view in NodeBB ?
Thanks
-
@kacemlight post content is not available on the category page api
You can see what's available in the category page api for one of the categories in this community using this link
https://community.nodebb.org/api/category/2/general-discussion
-
Yes I know. Is there a way to add post content to the category page API ?
Thanks, -
@kacemlight You will have to modify core for that
-
You can modify the api data using the hook in the page's controller method.
In your case,
filter:category.build
https://github.com/NodeBB/NodeBB/blob/97ce08f5af2b0a324b10682246fc19aaa85d98da/src/controllers/categories.js#L273But it's not a simple task afaik. You'll need to pull the post data from the database yourself, filter it based on the users' privileges, and arrange it in the way you want. Basically everything that happens in the topic controller, but substituting the users' settings for your own.
-
I don't know yet how it really works, how server passe API data via template to client-side. Has someone here a tutorial or something that can help me ?
Thanks,
-
You should look at how plugins do it, because you have to do the same.
In your plugin.json, you need to add a
library
file and ahooks
array. Create the library file and in it define the hook function. The hooks that you want likely end in.build
, but not all pages have a build hook. The function gets passed an object with atemplateData
member object. You modify templateData and pass back the original object. Whatever you add totemplateData
is exposed in the template. e.g...// plugin.json { "id": "nodebb-theme-persona", "scripts": [ "lib/persona.js", "lib/modules/nprogress.js", "lib/modules/autohidingnavbar.min.js", "lib/modules/slideout.min.js" ], "library": "library.js", "hooks": [ { "hook": "filter:category.build", "method": "filter_category_build" } ] }
// library.js module.exports = { filter_category_build: function (data, callback) { console.dir(data.templateData); // See what's inside. data.templateData.banana = "Best Fruit"; // Add some data. callback(null, data); // Send it back. } };
// category.tpl {banana} // Prints "Best Fruit"
If you want to pass an array (such as for attaching posts data), you also have to make sure you use <!-- BEGIN -->, e.g.
data.templateData.fruits = [ { color: "red", fruit: "apple"}, { color: "yellow", fruit: "banana"} ]
<!-- BEGIN fruits --> <p> {fruits.color} {fruits.fruit} </p> <!-- END fruits --> // Prints: // red apple // yellow banana