netcase netcase - 1 year ago 160
Javascript Question

Typo3 extbase - fluid variable to jquery

How could one pass a fluid variable from the controller to jQuery or JavaScript?

Inside my controller:

public function tabs () {
$this->view->assign('tab', 1);

Inside fluid I'd use the variable as {tab}
But how can I pass to JavaScript? Any help would be appreciated...

Answer Source

JS in fluid templates behaviour is ... terrible in most cases, if your JS code contains any curly brackets you need to include external script instead placing the script in the view.

The reason is that Fluid thinks, that all curly brackets belongs to its scope, so when you write:

$('#mySelectBox').change(function() {
    // functions body code
    // next line
    // next line

Fluid destroys the JS and renders it as just:

$('#mySelectBox').change(function() Array);

Then most often I use something like this:

in the view:

<script>var TxMyExtKeyTabNumber = {tab}</script>
<script src="path/to/static/script.js"></script>

in script.js: