Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Knockout.js virtual elements #958

Closed
wants to merge 1 commit into from
Closed

Generate Knockout.js virtual elements #958

wants to merge 1 commit into from

Conversation

vstirbu
Copy link

@vstirbu vstirbu commented Apr 10, 2013

Add ability to generate Knockout.js virtual elements from block comments. For example:

//ko if: true
  #test

outputs:

<!-- ko if: true -->
<div id="test"></div>
<!-- /ko -->

@vendethiel
Copy link
Contributor

What about passing your own Compiler ? I think it's better in your use case since this kind of things does not belong in the official compiler

@vstirbu
Copy link
Author

vstirbu commented Apr 11, 2013

Yes, this feature does not concern an audience as large as that of the vanilla jade compiler, but forking jade just to add this feature is not very nice either... You just lost all new fixes and features that come along in jade without actively maintaining the fork.

Is there a way to gracefully extend the compiler to handle what might be considered niche cases?

@vendethiel
Copy link
Contributor

Yes, you can pass it as an option - sorry I was unclear :)!

@vstirbu
Copy link
Author

vstirbu commented Apr 11, 2013

Got it now! :)

So, basically i can have a "patch" compiler that does its stuff and then pass the intermediate result to jade.

@@ -454,6 +454,11 @@ Compiler.prototype = {
this.buffer('<!--[' + comment.val.trim() + ']>');
this.visit(comment.block);
this.buffer('<![endif]-->');
} else if (0 == comment.val.trim().indexOf('ko')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this still work if it was 'ko '. If so, that would be less likely to catch things that just happened to be comments starting with ko.

@ForbesLindesay
Copy link
Member

You can do something like:

var Compiler = require('jade').Compiler;
function KoCompiler(node, options) {
  Compiler.call(this, node, options);
}
KoCompiler.prototype = Object.create(Compiler);
KoCompiler.prototype.constructor = KoCompiler;

var oldVisitBlockComment = Compiler.prototype.visitBlockComment;
KoCompiler.prototype.visitBlockComment = function (comment) {
    if (0 == comment.val.trim().indexOf('ko ')) {
      this.buffer('<!-- ' + comment.val.trim() + ' -->');
      this.visit(comment.block);
      this.buffer('\\n');
      this.buffer('<!-- /ko -->');
    } else {
      return oldVisitBlockComment.call(this, comment);
    }
};

then just do:

jade.renderFile('foo.jade', {compiler: KoCompiler}, callback);

@tj
Copy link
Contributor

tj commented Apr 12, 2013

I don't like the idea of special-casing for any particular framework sorry! Too much of a maintenance burden

@tj tj closed this Apr 12, 2013
@vstirbu
Copy link
Author

vstirbu commented Apr 15, 2013

Yes, it makes sense to close this, and ForbesLindesay' comment provides a nice solution for my problem.

@nashibao
Copy link

What about using jade mixin? I wrote an example here. https://gist.github.com/nashibao/7889825

mixin if(val)
  <!-- ko if: #{val} -->
  block
  <!-- /ko -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants