views:

524

answers:

3

Hello, My layout.php calls include_javascripts() before my componet could call sfResponse::addJavascript(). Is there a "helper" or a "best practice" to handle this?

Do I have to Seperate the call sfResponse::addJavascript()? I were happy to avoid it.

Here ist my actual workaround:

<head>
  <?php $nav = get_component('nav', 'nav') /* Please show me a better way. */ ?>
  <?php include_javascripts() ?>
  ...
</head>
<body>
  <?php /* include_component('nav', 'nav') */ ?>
  <?php echo $nav ?>
  ...
</body>

Thanks

A: 

You may have to disable the sfCommonFiter which is now deprecated. The common filter automatically adds css / js into the layout.

Once you have done this, you can move the include_javascripts call anywhere within the layout.php file, below the include_component('nav', 'nav') call

timmow
This wont work for CSS as `include_css` returns `link` tags which are not allowed inside `body`. And I guess the `sfCommonFilter` has little to do with the problem. In fact, I guess it was working with this filter, as the link and script tags where added through this filter **after** the template was rendered. It may work with `include_javascripts` though.
Felix Kling
+2  A: 

If your component is always used just move your javascript inclusion into the layout's rendering in your app's view.yml:

default:
  javascripts: [my_js]

There is no need to separate the JS call when it is always used.

UPDATE:

If you must maintain the JS inclusion with the component you can place your component call in a slot before your include_javascripts() call to add it to the stack to be rendered and then include the slot in the appropriate place:

<?php slot('nav') ?>
<?php include_component('nav', 'nav'); ?>
<?php end_slot(); ?>
<html>
  <head>
    <?php include_javascripts() ?>
    ...
  </head>
  <body>
    <?php include_slot('nav'); ?>
    <?php echo $nav ?>
    ...
  </body>
</html>
Cryo
And when I remove the component I have to stress my brain to clean the view.yml? If I remember and if I have no dependencies?
@koalabruder: Yes, you would have to remember to remove the JS as well. If you *really* must have the JS in the component file then you'll want to move the component inclusion into a slot. I've updated my answer with the details.
Cryo
A: 

From: http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer

File Inclusion Configuration

// In the view.yml

indexSuccess:
  stylesheets: [mystyle1, mystyle2]
  javascripts: [myscript]

// In the action

$this->getResponse()->addStylesheet('mystyle1'); 
$this->getResponse()->addStylesheet('mystyle2'); 
$this->getResponse()->addJavascript('myscript');

// In the Template

<?php use_stylesheet('mystyle1') ?>
<?php use_stylesheet('mystyle2') ?>
<?php use_javascript('myscript') ?>
Tom