-1

I was wondering if there are any parsers/tools to take out (and I guess substitute place holders) so that JS code that has PHP intermingled with it can be parsed by the google closure compiler tool.

If not, maybe someone has a clever idea (short of writing something like this) to avoid the tedious and messy task of doing it by hand.

purify
  • 1
  • Any particular reason why you have intermingled PHP and JS in the first place? Separating them should help you a lot, no special tools needed. – Amadan Aug 28 '14 at 01:46
  • possible duplicate of [Partially skip sections with Google Closure Compiler](http://stackoverflow.com/questions/10449195/partially-skip-sections-with-google-closure-compiler) – Chad Killingsworth Aug 28 '14 at 01:51
  • I would think that google-closure-compiler would recognize PHP tags. I've used PHP's `glob()` to put images into the cache with JavaScript, therefore the JavaScript is in PHP. – StackSlave Aug 28 '14 at 01:51
  • @Amadan: It can be really useful to "output" certain values into javascript variables or function calls or especially to decide whether or not to include certain chunks of js code based on things that must be evaluated on the server side. For example you may want to disable parts of an interface because the user doesn't have sufficient privileges. – purify Aug 28 '14 at 01:55

1 Answers1

1

The way I normally solve such issues: never intermingle code; strictly separate data initialisation from logic.

E.g.

...
<head>
  <script type="text/javascript">
    var data = <?php echo json_encode($data); ?>;
  </script>
  <script type="text/javascript" src="script.js"></script>
</head>
...

This way, script.js can be PHP-free.

Amadan
  • 191,408
  • 23
  • 240
  • 301