You can define the string in the controller before displaying it in the Twig template:
Controller
public function indexAction() {
$js = '<script type="text/javascript">'.
'document.getElementById("DivInfo").style.display = "block";'.
'</script>';
return $this->render(
'TestBundle:Default:GuiLogin.html.twig',
array(
'js' => $js
)
);
}
Twig template
Place this code before the </html>
tag:
<div id="DivInfo" style="display:none;">
DivInfo
</div>
{% if (js is defined) %}{{ js|raw }}{% endif %}
The <div id="DivInfo">
is hidden by default and displayed if Javascript is enabled.
Comments
I don't understand what is the logic behind your question. There are several ways to display a code only in some conditions. Defining Javascript functions in the Controller is a bad idea since its more logic to put Javascript code in .js
files or in Twig HTML files.
Better solution
According to your comments you are checking the user credentials, so you can perform the test in the controller:
Controller
public function indexAction() {
$message = null;
if (check credentials)
{
$message = 'whatever';
}
return $this->render(
'TestBundle:Default:GuiLogin.html.twig',
array(
'message' => $message
)
);
}
Twig template
<div id="DivInfo" style="display:none;">
DivInfo
</div>
{% if ((message is defined) and (message is not null)) %}
<div>{{ message }}</div>
{% endif %}