-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2031 from wellcometrust/nunjucks-jsx-renderer
Nunjucks jsx renderer
- Loading branch information
Showing
8 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import nunjucks from 'nunjucks'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import * as components from '../views/components/jsx-components'; | ||
|
||
export default class Component { | ||
constructor(env) { | ||
this.tags = ['componentJsx']; | ||
this.env = env; | ||
} | ||
|
||
parse(parser, nodes, /*lexer*/) { | ||
const token = parser.nextToken(); | ||
const args = parser.parseSignature(null, true); | ||
parser.advanceAfterBlockEnd(token.value); | ||
|
||
return new nodes.CallExtension(this, 'run', args); | ||
}; | ||
|
||
run(_/*context*/, name, model) { | ||
const html = ReactDOMServer.renderToString(components[name](model)); | ||
return new nunjucks.runtime.SafeString(html); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import {Map} from 'immutable'; | ||
import Component from './component'; | ||
import ComponentV2 from './component-v2'; | ||
import ComponentJsx from './component-jsx'; | ||
import Icon from './icon'; | ||
|
||
export default Map({ | ||
Component, ComponentV2, Icon | ||
Component, ComponentV2, Icon, ComponentJsx | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import JsxTestComponent from './jsx-test-component/jsx-test-component.js'; | ||
|
||
export { | ||
JsxTestComponent | ||
}; |
15 changes: 15 additions & 0 deletions
15
server/views/components/jsx-test-component/jsx-test-component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Generic component to test using JSX with nunjucks | ||
import React from 'react'; | ||
import cssGridClasses from '../../../filters/css-grid-classes'; | ||
|
||
export default function JsxTestComponent(props) { | ||
return ( | ||
<div className="css-grid__container"> | ||
<div className="css-grid"> | ||
<div className={cssGridClasses({s: 12, m: 12, l: 12, xl: 12})}> | ||
Hello {props.toWhat} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters