Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro in Liquid Template language

I'm using Jekyll, which uses the Liquid Template language. I've used Jinja templating in the past, and it has the concept of a macro (just a named function). Does Liquid have something which provides equivalent functionality? If not, is there some Jekyll plugin which will extend Liquid to provide it?

like image 356
Ellis Michael Avatar asked May 29 '15 06:05

Ellis Michael


People also ask

What is Liquid templating language?

Liquid is a template language that allows us to display data in a template. Liquid has constructs such as output, logic, loops and deals with variables. Liquid files are a mixture of HTML and Liquid code, and have the . liquid file extension.

What language does Jekyll use?

Jekyll uses the Liquid templating language to process templates. Generally in Liquid you output content using two curly braces e.g. {{ variable }} and perform logic statements by surrounding them in a curly brace percentage sign e.g. {% if statement %} .

What is Liquid syntax?

Liquid syntax is a template language used in many web applications. In Current RMS, use Liquid syntax to place dynamic content and modify data in document layouts and discussion templates. In Liquid, there are three types of code: objects, tags, and filters.

What is Jekyll and Liquid?

Jekyll is a static site generator, a command-line tool that creates websites by merging templates with content files. Jekyll uses Liquid as its template language, and adds a few objects, tags, and filters.


1 Answers

You can create includes that accept parameters. It's not quite a macro but it's what I've used successfully on GitHub Pages.

More details and tips for managing includes and using parameters can be found in the Jekyll documentation.

Here's an example:

_includes/email_link.html

<a href="mailto:{{ include.user.email_address }}"
   title="Email {{ include.user.name }}">
    <i class="fa fa-fw fa-envelope"></i>
</a>

about.md

---
layout: page
title: About
---
{% include email_link.html user=site.users.erik %}

_config.yml

users:
    erik:
        name: Erik
        email_address: [email protected]
like image 59
Erik Gillespie Avatar answered Nov 13 '22 09:11

Erik Gillespie