function test_wrap_script () { $this->assertEquals ('<foo>', Page::wrap_script ('<foo>')); $this->assertEquals ("<script src=\"http://www.example.com/foo.js\"></script>\n", Page::wrap_script ('http://www.example.com/foo.js')); $this->assertEquals ("<script src=\"/foo.js\"></script>\n", Page::wrap_script ('/foo.js')); $this->assertEquals ("<script src=\"/foo.bar\"></script>\n", Page::wrap_script ('/foo.bar')); $this->assertEquals ("<link rel=\"stylesheet\" href=\"/foo.css\" />\n", Page::wrap_script ('/foo.css')); }
/** * Add a script to the head or tail of a page, or echo immediately if the template * rendering has already begun. Tracks duplicate additions so scripts will only * be added once. This makes it a good replacement for adding script include tags * to view templates. */ public function add_script ($script, $add_to = 'head', $type = '') { $script = Page::wrap_script ($script, $type); if (! in_array ($script, $this->scripts)) { $this->scripts[] = $script; if ($this->is_being_rendered) { echo $script; } elseif ($add_to === 'head') { $this->head .= $script; } elseif ($add_to === 'tail') { $this->tail .= $script; } } }