55A string interpolation component that formats and interpolates a template string in a safe way.
66
77``` jsx
8- import Interpolate from ' @doist/react-interpolate'
8+ import Interpolate from " @doist/react-interpolate" ;
99
1010function Greeting () {
11- return (
12- < Interpolate
13- string= " <h1>Hello {name}. Here is <a>your order info</a></h1>"
14- mapping= {{
15- name: ' William' ,
16- a: < a href= " https://orderinfo.com" / > ,
17- }}
18- / >
19- )
11+ return (
12+ < Interpolate
13+ string= " <h1>Hello {name}. Here is <a>your order info</a></h1>"
14+ mapping= {{
15+ name: " William" ,
16+ a: < a href= " https://orderinfo.com" / > ,
17+ }}
18+ / >
19+ );
2020}
2121```
2222
@@ -40,24 +40,24 @@ Please see the [Interpolation syntax](./#interpolation-syntax) section below for
4040
4141An object that defines the values to be injected for placeholder and tags defined in the template string. Optional.
4242
43- - For placeholder or self-closing tag, the mapping value could be any valid element value
44- - For open & close tag, the mapping value could be either renderer function or an element.
43+ - For placeholder or self-closing tag, the mapping value could be any valid element value
44+ - For open & close tag, the mapping value could be either renderer function or an element.
4545
4646``` jsx
4747< Interpolate
48- string= " Hello {name}. Here is <orderLink>your order info</orderLink><hr/>. \
48+ string= " Hello {name}. Here is <orderLink>your order info</orderLink><hr/>. \
4949 Please contact <supportLink>support</supportLink> for help"
50- mapping= {{
51- // you can map placholder and self-closing tag to any valid element value
52- name: ' William' ,
53- hr: < hr className= " break" / > ,
50+ mapping= {{
51+ // you can map placholder and self-closing tag to any valid element value
52+ name: " William" ,
53+ hr: < hr className= " break" / > ,
5454
55- // you can map open & close tag to a rendering function
56- orderLink : (text ) => < a href= " https://orderinfo.com" > {text}< / a> ,
55+ // you can map open & close tag to a rendering function
56+ orderLink : (text ) => < a href= " https://orderinfo.com" > {text}< / a> ,
5757
58- // or you can map open & close tag to an element
59- supportLink: < a href= " https://orderinfo.com" / > ,
60- }}
58+ // or you can map open & close tag to an element
59+ supportLink: < a href= " https://orderinfo.com" / > ,
60+ }}
6161/ >
6262```
6363
@@ -83,21 +83,21 @@ Here is interpolation syntax you can use in your `string`.
8383#### Placeholder
8484
8585``` jsx
86- ' hello {user_name}'
86+ " hello {user_name}" ;
8787```
8888
8989Placeholder name should be alphanumeric (` [A-Za-z0-9_] ` ). Placeholders could be mapped to any valid element value.
9090
9191#### Open & close tags
9292
9393``` jsx
94- ' Here is <a>your order info</a>'
94+ " Here is <a>your order info</a>" ;
9595
9696// tag name could be any alphanumeric string
97- ' Here is <link>your order info</link>'
97+ " Here is <link>your order info</link>" ;
9898
9999// you can nest tag and placeholder
100- ' Here is <a><b>you order info {name}</b></a>'
100+ " Here is <a><b>you order info {name}</b></a>" ;
101101```
102102
103103Tag name should be alphanumeric (` [A-Za-z0-9_] ` ).
@@ -131,32 +131,32 @@ Open & close tag could be mapped to a rendering function, which would take a sin
131131
132132``` jsx
133133< Interpolate
134- string= " Here is <a>your order info</a>"
135- mapping= {{
136- a : (text ) => (
137- < a href= " https://orderinfo.com" >
138- < b> {text}< / b>
139- < br / >
140- < / a>
141- ),
142- }}
134+ string= " Here is <a>your order info</a>"
135+ mapping= {{
136+ a : (text ) => (
137+ < a href= " https://orderinfo.com" >
138+ < b> {text}< / b>
139+ < br / >
140+ < / a>
141+ ),
142+ }}
143143/ >
144144```
145145
146146Unclosed tag or incorrect nesting of tag would result in syntax error.
147147
148148``` js
149149// bad: no close tag
150- ' Here is <a>your order info'
150+ " Here is <a>your order info" ;
151151
152152// bad: incorrect tag structure
153- ' Here is <a><b>your order info</a></b>'
153+ " Here is <a><b>your order info</a></b>" ;
154154```
155155
156156#### Self closing tag
157157
158158``` js
159- ' Hello.<br/>Here is your order'
159+ " Hello.<br/>Here is your order" ;
160160```
161161
162162Tag name should be alphanumeric (` [A-Za-z0-9_] ` ). Self closing tags could be mapped to any valid element value.
0 commit comments