Enabling Syntax Highlighting in SilverStripe 4

Enabling Syntax Highlighting in SilverStripe 4

Enabling Syntax Highlighting in SilverStripe 4

Enabling Syntax Highlighting in SilverStripe 4

Updated 2 years ago
3 Minute(s) to read
Posted 5 years ago Updated 2 years ago 3 Minute(s) to read 25 comments

SilverStripe's backend HTML WYSIWYG editor does not come out-of-the-box with support for inserting code snippets or samples with syntax highlighting enabled. Fortunately TinyMCE, the WYSIWYG editor that SilverStripe uses, comes with support for inserting code snippets that make use of PrismJS. In order to activate it as part of your editor, simply go the PHP path under your SilverStripe installation

Open up the configuration file for your SilverStripe installation.

<path to silverstripe>/app/_config.php

Insert the following snippet.


use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;

TinyMCEConfig::get('cms')->addButtonsToLine(2, 'codesample');

You should then find this button as part of your SilverStripe installation's administrative panel editor for all content (see highlighted in red below).

This button should appear in the toolbar for TinyMCE's editor providing that you did everything correctly.

This button should appear in the toolbar for TinyMCE's editor providing that you did everything correctly.


There are a couple of requirements before code samples will appear correctly in your templates or theme.


The TinyMCE codesample plugin makes use of PrismJS for syntax highlighting, and that can be found here.

You will require

  • prism.js
  • prism.css

Once you have downloaded the required files, you can then bootstrap them as part of your template through one of two methods.


Update your root layout template to look something like this in the <head></head>.


<% require themedJavascript('prism/prism.min.js') %>
<% require themedCSS('prismjs/prism.css') %>


Page Controller

Alternatively, if you would prefer to only load these requirements with the page types that will use these asset dependencies, you can also modify the controller for your page type with the code snippet below.



The above snippet should generally be included in the code where the page get's initialized or loaded.

SilverStripe SiteTree

Next, you are going to want to ensure that the PrismJS JavaScript and CSS file are loaded with the PageController on each refresh. Refer to this configuration file snippet below.

Refer to the SilverStripe documentation here about other methods of specifying assets that are required to be loaded by the page.

Known Issues

There is one known issue with the current implementation of the "codesample" plugin for TinyMCE. SilverStripe seemingly removes the enclosing <code></code> from generated code samples on submission of new content, meaning that code will be improperly highlighted.

There is a slightly hacky work around to this, and it's to simply make use of jQuery to encapsulate all <pre> blocks that specify the "language-*" CSS class.

Refer to the JavaScript below.

            Look at this fucking atrocious hackjob. I had to shim this in because
            tinyMCE likes to strip the <code> tags from my code samples on db insert.

        document.addEventListener("DOMContentLoaded", function(event){
            $('pre').each(function(idx, elem) { $(elem).wrapInner('<code></code>'); });

Simply place this in the <head></head> of your template, and it should fix the problem. This JavaScript code runs once the page's DOM has loaded, and will iterate all <pre> DOM elements in the page that ensure their contents are encapsulated by the <code> tag.

It's currently what I use for fixing the problem with this instance of SilverStripe.

Programming Languages:

PHP JavaScript



You've made your position very clearly!.
<a href=>college paper writing service</a> order custom paper <a href=>college paper writing service</a> pay for college papers

Great content, Regards.
<a href="">paper writing services</a> buy a paper <a href="">pay for research paper</a> best college paper writing service

Thanks a lot! I appreciate it.
<a href="">thesis creator</a> argumentative thesis statement <a href="">thesis writing</a> tentative thesis
<a href=>writing a college application essay</a> mba essay writing service <a href=>writing a good essay</a> essay writing service oxford
essay writing skill

Thank you! I value it.
<a href=>urgent essay writing service</a> fast essay writing service <a href=>online check writing service</a> spongebob writing essay

Wow a good deal of wonderful data.
<a href="">writing a dissertation</a> dissertation writers <a href="">writing help</a> dissertation writing services reviews

Cheers! Excellent stuff.
<a href=>hire someone to do my homework</a> do my homework <a href=>xyz homework</a> xyz homework

Amazing stuff. Thanks.
<a href="">order essay</a> pay to write essay <a href="">pay for essay papers</a> pay someone to write your paper
<a href=>persuasive essay writer</a> write my essay online <a href=>essay writer review</a> ai essay writer
methodology dissertation

Thanks! I appreciate this!
<a href="">graduate paper writing service</a> pay for college papers <a href="">pay someone to write paper</a> pay someone to write a paper

Cheers, Quite a lot of forum posts.
<a href="">seo article writing service</a> paper writing service college <a href="">unique essay writing service</a> online paper writing service

You expressed that effectively.
<a href=>my phd</a> dissertation assistance <a href=>writing dissertation service</a> proposal dissertation