Twig extensions for PieCrust
PieCrust adds a few functions to Twig by default. They greatly help with authoring a website.
URL functions
You can easily insert the URL to a page without having to think about whether
you have pretty URLs enabled or not, or if it’s for a baked website or
whatever. That’s done with the pcurl function:
{{ pcurl('my/page' }}
Similarly to the pcurl function, the pctagurl and pccaturl functions will
generate URLs to a tag or category listing page with respect to the site
configuration:
{{ pctagurl('sometag' }}
{{ pccaturl('category' }}
Last but not least, the pcposturl function generates the URL to a post given a
year, month, day and name. It will take into account the posts file-system
when generating the URL:
{{ pcposturl(2011, 11, 21, 'some-post') }}
Those functions will update their output automatically if you change the posts, tags or categories URL formats.
Word count function
The wordcount(text) function counts the number of words in the given text.
Nocache filter
This filter adds an URL query parameter of the form ?t=12345 at the end of the
provided text. This is useful to prevent caching on updated resources like CSS
sheets:
{{ pcurl('css/mobile.css')|nocache }}
By default, it will use a parameter t and the current timestamp. You can
override that behaviour by specifying them directory:
{{ pcurl('css/mobile.css')|nocache('v', 5) }}
Formatting filters
You can format a small amount of text by applying one of the formatting filters:
{{ post.title|markdown }}
Specific filters are availebl:
markodwnwill run Markdown on the text.textilewill run Textile one the text.formatwith(blah)will run the formatter whose name isblah.
Syntax highlighting
If you’re inserting source code into a page, you can make it pretty by using syntax highlighting on it. PieCrust uses GeSHi for that, and it should support most of the languages you’re likely to need.
In Twig, you can use the geshi tag, followed by the language name, to
specify a portion of text to process. For example, here’s how you can add some
PHP code:
{% geshi 'php' %}
include 'foo.php';
function bar() { echo 'Bar'; }
{% endgeshi %}
Tag stripping
The striptag filter will strip any HTML tags found at the beginning or end of the given string.
For example, this will just print wrapped in plastic (without the <p> tags):
{{ "<p>wrapped in plastic</p>"|striptag }}
You can specify a tag to strip, if only that tag must be removed:
{{ "<p>wrapped in plastic</p>"|striptag('p') }}
External text import
The textfrom function will output text from an external file:
{{ textfrom("~/Dropbox/Drafts/my-fancy-post.txt")|raw }}
This is useful when, for example, you want to preview a post draft currently located somewhere else, like a Dropbox folder.