Accessing data inside header.tpl?
-
I am curious to know if I need to add a filter to access categories for example inside my header.tpl?
I want to build a small navigation of categories, but I cannot access that and for future reference I am wondering how to analyze certain data and access it I am trying to read as much as I can on the docs, but I feel like I am missing something.
I see I can do
/api/categories
and get a json response, thats cool but how should I use that in my application? Idealy I want to render the category navigation server side before the HTTP request. -
The controller for the
/recent
page is here: https://github.com/NodeBB/NodeBB/blob/master/src/controllers/recent.js#L28Note the highlighted line. This hook is executed before the page is rendered, so you can have a plugin subscribe a hook here and add category data.
-
@julian said:
k is executed before the page is rendered, so you can have a plugin subscribe a hook here and add cate
Cool so basically inside this make an ajax call to /api/categories? Sounds good and will be useful.
I meant to put header.tpl, I changed it at the last second when I realized my sub header is inside the header.tpl... I asked this before and I cant find my question I think it was deleted somehow by accident, I think you answered it but now I cant go back to reference it.
So I am hoping you can explain how the hook for header.tpl works, also how should I be pulling in the json within the hook? Via ajax or some alternate method, I would prefer to avoid ajax as I would like the content SEO friendly.
-
@Michael-Joseph-Aubry Well, you'd access the NodeBB api directly and add the data to the header object.
-
Since I am displaying recent first,
/api
shows an object with topics not categories. -
@Michael-Joseph-Aubry No, I mean adding the data on the server side with Node.
require
the categories and listen to this hook, adding the data. -
@pitaj said:
quire the categories and listen to this hook, adding the d
Ahh that makes more sense, so inside my theme.js I can have a method that uses node to pull in the categories and expose it. Ok so I guess I have to dig up some examples, do you have any examples on github that is similar?
-
GitHub - NodeBB/nodebb-plugin-custom-homepage: Allows you to define a custom homepage for NodeBB
Allows you to define a custom homepage for NodeBB. Contribute to NodeBB/nodebb-plugin-custom-homepage development by creating an account on GitHub.
GitHub (github.com)
That's a plugin that uses hooks. It doesn't use this specific hook as far as I know, but the hooks all work the same.
-
@Michael-Joseph-Aubry check out nodebb-plugin-quickstart (search it on Google)
-
@pitaj Ok well I keep seeing this
nodebb-plugin-quickstart/library.js at master · NodeBB/nodebb-plugin-quickstart
A starter kit for quickly creating NodeBB plugins. - nodebb-plugin-quickstart/library.js at master · NodeBB/nodebb-plugin-quickstart
GitHub (github.com)
plugin.addAdminNavigation = function(header, callback) { header.plugins.push({ route: '/plugins/quickstart', icon: 'fa-tint', name: 'Quickstart' }); callback(null, header); };
I just dont get how the server passes data to the client, I am looking at all the source code I can find including the example but there is nothing obvious about this? Can someone please just tell me how to simply pass something from library.js to the client?
-
Here is what I have done, I finally figured out how to get a list of categories, I kind of like this method but I hate having to pass an array, but it is what it is.
var theme = {}; var catLength = [1,2,3,4,5,6,7]; var Categories = module.parent.require("./Categories"); theme.init = function(params, callback) { Categories.getCategoriesData(catLength, function(err, data) { data.forEach(function(something, index) { if(something) { console.log(data[index].name); } }); }); callback(); }; module.exports = theme;
Cool now I have all the categories and the next step is to create a dynamic header based on the categories, the problem is I am so confused on how to do that, I think I am missing something very obvious!
I am inside a theme and I want to pass this into the precompiled template!