Jump to content

Theme Tip: Dynamic(ish) forum feeds inside Pages databases

Recently, we had a post in our pre-sales forum that asked how to achieve a few different things with Pages. One of the questions asked was if it was possible to show topics from a particular forum in each database record. While Pages can create a topic for each record for you, there's no way to associate an entire forum with a record.

In my reply, I indicate that you'd need to have a forum ID stored with each record in a custom field, and then use PHP to interact with our API to pull the topic list.

As it turns out, however, there's an easier way that I discovered after some experimentation. In hindsight it's obvious, but I want to share it here because it could open up some other interesting possibilities with some creative uses.

Setting up blocks

The first thing we need to do is create our blocks. We're going to create a block for each of our forums. You can set whatever parameters you want here, but the important thing is that they're named consistently using the forum ID. So, for my forum ID 2, I've named the block forum_2. This will allow us to include our blocks later.


Creating one of the blocks we'll need


Adding the field

Next we'll need to create a field in our Pages database that will be used to set the forum ID that is going to show in each record. For simplicity, I'm creating a Number field and I'll enter the forum ID manually, but if you wanted to go further, you could create a Select Box field, with the key being each forum and the value being the name. This would give you a friendlier input from which to select the forum for each record.

Here, though, I've just created the Number field, and named it Forum ID.


Setting up the database field


Using the field formatter to show the correct block

Finally, we'll use the Field Formatting options to show the correct block based on the forum ID entered for each record. On the Display Options tab, I'm going to hide the field from the listing template, but show it on the display template. I've selected Custom as the format, then entered this format:

{{if $formValue}}

That's it - that's all you need for this to work. It's very simple. All we're doing is passing the $formValue of the field (which is the raw value) into the {block} tag as a variable, so that the block that is rendered depends on this value. As long as a block exists with the correct key, it'll be shown in the display view:

Vestibulum Fringilla Porta Nullam - Records - IPS Community Suite 2016-05-08 12-34-53.jpg

End result, with the correct block pulled in based on the ID we provided to the record


Going further

So, given that we know we can use variables in block names to pull in different content (providing the block has been created ahead of time), what other possibilities are there? For starters, we aren't just restricted to using field formatters. Instead, we could use blocks directly in the database templates, using some of the data available there.

Here's one idea - if you have just a few staff members posting records, you could create a block for each staff member that lists their recent posts, status updates, etc. In your database template, you could include the correct block by doing this:



I hope this relatively simple approach gives you some ideas for more creative ways to use blocks. If you have any suggestions for other ways to use this approach, please let us know in the comments!



Recommended Comments

On a similar note, do you have an idea why this doesn't work, @Rikki? I'm trying to get around the fact that I need the content to change depending on which language I'm using.

What I've done is set up a language string which generates a bit of text depending on which language I'm using, "en" for English, "eo" for Esperanto. Then I have two blocks, one called "course-attendance-en" and another called "course-attendance-eo".

In my page I call "{block="course-attendance-{lang='eab_lang_code'}"} and ... nothing results. If I enter the full block names, then it works. But like this resolves to nothingness. I don't get it because if I use the language string on its own, then it correctly displays "en" or "eo". I thought I'd been dead clever but, alas. Do you have any idea why it doesn't work? It looks as though it should.

Link to comment
Share on other sites

42 minutes ago, Meddysong said:


Try checking the language first in a PHP IF query {{ if …}} and put your codes as result in a variable, e.g. {{$currentLanguage = "eo";}}. Then you can call the block just like in Rikki’s example, e.g. {block="course-attendance-{$currentLanguage}"}

Link to comment
Share on other sites

You've set me on the right path, Ralf! That could get a bit fiddly having to do that all the time, so here's my work-around:

- I need to get the letters en or eo, depending on language.
- In both cases, these letters are the first two of the locale, en-GB.UTF8 and eo-XX.UTF8.
- So if I can find a way of getting the first two letters of the locale, then they can by my variable to be appended to the block name.

  {{$currentLanguage = substr(\IPS\Member::loggedIn()->language()->short, 0, 2);}}

And there we go ^_^

It will still get a bit repetitive if I have to do that in every template, of course, so it would be better if I could make $currentLanguage a global variable. Is that what constants.php is for?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...