V8.5: How to swap block render on page depending on route?

I have a page with a block showing a list of items at a route /item-list. How can I set a block controller to render separate views (view.php) and use their own assets view.css and view.js on the same page based on an action? e.g.

/item-list will use the block’s view.php, view.css and view.js (no action, simply view())
/item-list/item/123 same controller but render different view.php, view.css and view.js (action_item())

I tried rendering from a templates/view.php - it renders the view but uses the default view.css/js, not those in the templates folder.

Maybe just keep rendering the view but make it load a different element for each route?

Is there documentation on how to use elements? Although that doesn’t solve the problem that I need different css and js for each view


That’s doesn’t solve the problem with assets.

JeRo suggested to try require different assets depending on the action. How couldn’t I think of that before? )))

I’ve simply added that to the view():

    if ($this->getAction() !== 'item') {
        $this->requireAsset('javascript', 'item_list');
        $this->requireAsset('css', 'item_list');

and it seems to be working.

I have 2 blocks on a page: item_list and item.

Item List controller has:

    public function action_item() 

Item controller has:

    public function action_item($handle = null) 

Each view has

    $form_action = $view->action('submit', $app->make('token')->generate('new_item'));
    <form ... action="<?php echo $form_action?>" ...>

The item_list action is fine on a page /items:


but the item action is wrong:


The current page is /items/item/123. Why is the item block action not from the block but from the system edit?

I guess I can do:

$form_action = Url::to($page->getCollectionPath(), 'submit', $app->make('token')->generate('new_item'), $bID);

but is this the right way?

Do both controllers have action_submit() defined?

@Myq Yes, both controllers have the same action. I couldn’t find any better way but to swap block render based on action. Now the problem is I can’t get the page’s action and parameter, because controller->getAction() doesn’t return anything.