Skip to content

Commit d3643d9

Browse files
committed
Add rendering options to docs
1 parent 3200ca2 commit d3643d9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,58 @@ render(customElTag{
338338
}); // <custom-el data-thing="value">Hi</custom-el>
339339
```
340340
341+
### 5. Rendering
342+
343+
By default the `render` function appends everything to an internal string buffer, which it returns. However if you want to get that first byte out before rendering the whole doc, you can hook in with a function to stream the output while the rendering is still in progress:
344+
345+
```c++
346+
#include <sstream>
347+
std::stringstream out;
348+
size_t chunkSize{256};
349+
350+
// Our hook function takes rendered data, and a reference to the internal buffer:
351+
void onRenderData (const std::string_view data, std::string &buffer) {
352+
// In this case we are going to still use the internal buffer...
353+
buffer.append(data);
354+
if (buffer.size() >= chunkSize) {
355+
// ...and then stream it out every 256 characters:
356+
out << buffer;
357+
buffer.clear();
358+
}
359+
}
360+
361+
doc myDoc {
362+
head {},
363+
body {"Hello world"},
364+
};
365+
366+
// Start the render:
367+
std::string leftovers = render(myDoc, {
368+
nullptr, // We're not using a placeholder populator.
369+
onRenderData, // Provide our render output receiver.
370+
chunkSize // Preset the size of the internal buffer.
371+
});
372+
373+
// Some data might be left in the buffer - this is returned so we can stream that too:
374+
out << leftovers;
375+
```
376+
377+
You can also defer work until calling `render` by using `lazy`. However lazy blocks are still executed in a pass _before_ the first bytes are rendered.
378+
379+
```c++
380+
dv myDiv{
381+
// Evaluated now:
382+
h1{"Hello"},
383+
// Lazy block takes a function:
384+
lazy{[] () {
385+
// Only evaluated after render() is called below:
386+
return p{"I'm lazy"};
387+
}},
388+
};
389+
390+
render(myDiv); // <h1>Hello</h1><p>I'm lazy</p>
391+
```
392+
341393
## 🔥 Performance
342394
343395
Some basic [benchmarks](test/benchmark/benchmark.cpp) are built at `build/test/benchmark/webxx_benchmark` using [google-benchmark](https://github.com/google/benchmark.git). Webxx appears to be ~5-30x faster than using a template language like [inja](https://github.com/pantor/inja).

0 commit comments

Comments
 (0)