سؤال

I'm trying out a more maintainable way to structure my code as learnt by some tutorials at nettuts+.

I've succeeded in making my header and footer files separate from the main content of each page so that they are loaded along with each different page, thus making it easier to manage changes to these two files.

I currently want to add some jQuery code to my footer, for only one page. This is where I've hit a wall in thinking. How can I allow this jQuery code to be present in my footer for only one page, and not all pages? Could someone please explain?

I was thinking of doing something like:

<?php if($title = "The jQuery Page"){?>
    <script>
       jquery code here....
    </script>
<?php } ?>

But I think that is considered messy, as you are using one language to set off another.

Edit: Here's the requested code:

In my Controller I call the views:

 $data['title'] = "The jQuery Page";
    $this->load->view('header', $data);
    $this->load->view('cool_page');
    $this->load->view('footer');

Then in my footer I want to load a specific script in only this one pages footer:

<script src="<?php echo base_url();?>js/jquery-1.10.2.min.js"></script>
        <script>
            $(document).ready(function() {

                /*cool jquery stuff*/

            });
        </script>
هل كانت مفيدة؟

المحلول

Try to load a view into the main view

/application/controllers/test.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
  public function index() {
    $data = array(
      'title' => "The jQuery Page", 
      'script' => TRUE // if you omit or comment this variable the 'views/script.php' is not loaded
    );
    $this->load->view('test', $data);
  }
}

/application/views/test.php

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
  <?php if (isset($script)) { $this->load->view('script'); } ?>
</body>
</html>

/application/views/script.php

<script>
  jquery code here....
</script>

نصائح أخرى

You could specify JS to include as part of the data passed to the view:

CONTROLLER1

$this->data['js'][] = 'alert(\'Hi\');';
$this->data['js'][] = 'alert(\'Hey\');';

$this->load->view('footer', $this->data);

CONTROLLER2

$this->data['js'][] = 'alert(\'Yo\');';
$this->data['js'][] = 'alert(\'Sup\');';

$this->load->view('footer', $this->data);

Footer VIEW

if(isset($js) && is_array($js) && count($js) > 0){
    echo '<script>';
    foreach($js as $key=>$value){
        $value;
    }
    echo '</script>';
}

I don't think there is a way around making a conditional 'if' statement around the script, so I used the suggested solution in my question.

Check the title to see if it is the desired page:

<?php if($title = "Photo Albums - Hands of Humanity"):?>

Then add the script to the DOM if it is so:

<script>
$(document).ready(function() {

//jquery stuff

});
</script>
<?php endif;?>

If there are other ways which are better for maintainability, I would be happy to hear them

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top