How do you indent JavaScript printed by PHP (or another server side language)?
-
For a better idea of what I'm asking consider: A) No JavaScript indentation: echo "_____"; echo "_____"; echo "_____"; B) Within PHP source: echo "_____"; echo "_____"; echo "_____"; C) With spaces (so that resulting identation is visible in HTML source): echo "_____"; echo " _____"; echo " ______";
-
Answer:
There's rarely a good reason to do either of those options. If you really can't think of a better way to do it (e.g. simply using PHP to generate a JSON configuration object), then I would just embed PHP in the JS: /** * Generated JS */ myApp.foo = function(bar) { var pattern = '<?php echo $foo->getPattern(); ?>'; var id = '<?php echo $foo->getId(); ?>'; return bar.match(pattern); }; or create a JS template: /** * JS Template */ myApp.foo = function(bar) { var pattern = %pattern%; var id = %id%; return bar.match(pattern); };
Calvin Huang at Quora Visit the source
Other answers
if it's a single block of js then i would just use the fact that you can spread a single php string over several lines. this will give you your desired js formatting. for example: printf(" myApp.foo = function(bar) { var pattern = '%s'; var id = '%s'; return bar.match(pattern); }; ", $foo->getPattern, $foo->getId() );
Bhu Boue Vidya
Related Q & A:
- How to upload a file to another server using cURL and PHP?Best solution by Stack Overflow
- How to change javascript values to php?Best solution by stackoverflow.com
- How to pass a javascript object back to the server?Best solution by developer.mozilla.org
- How to get JavaScript value in PHP?Best solution by Stack Overflow
- How to call Javascript function in PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.