For example, I may have a flexible content field that gives you the option of either pulling content automatically from a post type or manually defining the content.
Say a slider block that would ask you if you want to display title and thumbnail of posts in a post type, or manually define a title and image for each slide with a repeater.
The context for the slides changes according to the chosen option. On one hand, you have to fetch posts and display the wanted information, on the other you simply use the manually defined content.
Here's what my files (basically) look like so far:
index.php
$context = Timber::get_context();
$context['post'] = ( is_front_page() ) ? new Timber\Post( get_option( 'page_on_front' ) ) : new Timber\Post();
Timber::render( 'page.twig', $context );
page.twig
{% extends 'base.twig' %}
{% block content %}
{% for bloc in post.meta('blocs') %}
{% include 'blocs/' ~ bloc.acf_fc_layout ~ '.twig' ignore missing %}
{% endfor %}
{% endblock %}
So twig files for each flexible layout are automatically included and I can access the fields just fine. However in my slider example the context for the slides changes depending on the selected options, and I'm trying to keep as much logic out of the twig files as possible.
Fetching posts inside the twig template and setting variables seems messy and counter to the goal of using twig in the first place. Is there a better way to handle this?