Skip to content

Instantly share code, notes, and snippets.

@paulmsmith
Created November 12, 2016 15:44
Show Gist options
  • Save paulmsmith/db77cd0debbd29f3ca3eb06608956478 to your computer and use it in GitHub Desktop.
Save paulmsmith/db77cd0debbd29f3ca3eb06608956478 to your computer and use it in GitHub Desktop.
call explanation nunjucks/jinja
{# The field macro which accepts the configuration object and uses it #}
{% macro field(config) %}
<div class="field-row field-row--{{ config.type | d('text') }}{{ ' field-row--' + config.size if config.size }}">
{# the caller is then invoked and the config is passed back #}
{% if caller %}{{ caller(config) }}{% else %}<p>Default HTML here</p>{% endif %}
</div>
{% endmacro %}
{# calling the 'field' macro and passing in a configuration object (could be a JSON schema) #}
{%- call(config) field({ id: 'theID', type: 'password', size: 'small' }) %}
{# as the caller we now have access here to the configuration object we originally passed in to the macro we called #}
<input type="{{ config.type }}" name="{{ config.id }}" value="{{ config.value }}" id="{{ config.id }}">
{%- endcall %}
{# this would be the output, this is a simple example, the macro being called
could mutate the object that was passed and return that if required #}
<div class="field-row field-row--password field-row--small">
<input type="password" name="theID" value="" id="theID">
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment