asmisha
asmisha

Reputation: 75

How should i put total number of articles on every page

I've just started to learn symfony 2, and I have a small question

For example I have a site with articles. I want to show the total number of articles on every page, but every page is generated by some bundle.

So I need kind of some common script to do initialization of main variables, such as articles count.

How should I do it?

Upvotes: 1

Views: 250

Answers (1)

Steven Mercatante
Steven Mercatante

Reputation: 25305

A simple way to achieve this is to have an action that fetches and outputs the total number of articles. You can then render that action from within other views (ex: $view['actions']->render('ArticlesBundle:Articles:getCount')). If you want this displayed on every page of your site, I would recommend rendering it from within your base.html.php view (or base.html.twig depending on which templating engine you're using). I'd also recommend caching it somehow, so you're not constantly hitting the database for a count that may not change very often.

See the docs on Embedding Controllers for more detail.

You can further expand on this example to define other 'main variables' that you want to use throughout your site. One idea is to define a service object whose purpose is to hold these variables, and then access its contents when you need to.

By the way, the title of your question is not descriptive of your problem, please consider rephrasing it.

Upvotes: 7

Related Questions