Nunjucks: [Question] Is it possible to pass multiline content / markup to nunjucks macros?

Created on 12 Sep 2018  路  2Comments  路  Source: mozilla/nunjucks

Wondering whether it's possible to pass content like:

  <div class="TestHeader ">
    <span class="TestCounter"></span>
    <span class="TestComponent">TestBoxLeft</span>
  </div>

To a nunjucks macro? So if the above content could be inserted into a variable called content the macro could be called like this:

{% import "insert.njk" as insert %}

{{ insert.content( content ) }}

Most helpful comment

{% macro content() %}
foo {{ caller() }} bar
{% endmacro %}

{% call content() %}
  <div class="TestHeader ">
    <span class="TestCounter"></span>
    <span class="TestComponent">TestBoxLeft</span>
  </div>
{% endcall %}

https://mozilla.github.io/nunjucks/templating.html#call

All 2 comments

{% macro content() %}
foo {{ caller() }} bar
{% endmacro %}

{% call content() %}
  <div class="TestHeader ">
    <span class="TestCounter"></span>
    <span class="TestComponent">TestBoxLeft</span>
  </div>
{% endcall %}

https://mozilla.github.io/nunjucks/templating.html#call

@devoidfury THAT'S VERY COOL - Thank you! I played around with a mini prototype that can be found here in case anyone else needs to do something similar. In my case I'm planning on rearchitecting superflycss/cli to generate tests like these using the nunjucks macros. Right now I'm using JSDOM to copy the markup content to another section so that the markup example section can be syntax highlighted, and now it looks like I can cut out the JSDOM manipulation and use pure nunjucks. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

popomore picture popomore  路  6Comments

mattbasta picture mattbasta  路  6Comments

atian25 picture atian25  路  5Comments

boutell picture boutell  路  6Comments

dgirgenti picture dgirgenti  路  3Comments