jbboehr / handlebars.c

C implementation of handlebars.js
GNU Lesser General Public License v2.1
35 stars 5 forks source link

Question: Documentation / this #53

Closed flip111 closed 7 years ago

flip111 commented 7 years ago

I saw there is a doc folder with some doxygen stuff in it. Is the available documentation available online to read in a browser? That would be great. If there is, could the link to the documentation be added in the readme?

I wanted to read about the this variable, in javascript this variable is a reference to the original data you passed in (see example below), how does the C implementation handle this?

<!doctype html>
<html lang="en">
<head>
  <title>handlebars test</title>
</head>
<body>
  <script src="handlebars-v4.0.5.js"></script>
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
{{#myCustomBlock}} some data to analyze{{/myCustomBlock}}
{{#otherPart}}use analyzed data{{/otherPart}}
</script>
<script>
Handlebars.registerHelper('myCustomBlock', function(options) {
  this.extradata = options.fn();
  return options.fn(this);
});
Handlebars.registerHelper('otherPart', function(options) {
  console.log(this);
  // Output of this console.log is:
  // Object {startData: "initial data loaded", extradata: " some data to analyze"}
  // this means that i can transfer data from one block to another block even if loose scope
  // because this is a reference to the original data i passed in and not a copy !!
  return options.fn(this);
});
var source   = $("#entry-template").html();
var template = Handlebars.compile(source);
var context = {startData: "initial data loaded"};
var html    = template(context);
</script>
</body>
</html>
jbboehr commented 7 years ago

There is not currently documentation available online. I'll make an issue (#54) for it.

Of course, it's not possible to directly replicate this in C. The value that would be this is passed as options->scope: https://github.com/jbboehr/handlebars.c/blob/v0.6.0/tests/fixtures.c#L103-L105

flip111 commented 7 years ago

Not sure where options is coming from? Is it a pointer or a copied value?

jbboehr commented 7 years ago

It's a pointer: https://github.com/jbboehr/handlebars.c/blob/master/src/handlebars_helpers.h#L49 https://github.com/jbboehr/handlebars.c/blob/master/src/handlebars_vm.c#L124 https://github.com/jbboehr/handlebars.c/blob/master/src/handlebars_helpers.c#L70

flip111 commented 7 years ago

thank you for explanation