can i use pug (ex-jade) with react framework?

后端 未结 2 786
南旧
南旧 2020-12-16 02:43

i have read some of pug documentation. its said that i have to install pug first and i\'m already done that. then i have to require pug in my js file. but i don\'t know wher

2条回答
  •  独厮守ぢ
    2020-12-16 03:37

    With Pug, you have two options: render template to HTML string, passing the data object right away or render template to an efficient javascript function that outputs html when passed a data object.

    When using pug(alone) with dynamic data, the choice is obviously to compile to function, so that data can be applied on the client.

    However, React does not actually consume, or send to the client, html. If you read an explanation of JSX, you will see that it is just HTML-lookalike syntactic sugar that gets compiled to a javascript function that programmatically creates DOM nodes (essential for the way React handles diffing and updating the page). Pug at the moment, even on the client, outputs an HTML string. Hence, the only way we will be able to use it is dangerouslySetInnerHTML as following:

    //from https://runkit.io/qm3ster/58a9039e0ef2940014a4425b/branches/master?name=test&pug=div%20Wow%3A%20%23%7Ba%7D%23%7Bb%7D
    function pug_escape(e){var a=""+e,t=pug_match_html.exec(a);if(!t)return e;var r,c,n,s="";for(r=t.index,c=0;r]/;
    function pug_rethrow(n,e,r,t){if(!(n instanceof Error))throw n;if(!("undefined"==typeof window&&e||t))throw n.message+=" on line "+r,n;try{t=t||require("fs").readFileSync(e,"utf8")}catch(e){pug_rethrow(n,null,r)}var i=3,a=t.split("\n"),o=Math.max(r-i,0),h=Math.min(a.length,r+i),i=a.slice(o,h).map(function(n,e){var t=e+o+1;return(t==r?"  > ":"    ")+t+"| "+n}).join("\n");throw n.path=e,n.message=(e||"Pug")+":"+r+"\n"+i+"\n\n"+n.message,n}function test(locals) {var pug_html = "", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_debug_line;try {;var locals_for_with = (locals || {});(function (a, b) {;pug_debug_line = 1;
    pug_html = pug_html + "\u003Cdiv\u003E";
    ;pug_debug_line = 1;
    pug_html = pug_html + "Wow: ";
    ;pug_debug_line = 1;
    pug_html = pug_html + (pug_escape(null == (pug_interp = a) ? "" : pug_interp));
    ;pug_debug_line = 1;
    pug_html = pug_html + (pug_escape(null == (pug_interp = b) ? "" : pug_interp)) + "\u003C\u002Fdiv\u003E";}.call(this,"a" in locals_for_with?locals_for_with.a:typeof a!=="undefined"?a:undefined,"b" in locals_for_with?locals_for_with.b:typeof b!=="undefined"?b:undefined));} catch (err) {pug_rethrow(err, pug_debug_filename, pug_debug_line);};return pug_html;}
    // pug source: "div Wow: #{a}#{b}"
    // this would obviously be much shorter if you include pug-runtime globally in your application
    
    function createMarkup(a,b) {
      return {__html: test({a:a,b:b})};
    }
    
    function MyComponent(props) {
      return 
    ; } ReactDOM.render( , document.getElementById('root') )
    
    
    

    Alternatively, there are attempts to translate jade or pug syntax into react directly, such as pug-react-compiler and babel-plugin-transform-pug-to-react. It seems they solved including further react components inside the pug template, which might be a desirable tradeoff for them possibly having quirks.

提交回复
热议问题