Adding HTML Templates into TinyMCE Editor
<p>This can be very useful if you are using the tinymce editor on your site and want to be able to add simple or complex html templates.</p>
<p><img src="https://i.imgur.com/Bq0L0bV.gif" /></p>
<p>Here is how to do it:</p>
<p>Add this to your tinymce init</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">tinymce.init({ <br /> / ... / plugins: "template", <br />});</code></pre>
<p>You can then add the option to access the templates either to the tinymce toolbar or as a dropdown menu.</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">tinymce.init({ <br /> / ... / menubar: "insert", <br /> toolbar: "template", <br />});</code></pre>
<p>Adding the template you can choose if its a string included in the init or if its more detailed you can link it to a file. </p>
<p>The template format is in:</p>
<ul>
<li>title</li>
<li>description</li>
<li>content or url</li>
</ul>
<h4>Javascript</h4>
<pre><code class="javascript hljs">tinymce.init({ <br /> / ... / <br /> templates: [ <br /> { <br /> title: "Some title 1", <br /> description: "Some desc 1", <br /> content: "<p>My content</p>", <br /> }, <br /> { <br /> title: "Some title 2", <br /> description: "Some desc 2", <br /> url: "templates/development.html", <br /> }, <br /> ], <br />});</code></pre>
<p>I usually have a block that i use to insert code for highlight js formatting, which is pretty basic, so ill just include it in the content in my init.</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">templates: [ <br /> { <br /> title: "HLJS HTML", <br /> description: "Some desc 1", <br /> content: "<p>My content</p>", <br /> },</code></pre>
<p>Current Function to load tinymce</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">function loadtinymce(idclass) {<br /> tinymce.init({<br /> selector: idclass,<br /> plugins: [ "spellchecker code autolink link image fullscreen searchreplace wordcount visualblocks visualchars insertdatetime media table paste textcolor textpattern emoticons media lists" ],<br /> toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link unlink image | code | forecolor backcolor | emoticons",<br /> imageadvtab: true,<br /> skin: "oxide-dark",<br /> contentcss: "dark",<br /> height: 400,<br /> / contentcss : "/css/style.css", /<br /> relativeurls: false,<br /> converturls: false,<br /> removescripthost : false<br /> });<br /> }</code></pre>
<p>Add a template for HLJS HTML block</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">function loadtinymce(idclass) {<br /> tinymce.init({<br /> selector: idclass,<br /> plugins: [ "spellchecker code autolink link image fullscreen searchreplace wordcount visualblocks visualchars insertdatetime media table paste textcolor textpattern emoticons media lists" ],<br /> toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link unlink image | code | forecolor backcolor | emoticons",<br /> imageadvtab: true,<br /> skin: "oxide-dark",<br /> contentcss: "dark",<br /> height: 400,<br /> / contentcss : "/css/style.css", /<br /> relativeurls: false,<br /> converturls: false,<br /> templates: [ <br /> { <br /> title: "Some title 1", <br /> description: "Some desc 1", <br /> content: "<p>My content</p>", <br /> }, <br /> { <br /> title: "Some title 2", <br /> description: "Some desc 2", <br /> url: "templates/development.html", <br /> }, <br /> ], <br /> removescripthost : false<br /> });<br /> }</code></pre>
<p>Here is the final function including the plugin and menu item</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">function loadtinymcetest(idclass) {<br /> tinymce.init({<br /> selector: idclass,<br /> plugins: [ "template spellchecker code autolink link image fullscreen searchreplace wordcount visualblocks visualchars insertdatetime media table paste textcolor textpattern emoticons media lists" ],<br /> toolbar1: "template | insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link unlink image | code | forecolor backcolor | emoticons",<br /> imageadvtab: true,<br /> skin: "oxide-dark",<br /> contentcss: "dark",<br /> height: 400,<br /> / contentcss : "/css/style.css", /<br /> relativeurls: false,<br /> converturls: false,<br /> templates: [<br /> {<br /> title: "HTML",<br /> description: "A HTML HLJS Code Block",<br /> content: "<pre><code class='html hljs xml'>HTML</code></pre>",<br /> / url: "templates/development.html", /<br /> },<br /> {<br /> title: "Some title 2",<br /> description: "Some desc 2",<br /> url: "templates/development.html",<br /> },<br /> ],<br /> removescript_host : false<br /> });<br /> }</code></pre>
<p>You can see this adds an additional menu item to the tinymce editor, and allows you to access the templates.</p>
<p>Here is the icon location for the templates:</p>
<p><img src="https://i.imgur.com/26fhsZN.png" /></p>
<p>Then it opens the template selector, with our new templates:</p>
<p><img src="https://i.imgur.com/qJ6FNeh.png" /></p>
<p>Pick your template, and select save. which will add this block into the editor.</p>
<p><img src="https://i.imgur.com/qHGb7UC.png" /></p>
<p>Paste your code in. I noticed sometimes while pasting in the code, it would delete some of the code block, so i had to add in some dots while pasting (might be a bug in tinymce). You can see in the demo below. </p>
<p><img src="https://i.imgur.com/g4ukKFi.gif" /></p>
<h4>HTML</h4>
<pre><code class="html hljs xml"><p><br />test<br /></p></code></pre>
<p>The End! Hopefully this helps you adding templates into your tinymce installation.</p>
<p>You can see the full working demo, scripts and code below.</p>
<p>Here is another one for Video Embed with autoplay and loop</p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">{<br /> title: "Video Embed",<br /> description: "A PHP HLJS Code Block",<br /> content: "<p><strong><video autoplay='autoplay' loop='loop' controls='controls' width='100%' height='600'><source src='https://i.imgur.com/ot1hc51.mp4' type='video/mp4' /></video></strong></p>",<br />},<br /></code></pre>
HTML
<h2>Demo TinyMCE with Templates</h2>
<textarea id='mytinymce'></textarea>Scripts
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/skins/ui/oxide/content.min.css" integrity="sha512-U8A/twP2qv8LP/I9jf9GOI6FioG4BhrB1yMkT6Z+5UI44Z5qhuVeR5Pnh4NATpPPmaPP34gm6GV1roiejydBYQ==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/skins/ui/oxide-dark/skin.min.css" integrity="sha512-r3IXhPGM2XA8Yqjv5SKfx/3FNWSaAtk6h1TbZh2Oz38Mp+xO1zRGMu2Ek/IF870FyMV2f1Gq5og5i3wYEC5W8w==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.6.2/tinymce.min.js" integrity="sha512-sOO7yng64iQzv/uLE8sCEhca7yet+D6vPGDEdXCqit1elBUAJD1jYIYqz0ov9HMd/k30e4UVFAovmSG92E995A==" crossorigin="anonymous"></script>Javascript
function load_tinymce_test(idclass) {
tinymce.init({
selector: idclass,
plugins: [ "template spellchecker code autolink link image fullscreen searchreplace wordcount visualblocks visualchars insertdatetime media table paste textcolor textpattern emoticons media lists" ],
toolbar1: "template | insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link unlink image | code | forecolor backcolor | emoticons",
image_advtab: true,
skin: "oxide-dark",
content_css: "dark",
height: 400,
/* content_css : "/css/style.css", */
relative_urls: false,
convert_urls: false,
templates: [
{
title: "HTML",
description: "A HTML HLJS Code Block",
content: "<h4>HTML</h4><pre><code class='html hljs xml'>HTML</code></pre>",
/* url: "templates/development.html", */
},
{
title: "CSS",
description: "A CSS HLJS Code Block",
content: "<h4>CSS</h4><pre><code class='css hljs'>.css { value: 1px; }</code></pre>",
},
{
title: "JS",
description: "A JS HLJS Code Block",
content: "<h4>Javascript</h4><pre><code class='javascript hljs'>function test() { console.log('test'); }</code></pre>",
},
{
title: "PHP",
description: "A PHP HLJS Code Block",
content: "<h4>PHP</h4><pre><code class='php hljs'>a = 1;</code></pre>",
},
],
remove_script_host : false
});
}
$(document).ready(function(){
load_tinymce_test("#mytinymce");
});