4

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

3 Answers 3

11

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') ?>
Sign up to request clarification or add additional context in comments.

Comments

3

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>

2 Comments

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.
0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.