0

hmmm, i know i can do this with jquery but i'm trying no to use javascript and instead use php. also to learn from.

I have a folder hierarchy like this

modules
 |-navagation
    |-js
       |-nav.js

and I want to find a way to append that .js to the

 <head></head>

like this

<html>
    <title></title>
    <head>
        <script scr="jquery.js"></script>
        <script src="nav.js"></script>
    </head>
</html>

My goal is that I want to upload modules to my modules folders and have the php automatically pick it up and load up the resources from the modules.

One thing that I'm struggling with is that I'm loading my files via Smarty using .tpl to display my markup. Thats the only berrier i'm dealing with.

my original method was this but because I'm doing it with template files i'm at a loss.

ob_start();
include(THEME_PATH . "/index.html");
$content = ob_get_contents();
ob_end_clean();

// Scripts to include
$scripts = BaseHelper::include_script("http://code.jquery.com/jquery-1.4.2.min.js");
$scripts .= BaseHelper::include_script("js/jquery.sound.js");
$scripts .= BaseHelper::include_script("js/jquery.jblock.js");
$scripts .= BaseHelper::include_script("js/init.js");

// Add scripts to theme
echo str_replace("</head>", $scripts . "\n</head>", $content);
Gordon
  • 312,688
  • 75
  • 539
  • 559
Eli
  • 4,329
  • 6
  • 53
  • 78
  • I don't fully understand your question. But try `` – Khez Apr 16 '11 at 08:11
  • 1
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Apr 16 '11 at 08:11
  • If you want to modifiy the DOM and already know jQuery, then you could use phpQuery (see link above). However, depending on your template, it might be easier to pass the script content to the template instead of changing the DOM. Basically, there is very little need to use Smarty, when you are going to modify your templates with DOM, because DOM can fully substitute Smarty. – Gordon Apr 16 '11 at 08:12

1 Answers1

0

I would turn your index.html template page into a php page, create your scripts prior var prior to including the template and then render them from within the included template file:

ob_start();
// Scripts to include
$scripts = BaseHelper::include_script("http://code.jquery.com/jquery-1.4.2.min.js");
$scripts .= BaseHelper::include_script("js/jquery.sound.js");
$scripts .= BaseHelper::include_script("js/jquery.jblock.js");
$scripts .= BaseHelper::include_script("js/init.js");
include(THEME_PATH . "/index.php");
$content = ob_get_contents();
ob_end_clean();

index.php:

<head>
<?php echo $scripts ?>
</head>
Demian Brecht
  • 21,135
  • 5
  • 42
  • 46