Skip to main content

Code Prefill

There are many ways to pre-fill code into playgrounds. This is generally achieved either by the SDK or using query params.

Prefill using SDK​

When creating an embeded playground, the following embed options allow prefill with code:

config​

EmbedOptions.config

loads a configuration object (or a URL to JSON file representing the configuration object)

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"markup": {
"language": "html",
"content": "<h1>Hello World!</h1>"
},
"style": {
"language": "css",
"content": "h1 { color: blue; }"
}
}
};
createPlayground('#container', options);

import​

EmbedOptions.import

allows importing from many sources.

Examples:

Import GitHub directory:

show code
import { createPlayground } from 'livecodes';

const options = {
"import": "https://github.com/bradtraversy/50projects50days/tree/master/progress-steps"
};
createPlayground('#container', options);

Import shared project:

show code
import { createPlayground } from 'livecodes';

const options = {
"import": "id/6ys2b8txf33"
};
createPlayground('#container', options);

template​

EmbedOptions.template

loads one of the starter templates.

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "react"
};
createPlayground('#container', options);

Prefill using query params​

Query parameters can provide easy and powerful ways for configuring the playground.

Example:

https://livecodes.io/?md=**Hello World!**

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"md": "**Hello World!**"
}
};
createPlayground('#container', options);

Auto-Prefill from page DOM​

TODO...