Feature #36410

Allow templates to send arguments back to layout

Added by Florian Carstens almost 9 years ago. Updated over 8 years ago.

Status:
New
Priority:
Should have
Assignee:
-
Category:
ViewHelpers
Target version:
-
Start date:
2012-04-21
Due date:
% Done:

0%

Estimated time:
Has patch:
No

Description

Layouts can differ a little on a website from page to page without affecting the whole layout. For example: Some pages contain a sidebar, others not. Or: Each page has it's own background which is set by a body or div container css class. I would like to set these kind of settings by sending arguments to the layout. That could e.g. be realised by extending the layout viewHelper:

  <f:layout name="Master" arguments="{cssClass: blueBackground}" />

Of course, I can do this in the controller. But I think this wouldn't be the right place according to the MVC design pattern.

#1

Updated by Adrian Föder over 8 years ago

I voted down for a simple reason: Sending back something removes the "side-effect-freeness" of Fluid Views (take it literally: they are Views). A View should be "read only", or "non-desctructive", or whatever you like.
For the same reason, by the way, I also agree to not having writable variables inside a view (like eg Smarty has).

#2

Updated by Alexander Berl over 8 years ago

Good point. Do you have any ideas for alternative solutions to this problem?
I have similar issues, which are currently solved by including different stylesheets depending on the current view (can be done via ViewHelper or creating a <section> inside the layout <head>), but it's sometimes a hassle to have these sections in every template even if you don't need it.

#3

Updated by Adrian Föder over 8 years ago

well, what solves most is having an own, "project specific" abstract class lying between the framework's own ActionController and your concrete action controllers:

abstract class AbstractBaseController extends \TYPO3\FLOW3\Mvc\Controller\ActionController

Then I have such one:

    /**
     * Initializes the view before invoking an action method.
     *
     * @param \TYPO3\FLOW3\Mvc\View\ViewInterface $view The view to be initialized
     * @return void
     */
    protected function initializeView(\TYPO3\FLOW3\Mvc\View\ViewInterface $view) {
        $localeCode = (string)$this->localizationService->getConfiguration()->getCurrentLocale();
        $localeCode = str_replace('_', '-', $localeCode);
        $this->response->setHeader('Content-Language', $localeCode);
        $view->assign('localeCode', $localeCode);

        $view->assign('account', $this->account);
        $view->assign('bodyClass', $this->generateBodyClassName());
    }

The generateBodyClassName() creates a smart representation of the current controller, representing entity, logged-in/logged-out status etc; and from here on most can be done via CSS.
take, for example,

<body class="ShopEntity ShopEntityOrderAction authenticated">

or similar (just an example).

So again, that abstract controller is pretty helpful so that I even encourage to do this as best-practice.

Also available in: Atom PDF