What jinja2 template features are available in Aggregation Formats? - getstream-io

The documentation says aggregation rules use Jinja2 template syntax (https://getstream.io/docs/#aggregated-feeds), however many Jinja2 features don't seem to be supported and return syntax errors when trying to preview on the getstream.io aggregated feed group config screen.
For example, {{ actor|replace(":", "|") }} and {{ actor|length }} return syntax errors, but {{ actor|lower }} and {{ actor|int }} seem to work fine.
Does anyone know what subset of Jinja2 features are actually supported?

You can only use a very limited set of filters. For example int, lower and strftime on the time object. Next to that you can use the if, else and endif control structures.
We're working on a complete list and will document them here: https://getstream.io/docs/#aggregated-feeds and update this SO answer accordingly.
We hope this satisfies your needs for now.

Related

How to truncate text and use the raw filter in Twig

The issue is that the raw Twig filter must go at the end of the chain for it to work correctly and replace the HTML entities with their corresponding characters. This causes a problem as I need to also use the truncate function. The truncation is happening correctly but in the instances where the truncation happens in the middle of one of the HTML entity strings the raw function then fails to remove this entity.
Current solution:
{{ BlogPost.description|striptags|truncate(80)|raw }}
Input string:
<p>It supports your pupils to think like scientists – but that doesn’t mean it's only for science!</p>"
What the current solution achieves:
It supports your pupils to think like scientists – but that doesn&rsq...
What I want to achieve:
It supports your pupils to think like scientists – but that doesn't m...

Are feed group and feed id available as separate variables in aggregation formats

The documentation says the following variables are available:
feed_group: the feed group (e.g. timeline)
feed_id: the feed id (e.g. 123)
However, when I preview this aggregation format {{ feed_group }}_!_{{ feed_id }} the result is something like _!_timeline_aggregated:040fd3ae-ad3b-4e64-95f9-32da6daff105, indicating feed_id contains the full feed string and feed_group is blank or undefined.
Is there any way to get separate feed_group and feed_id strings as the docs indicate?
[edited after conversation in the comments]
There was an unfortunate consistency in the docs which has been updated since. feed_group was indeed not set and the feed_id contained the feed_group as well, divided from the id with a semicolon (:).
For backward compatibility only the docs have been updated. The feed_id is now something like notification:123 and the feed_group is no longer a documented variable. Also verb.id got renamed to just verb.

json_decode collection in template

I have a collection passed from a controller action and added to the view from a variable which I can iterate through for all of the articles in a blog. Each article has its own JSONB column stored in a PostgreSQL table.
I am wondering how to iterate through each article in the blog while JSON decoding each piece of data stored in that specific JSONB column for neat display on a Twig template page. Does anyone have any idea about how to do this? Would it be done in the Controller action and stored into a variable or in a Twig template itself? Could it be done both ways? If so, I would be interested to see how it could be done both ways!
It is easy for me to figure out how to do so on each individual article show page since one article is selected and you can json_decode that one column from the table in the controller then pass a variable (for this example I will call it decoded).
To print a data attribute in a Twig template I can do something like this:
{{decoded['title']}}
Or this:
{{decoded['body']}}
So, how can you achieve something similar with an entire blog collection on a blog index page as opposed to one specific article from the blog table on a show page?
If I were trying to do the same thing with a typical textbook collection in a Twig template the variable passed by the controller to the view (Twig template) would be accessed and iterated through each column in the table like this:
{% for article in blog %}
{article.title}
{article.body}
{% endfor %}
So...I can access the newly added JSON column now similarly by adding it to a similar loop structure on a Twig template page and calling:
{article.json_data}
...but my problem is that there is no json_decode filter for Twig template variable output, only json_encode which can be called in a similar iteration loop like this:
{article.json_data | json_encode}
...but I need json_decode not json_encode, thus my question!
Thank you for any and all help offered!

Retrieving SQL query behind channel entries tag

EE 2.5.5
I'd like to find the exact SQL query that is run by a channel entries tag, I've tried displaying the database queries by I can't see any related to running a channel entries tag.
I've simplified the template and now just have the tag in it:
{exp:channel:entries channel="mychannel"
disable="member_data|comments|trackbacks|categories" dynamic="no" require_entry="yes"}
{title}
{/exp:channel:entries}
I hope I'm understanding what you need.
Try using {total_results} as opposed to {total_entries} or are you looking for something like SELECT COUNT(entry_id) FROM exp_channel_titles WHERE status="open"
Edit:
If your output profiler is enabled in the backend you should be able to see something like the image below. In the orange text you'll see build query function I'm speaking of.

Expression Engine - Is it possible for exp:channel:entries tag to have complex filters?

Coming from Codeigniter and new to Expression Engine, I am at a loss on how to do complex filters on the exp:channel:entries tag.
I am interested in this filters
status
start_on
stop_before
How do you do you implement filters for a complex condition like this?
(status=X|Y|Z AND start_on=A AND stop_before=B) OR (status=X AND start_on=C AND stop_before=D)
Is this even possible?
You can only use the search= parameter to search on “Text Input”, “Textarea”, and “Drop-down Lists” fields unfortunately. So you'd need to use the query module for this.
If you're just querying on those parameters you should be able to get the entry id's you need from the exp_channel_titles table, then use something like the Stash plugin to feed the entry_id's of the results into a regular channel entries tag. Yes it's nominally one more query that way but as EE abstracts the db schema fairly heavily the alternative is to get lost in a mess of JOINs.
So something like (pseudocode, won't work as is):
Get the entries, statuses are just a string in exp_channel_titles, entry_date is the date column you want - which is stored as a unix timestamp, so you'll need to select it with something like DATE( FROM_UNIXTIME(entry_date)) depending on the format of your filter data.
{exp:stash:set name="filtered_ids"}{exp:query sql="SELECT entry_id
FROM exp_channel_titles
WHERE status LIKE ...<your filter here>"
backspace="1"
}{entry_id}|{/exp:query}{/exp:stash:set}
Later in template:
{exp:channel:entries
entry_id="{exp:stash:get name="filtered_ids"}"
}
{!--loop --}
{/exp:channel:entries}
Yes it's a mess compared to what you're probably used to in pure CI, but the trade off is all the stuff you get for free from EE (CP, templating, member management etc).
Stash is awesome by the way - can be used to massively mitigate most EE performance issues/get around parse order issues
You can get a lot of this functionality using the search= parameter in your {exp:channel:entries...} loop.
It's not immediately clear to me how you'll get the complexity you seek, so you might end up resorting to the query module though.
If you're working with dates you might find the DT plugin useful.

Resources