Marko allows any Marko template/UI component to be rendered on the server or in the browser. A page can be rendered to a Writable
stream such as an HTTP response stream as shown below:
var template = ; // Import ./template.markomodule {res;template;};
Marko can also provide you with a Readable
stream.
var template = ; // Import ./template.markomodule {// Return a Readable stream for someone to do something with:return template;};
ProTip: Marko also provides server-side framework integrations:
When a page is rendered on the server, additional code is added to the output HTML to allow the UI to instantly boot in the browser. This additional code allows UI components rendered on the server to be mounted in the browser automatically. For each top-level UI component, Marko will serialize the component's data (including input
and state
and any properties added to the UI component instance) so that each top-level UI component can be re-rendered and mounted when the page loads in the browser. Only a "partial" re-render is done for each top-level UI component. That is, when doing the partial re-render in the browser, the DOM is not updated and no virtual DOM is actually produced.
Marko encodes required information into attributes of rendered HTML elements and it also generates <script>
tags that will cause UI components to be mounted. The code inside the <script>
simply registers UI components and when the Marko runtime finally loads, all of the registered UI components will then be mounted. This allows the Marko runtime to be loaded at anytime without causing JavaScript errors.
When a server-rendered page loads in the browser it's possible for marko to automatically detect UI components rendered on the server and create and mount them with the correct state
and input
in the browser.
If you are using Lasso.js then the bootstrapping will happen automatically as long as the JavaScript bundles for your page are included via the <lasso-body>
tag. A typical HTML page structure will be the following:
<html lang="en"><head><meta charset="UTF-8"><title>Marko + Lasso</title><!-- CSS includes --><lasso-head/></head><body><!-- Top-level UI component: --><app/><!-- JS includes --><lasso-body/></body></html>
html lang="en"headmeta charset="UTF-8"title -- Marko + Lasso// CSS includeslasso-headbody// Top-level UI component:app// JS includeslasso-body
ProTip: We have provided some sample apps to help you get started with Marko + Lasso
If a JavaScript module bundler other than Lasso is being used then you will need to add some client-side code to bootstrap your application in the browser by doing the following:
require('marko/components').init()
For example, if client.js
is the entry point for your client-side application:
// Load the top-level UI component:;// Now that all of the JavaScript modules for the UI component have been// loaded and registered we can tell marko to bootstrap/initialize the app// Initialize and mount all of the server-rendered UI components:;
ProTip: We have provided some sample apps to help you get started:
For each top-level UI component, Marko will serialize the component's data (including input
and state
and any properties added to the UI component instance) down to the browser. You can control which data gets serialized by implementing toJSON
or by reassigning this.input
in the UI component's onInput(input, out)
lifecycle method as shown below:
class{// Do not serialize any input:thisinput = null;// Serialize a new object instead of the provided input:thisinput =foo: 'bar';}
NOTE: Marko does allow cycles in serialized objects and Duplicate objects will only be serialized once
There are some caveats associated with rendering a page on the server:
Date
objects are serializableout.global
is serialized by default, but this can be changed as shown belowIf there are specific properties on the out.global
object that need to be serialized then they must be whitelisted when the top-level page is rendered on the server. For example, to have the out.global.apiKey
and the out.global.locale
properties serialized you would do the following:
EDITtemplate;
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.