Would you like to display code on your Piranha website like the examples below? With Prism.js it’s easy. This guide will help you build a custom block which does just that.
Add Prism.JS to your project
First thing we need to do is to include the requires Javascript and CSS files to our project. Go to Prismjs.com. Under the section “Basic usage” you’ll find different ways to include the required files into your project. You can either download them or preferably use a CDN to reduce server load.
Make sure you get the following files, either one by one or as a bundle:
- prism.min.css
- prism-line-numbers.min.css
- prism.min.js
- prism-autoloader.min.js
- prism-line-numbers.min.js
<!-- Note that this is just an example and the files might be outdated -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.css" />
<!-- Note that this is just an example and the files might be outdated -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
Create an Enum to handle different Code languages
Now when you have the latest Javascript and CSS files for Prism.Js we're ready to start building the components for your new block. We want to have the possibility to use multiple code languages and not just one. Therefore we need to create an Enum. An enum can be used to populate a SelectField with options in Piranha. In this guide we named the enum CodeType and the file CodeType.cs
You can preferably put the Enum in a separate folder called Enums
public enum CodeType
{
Bash,
CSS,
Dotnet,
Html,
Javascript,
Python
}
Adding the block model
We are now ready to start building a model for our block. Create a new class and name it CodeBlock. Make sure it inherits from the class Block. It’s important that all the field properties have the Data annotation [Field] so Piranha understands what to do with the properties
[BlockType(Name = "Code", Category = "Special", Icon = "fas fa-code")]
public class CodeBlock : Block
{
[Field(Title = "Code title")]
public StringField CodeTitle { get; set; }
[Field(Title = "Code type", Options = FieldOption.HalfWidth)]
public SelectField<CodeType> CodeType { get; set; }
[Field(Title = "Show line numbers", Options = FieldOption.HalfWidth)]
public CheckBoxField ShowLineNumbers { get; set; }
[Field]
public TextField Code { get; set; }
}
Creating a new view
Our block needs a view. Create a separate folder for your block views if you don’t have one already. Create a new view with the same name you had for your model. In this case the file will be called CodeBlock.cshtml
At the top of the file we’ll add our model. Make sure you have the right namespace which might differ from this example.
@model MvcWeb.Models.Blocks.CodeBlock //Replace this with the actual block namespace
@{
string codeClass = $"language-{Model.CodeType.Value.ToString().ToLower()}";
}
<div class="block code-block">
<pre class="@(Model.ShowLineNumbers.Value ? "line-numbers" : "")"><code class="@codeClass">@Model.Code.Value</code></pre>
</div>
Register your new block
The last step is to register your new block in the Configure() method in Startup.cs. If the block is not registered there then it won’t show up in the manager.
Piranha.App.Blocks.Register<CodeBlock>();