Displaying Discourse Variables on Other Pages

סיכום מבוסס בינה מלאכותית

In a discussion titled “Displaying Discourse Variables on Other Pages” under the Dev category, Gnisha seeks help displaying site statistics (like user, topic, and post counts) from the /about page onto the forum’s front page via a custom plugin. They attempted using {{number model.stats.user_count}} in their plugin’s .hbs file but only saw 0 displayed despite having active users. pfaffman suggests checking if the plugin’s context has access to the required data or fetching it from an existing page. Gnisha clarifies they’re building a plugin to show board stats similar to vBulletin, sharing their current HBS template code and noting they copied the syntax from Discourse’s about.hbs. fzngagan explains that model.stats is populated by the route’s model method and offers two approaches: calling the relevant Rails controller via AJAX or replicating its logic in a custom controller. They also recommend this guide for foundational understanding. Gnisha acknowledges the advice and shares their plugin repo: https://github.com/NCC-Gnisha/discourse-site-statistics, planning to review the guide for further progress.

Hello.
I am wanting to display some site statistics from the /about page on the front page in combination with another plugin.

For example, I’ve added Members: {{number model.stats.user_count}} to the .hbs file of a plugin, but when I rebuild discourse all it displays is 0. I have more than 0 users. :slight_smile:

Any ideas, or hints?

It’s hard to say without knowing more about the plugin. It might be easier to get that data from a page that has it already.

Basically, I’m writing a plugin to display board statistics on the bottom of the forum, similar to what vBulletin does.

So far, I have this:
SiteStats

In my plugin directory, my /assets/javascripts/discourse/components/site-stats.hbs is the following:

<div class="board-statistics-headrow">{{d-icon "far-chart-bar"}}  {{i18n 'about.stats'}}</div>
<div class="board-statistics-row">
  Threads: {{number model.stats.topic_count}}  Posts: {{number model.stats.post_count}}  Members: {{number model.stats.user_count}}>
</div>
</div>

I grabbed the {{number model.stats.topic_count}} from the about.hbs file inside the Discourse GitHub repository.

You have two ways to do this.

Just check which (Rails) controller is being called on the /about page to fetch the data you need.

In your ember route file’s model method, you can call that controller via ajax and fetch the required data.

Otherwise,

If you’ve written your own controller, then create a new method and copy the logic from the controller you found to your new controller and (maybe) filter the data you need if that’s required.

this model variable is what is returned by the model method of the route in question.

I think you should go through this guide to get a holistic understanding of how things work in discourse.

Alright. I’ll check out that Plugin guide and see if I can make any additional progress.

So far, I have https://github.com/NCC-Gnisha/discourse-site-statistics