Live Sample Advice Needed

Hello, I have begun contributing code samples to the MDN where necessary and have noticed the use of the Live Sample feature on some pages. I found this feature fascinating and would love to implement it on my own samples.

I have read the guide for it however when I try to implement it, it gives me a whole newly formatted layout to insert code and information instead of working along with my already created sample code. I have already written and formatted the text and sample match the rest of the article so this doesn’t work for me, is it possible to add live samples around a code sample that has already been implemented?

Thanks in advance.

Hi! The short answer to your question is “yes”. It sounds like you’re using the Insert Live Sample Code Template button in the editor UI. You can also insert a live sample macro by typing it manually:

{{EmbedLiveSample(block_ID, width, height, screenshot_URL, page_slug)}}

The “block_ID” parameter can be the ID of any enclosing block or heading. For example, if the existing code sample is under a heading “Foo Example”, then you can use “Foo_Example” as the block_ID parameter. (On MDN, a heading element’s “title” value is automatically used as the “id” attribute, with spaces converted to underscores.)

The other parameters are optional, and use default values if you don’t specify them.

That was the short answer. The long answer is that we actually have multiple ways of displaying code samples. In addition to “static” and “live” examples, we also can have “live” examples that are stored on Github (and therefore can be more easily reused in multiple places), and interactive examples that readers can actually modify and play with. Interactive examples are currently only possible on CSS and JavaScript reference pages; they also are stored on Github. The various types of code examples are all described in excruciating detail in the Code Examples article. However, if you just want to convert some existing static examples to be “live”, you can ignore the Github-based example types.

2 Likes

Thank you for the extremely helpful reply, I’m impressed that there are so many different ways to implement a live sample! All of your information helped me understand a lot more where I was going wrong and what I can do instead, I will do further research into it so I can find the right way to implement for me.