Usefull twig function

Inside your templates (.html.twig files) and / or your documents (.md files) you can use several twig globals / functions.

Globals#

You have access to the carew global. This is an instance of Carew/Twig/Globals class. This one contains few things:

Some examples:

{{ carew.site.decription }}

{# read the next chapter, there is a much better way to do that #}


{# read the next chapter, there is a much better way to do that #}
Logo

Functions#

Note: You can tweak generated content for almost all functions by overriding the blocks.html.twig template. See the theme chapter for more information.

path#

path: Render a path to a document

{{ path('pages/index.md') }}

Render something like: index.html

Arguments:

  1. filePath: the full path to the document. i.e.: where the file is located on your disk.

If you want to change the generated content, override the document_path block.

link: Render a link to a document

{{ link('pages/index.md', 'a link') }}

Render something like: a link

Arguments:

  1. filePath: the full path to the document. i.e.: where the file is located on your disk.

  2. title (optional): The link title. Default value is the document title.

  3. attrs (optional): An array of HTML attributes.

If you want to change the generated content, override the document_link block.

render_document_toc#

render_document_toc: Render the Table Of Content of a document

{{ render_document_toc() }}

Render something like:

Arguments:

  1. toc (optional): A document instance or a TOC. The default value is the current document.

If you want to change the generated content, override the document_toc block.

render_document_*#

render_document_*: This is a wild-card function. It will take the * and render the document_* block.

Arguments:

  1. document (optional): A document instance. The default value is the current document.

If you want to change the generated content, override the document_* block.

Waning: The block document_* must be defined.

render_document#

render_document: Renders a document.

{{ render_document(carew.document) }}

Generates something like:

Document title

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, harum nam facilis modi eos est ipsa nostrum recusandae assumenda molestiae nemo omnis animi? Quia maiores fuga quam necessitatibus quaerat cum.

Arguments:

  1. document: A document instance.

This function will call one of the following block depending of the document type: post, page, api, unknown.

If you want to change the generated content, override one of theses blocks.

render_documents#

render_documents: Renders a collection of document.

{{ render_document(carew.document) }}

Generates something like:

Arguments:

  1. documents: A collection (array) of document instances.

If you want to change the generated content, override the documents block.

paginate#

paginate: Adds pagination support to a collection of documents.

warning: This function can only be applied in a file.md, not in a template.

warning: This function returns an array. It should be used with the render_documents function.

{{ render_documents(paginate(carew.posts)) }}

This function will add a pagination on the collection. So it will create as many page as needed.

Arguments:

  1. documents: A collection (array) of document instances.

  2. maxPerPage: Number of items per page.

If you want to change the generated content, override the pagination block.