admin管理员组

文章数量:1025497

Question: Does JavaScript have an equivalent to PHPs output buffering (start, get_clean) or <<< EOF ... EOF syntax to wrap inline HTML in a variable? Shims, libraries, functions, anything that gets the job done in modern browsers.

Why: I'd like to try to make a MVC framework in pure JS and the thought of creating blocks of HTML using strings or reading files and doing find/replace on keywords makes me wonder how efficient/maintainable the code would be.

Question: Does JavaScript have an equivalent to PHPs output buffering (start, get_clean) or <<< EOF ... EOF syntax to wrap inline HTML in a variable? Shims, libraries, functions, anything that gets the job done in modern browsers.

Why: I'd like to try to make a MVC framework in pure JS and the thought of creating blocks of HTML using strings or reading files and doing find/replace on keywords makes me wonder how efficient/maintainable the code would be.

Share Improve this question edited Feb 1, 2012 at 16:10 david 2,5851 gold badge36 silver badges52 bronze badges asked Jan 31, 2012 at 15:44 JonathanJonathan 5439 silver badges24 bronze badges 2
  • 1 I think you want templates. JavaScript operates on the DOM, not on the HTML source code. The purpose of the HTML source code is to be parsed by the browser. – Šime Vidas Commented Jan 31, 2012 at 15:45
  • No. JS has no 'heredocs'. There's also no output buffering, unless the JS interpreter in the browser caches/delays dom modifications for rendering efficiency. – Marc B Commented Jan 31, 2012 at 15:46
Add a ment  | 

3 Answers 3

Reset to default 3

To answer your question, no, JavaScript doesn't have anything like heredoc syntax. (CoffeeScript does, but ewww.)

You should take a look at how templating engines are implemented. I'm a fan of doT, which is very efficient. You define your template in a script block, load the template source from there, and the engine piles it into a function. (One of the few legitimate uses of eval.)

<script type="text/x-dot-template" id="mytmpl">
Hello, <b>{{=it.name}}</b>
</script>

 

var tmpl = doT.template($('#mytmpl').html());
tmpl({name:'test'}); // => 'Hello, <b>test</b>'

This keeps your markup out of your JavaScript and in the HTML, where it belongs.

No, JavaScript does not. The next version of ECMAScript has this as a proposal. But who knows when browsers will actually support it.

A mon alternative nowadays is to add a <script type="text/html"> block to your code (sometimes type is text/template, this seems arbitrary) , and load that block using JavaScript. Many templating tools now do this. Since it's just an HTML tag, you can place whatever you want inside, no need to concat strings like a madman.

I don't know what you mean with "output buffering". Every action in JavaScript executes immediately, though some layout rendering might be delayed by the browser.

There is no EOF-syntay in Javascript, but you can escape linebreaks with a backslash:

"a\
b" === "ab"; // true

That might help for readability.

Question: Does JavaScript have an equivalent to PHPs output buffering (start, get_clean) or <<< EOF ... EOF syntax to wrap inline HTML in a variable? Shims, libraries, functions, anything that gets the job done in modern browsers.

Why: I'd like to try to make a MVC framework in pure JS and the thought of creating blocks of HTML using strings or reading files and doing find/replace on keywords makes me wonder how efficient/maintainable the code would be.

Question: Does JavaScript have an equivalent to PHPs output buffering (start, get_clean) or <<< EOF ... EOF syntax to wrap inline HTML in a variable? Shims, libraries, functions, anything that gets the job done in modern browsers.

Why: I'd like to try to make a MVC framework in pure JS and the thought of creating blocks of HTML using strings or reading files and doing find/replace on keywords makes me wonder how efficient/maintainable the code would be.

Share Improve this question edited Feb 1, 2012 at 16:10 david 2,5851 gold badge36 silver badges52 bronze badges asked Jan 31, 2012 at 15:44 JonathanJonathan 5439 silver badges24 bronze badges 2
  • 1 I think you want templates. JavaScript operates on the DOM, not on the HTML source code. The purpose of the HTML source code is to be parsed by the browser. – Šime Vidas Commented Jan 31, 2012 at 15:45
  • No. JS has no 'heredocs'. There's also no output buffering, unless the JS interpreter in the browser caches/delays dom modifications for rendering efficiency. – Marc B Commented Jan 31, 2012 at 15:46
Add a ment  | 

3 Answers 3

Reset to default 3

To answer your question, no, JavaScript doesn't have anything like heredoc syntax. (CoffeeScript does, but ewww.)

You should take a look at how templating engines are implemented. I'm a fan of doT, which is very efficient. You define your template in a script block, load the template source from there, and the engine piles it into a function. (One of the few legitimate uses of eval.)

<script type="text/x-dot-template" id="mytmpl">
Hello, <b>{{=it.name}}</b>
</script>

 

var tmpl = doT.template($('#mytmpl').html());
tmpl({name:'test'}); // => 'Hello, <b>test</b>'

This keeps your markup out of your JavaScript and in the HTML, where it belongs.

No, JavaScript does not. The next version of ECMAScript has this as a proposal. But who knows when browsers will actually support it.

A mon alternative nowadays is to add a <script type="text/html"> block to your code (sometimes type is text/template, this seems arbitrary) , and load that block using JavaScript. Many templating tools now do this. Since it's just an HTML tag, you can place whatever you want inside, no need to concat strings like a madman.

I don't know what you mean with "output buffering". Every action in JavaScript executes immediately, though some layout rendering might be delayed by the browser.

There is no EOF-syntay in Javascript, but you can escape linebreaks with a backslash:

"a\
b" === "ab"; // true

That might help for readability.

本文标签: javascriptOutput buffer in JSStack Overflow