PHP Blocks in Jekyll
I use a lot of code blocks in my writing so I wanted to look at my options for how to format them now that I’m using Jekyll. Wordpress had me install extra plugins to get syntax highlighting.
This is the code block I’m using as a test. It includes a class, a property, and a method so I’m hoping it will give me a good cross section of my options.
<?php
class User extends Model
{
protected $variable = null;
public function getVariable()
{
return $this->variable;
}
}
Four Spaces
Markdown (which I’m a huge supporter of) allows you to use four spaces at the beginning of a line to indicate a code block.
Input
See above.
Output
<?php
class User extends Model
{
protected $variable = null;
public function getVariable()
{
return $this->variable;
}
}
This option is the easiest but it doesn’t provide any syntax highlighting.
pre and code
Input
Output
I’m not showing the example of this because it messed up the rest of my page layout but the end result is what occurs in the Four Spaces section above (because that’s how Markdown converts the four spaces). This option also doesn’t provide syntax highlighting.
Using Highlight Blocks
Jekyll support using highlight
blocks which get passed into something that syntax highlights the code. I’m super happy with the results.
Input
{% highlight php %}
[code here]
{% endhighlight %}
Output
startinline option
One of the things I don’t like about using the highlight option is that in order to get my code to syntax highlight correctly I need to start it with <?php
. It wouldn’t be a huge problem but Sublime Text assumes everything after that start PHP code is PHP and messes up syntax highlighting while I’m writing.
If I add startinline
to the highlight start it removes this requirement.
Input
{% highlight php startinline %}
[code here (minus the <?php)
{% endhighlight %}
Output
linenos
Another option I ran accross is the option to tell Jekyll to display line numbers. The downside to this is that it’s hard to copy and paste the text from here. However if you look at the “linenos with Tables” section theres a better option.
Input
{% highlight php startinline linenos%}
[code here]
{% endhighlight %}
Output
1
2
3
4
5
6
7
8
9
class User extends Model
{
protected $variable = null;
public function getVariable()
{
return $this->variable;
}
}
linenos with Tables
Input
{% highlight php startinline linenos=table %}
[code here]
{% endhighlight %}
Output
1
2
3
4
5
6
7
8
9
class User extends Model
{
protected $variable = null;
public function getVariable()
{
return $this->variable;
}
}
Overall
Overall, I think {% highlight php startinline %} is the best option with {% highlight php startinline linenos=table%} a close second when you need to refer to a specific line.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows