Capture like include


#1

I typeset the same “wrapping html” over and over. For example:

<div class="warning">
    <strong>Warning:</strong> Be careful!
</div>

I would like to write something like

{% warning %}
    Be careful!
{% endwarning %}

What is the best way to achieve this? Preferably I’d like a combined capture / include, so I could write

{% include warning.html %}
    Be careful!
{% endinclude %}

Is there anything like this available out of the box? What would the best way be to implement it?


#2

Out of the box you can use Jekyll includes with parameters depending on the level of sophistication you’re aftert.

Simple example would be something like:

_includes/warning.html

<div class="warning">		
  <strong>Warning:</strong> {{ include.message}}		
</div>

You’d drop it into your pages with:

{% include warning.html message="Be careful" %}

Which would render into:

<div class="warning">
  <strong>Warning:</strong> Be careful!
</div>

If you want to use custom tags like {% warning %}Be careful{% endwarning %}, then that’s venturing into plugin territory.


#3

What @mmistakes has suggested is the easiest way (and customizable way) to accomplish what you’re looking for.

However, if for some reason include doesn’t fit, then what you’re looking for is a custom Liquid tag and as Michael already noted, read more about it in Jekyll docs.

Shouldn’t be that difficult to accomplish but do requires some code to be written.

Something like this should do the trick: