2011-07-22 17:49:01 -04:00
---
layout: page
title: "Include Code"
date: 2011-07-22 09:13
2011-08-21 16:20:10 -04:00
updated: 201-08-21 16:18
2011-07-22 17:49:01 -04:00
sidebar: false
footer: false
---
Import files on your filesystem into any blog post as embedded code snippets with syntax highlighting and a download link.
In the `_config.yml` you can set your `code_dir` but the default is `source/downloads/code` . Simply put a file anywhere under that directory and
use the following tag to embed it in a post.
2012-06-03 01:24:03 -04:00
## Syntax
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
{{ "{% include_code [title] [lang:language] path/to/file [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false]" }} %}
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
### Basic options
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
- `[title]` - Add a custom figcaption to your code block (defaults to filename).
- `lang:language` - Force the syntax highlighter to use this language. By default the file extension is used for highlighing, but not all extensions are known by Pygments.
{% assign show-range = true %}
{% render_partial docs/plugins/_partials/options.markdown %}
## Examples
**1.** This code snipped was included from `source/downloads/code/test.js` .
2011-08-21 16:20:10 -04:00
{% include_code test.js %}
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
*The source:*
2011-08-21 16:20:10 -04:00
2012-06-03 01:24:03 -04:00
{{ "{% include_code test.js" }} %}
2011-08-21 16:20:10 -04:00
2012-06-03 01:24:03 -04:00
**2.** Setting a custom caption.
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
{% include_code ruby/test.rb Add to_fraction for floats %}
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
*The source:*
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
{{ "{% ruby/test.rb include_code Add to_fraction for floats" }} %}
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
This includes a file from `source/downloads/code/ruby/test.rb` .
### Including part of a file
**3.** Embed a file starting from a specific line.
2012-05-27 03:04:45 -04:00
{% include_code test.js start:10 %}
2012-06-03 01:24:03 -04:00
*The source:*
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
{% raw %}{% include_code test.js start:10 %}{% endraw %}
**4.** Embed a file ending at a specific line.
2012-05-27 03:04:45 -04:00
{% include_code test.js end:10 %}
2012-06-03 01:24:03 -04:00
*The source:*
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
{% raw %}{% include_code test.js end:10 %}{% endraw %}
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
**5.** Display only the lines in a specific range.
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
{% include_code test.js range:5-16 %}
2011-08-21 16:31:10 -04:00
2012-06-03 01:24:03 -04:00
*The source:*
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
{% raw %}{% include_code test.js range:5-16 %}{% endraw %}
2011-07-22 17:49:01 -04:00
2012-06-03 01:24:03 -04:00
### Other ways to embed code snippets
2012-05-27 03:04:45 -04:00
2012-06-03 01:24:03 -04:00
You might also like to [use back tick code blocks ](/docs/plugins/backtick-codeblock ) or [embed GitHub gists ](/docs/plugins/gist-tag ).