If you just want to play around with Marko in the browser, head on over to our Try Online feature. You'll be able to develop a Marko application right in your browser.
If you're starting from scratch,
marko-cli provides a starter app to get you going quickly. To get started:
npm install marko-cli --globalmarko create hello-worldcd hello-worldnpm start
npm install marko
or using yarn:
yarn add marko
Let's say we have a simple view that we want to render in the browser:
h1 -- Hello
First, let's create a
client.js that requires the view and renders it to the body:
var helloComponent = ;helloComponent;
We will also create a barebones HTML page to host our application:
<!doctype html><html><head><title>Marko Example</title></head><body></body></html>
Now, we need to bundle these files for use in the browser. We can use a tool called
lasso to do that for us, so let's get it (and the marko plugin) installed:
npm install --global lasso-clinpm install --save lasso lasso-marko
Now we can build our bundle for the browser:
lasso --main client.js --plugins lasso-marko --inject-into index.html
This builds a
client.js file to the newly created
static/ directory and injects the required
<script> tags into our HTML page to load our application in the browser. If we had css in the view then
<link> tags would have also been added.
Load up that page in your browser and you should see
Hello Marko staring back at you.
Marko provides a custom Node.js require extension that allows you to
div -- Hello !
// The following line installs the Node.js require extension// for `.marko` files. This should be called once near the start// of your application before requiring any `*.marko` files.;var fs = ;// Load a Marko view by requiring a .marko file:var hello = ;var out = fs;hello;
Using the Node.js require extension is completely optional. If you prefer to not use the Node.js require extension then you will need to precompile all of the marko templates using Marko CLI:
marko compile hello.marko
This will produce a
hello.marko.js file next to the original template. The generated
.js file will be what gets loaded by the Node.js runtime. It is important to leave off the
.marko extension when requiring a Marko template so that the
.js will be resolved correctly.
If you wish to only use the require extension in development, you can conditionally require it.
server.js to serve the view from an http server:
// Allow requiring `.marko` files;var http = ;var hello = ;var port = 8080;http;
hello.marko some content:
h1 -- Hello
Start the server (
node server.js) and open your browser to http://localhost:8080 where you should see the heading
Marko automatically injects a list of components that need to be mounted in the browser, right before the closing
</body> tag (as such, it required that you include a
<body> in your rendered output).
script tags. Luckily, the
lasso taglib will do all the heavy lifting for you.
npm install --save lasso lasso-marko @lasso/marko-taglib
Next, in your page or layout view, add the
htmlheadtitle -- Hello worldlasso-headbody</>lasso-body
Finally, configure your server to serve the static files that
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.