Guest User

Untitled

a guest
Jun 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. ----------------------------------------------------------------------
  3. Jinja Macro with simple Call
  4. ----------------------------------------------------------------------
  5. {% macro render_dialog(title, class='dialog') -%}
  6. <div class="{{ class }}">
  7. <h2>{{ title }}</h2>
  8. <div class="contents">
  9. {{ caller() }}
  10. </div>
  11. </div>
  12. {%- endmacro %}
  13.  
  14. {% call render_dialog('Hello World') %}
  15. This is a simple dialog rendered by using a macro and
  16. a call block.
  17. {% endcall %}
  18. */
  19.  
  20. // Equivalent with renderProps
  21. import React from "react";
  22. import ReactDOM from "react-dom";
  23.  
  24. const A = props => (
  25. <div className={props.className}>
  26. <h2>Render Props</h2>
  27. <div class="contents">{props.children()}</div>
  28. </div>
  29. );
  30.  
  31. const App = () => (
  32. <A className="app-block">{() => <div>{`I'm rendered`}</div>}</A>
  33. );
  34.  
  35. const rootElement = document.getElementById("root");
  36. ReactDOM.render(<App />, rootElement);
  37.  
  38. /**
  39. ----------------------------------------------------------------------
  40. Jinja Macro call block can be used with arguments
  41. ----------------------------------------------------------------------
  42. {% macro dump_users(users) -%}
  43. <ul>
  44. {%- for user in users %}
  45. <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>
  46. {%- endfor %}
  47. </ul>
  48. {%- endmacro %}
  49.  
  50. {% call(user) dump_users(list_of_user) %}
  51. <dl>
  52. <dl>Realname</dl>
  53. <dd>{{ user.realname|e }}</dd>
  54. <dl>Description</dl>
  55. <dd>{{ user.description }}</dd>
  56. </dl>
  57. {% endcall %}
  58. */
  59.  
  60. // Equivalent with renderProps
  61. import React from "react";
  62. import ReactDOM from "react-dom";
  63.  
  64. const users = [{ name: "John" }, { name: "Doe" }];
  65.  
  66. const A = ({ className, users, children }) => (
  67. <div className={className} users={users}>
  68. <h2>Render Props</h2>
  69. <div class="contents">{users.map(user => children(user))}</div>
  70. </div>
  71. );
  72.  
  73. const App = () => (
  74. <A className="app-block" users={users}>
  75. {user => <div key={user.name}>{`${user.name} is rendered`}</div>}
  76. </A>
  77. );
  78.  
  79. const rootElement = document.getElementById("root");
  80. ReactDOM.render(<App />, rootElement);
Add Comment
Please, Sign In to add comment