question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using variable in a macro

See original GitHub issue

Hi,

I want to use a macro in a layout, that renders a link list. I then want to pass a variable to that macro in every template that extends that layout, in order to add a class of “active” to one of the list links.

To do this, I have a nunjucks file with this code, which extends a layout and sets the variable:

{% extends "layout.nunjucks" %}
{% set activePage = "actualitat" %}

I’d like to use the value of the variable activeMenu in my layout.nunjucks file, where there’s this code, which doesn’t work:

{% import 'macros/navigation.nunjucks' as nav %}
{{nav.active({{ activePage }})}}

The macro I’m importing is:

{% macro active(activePage='home') %}
  <ul class="menu">
    <li><a href="/" class="{%if activePage == 'home' %}active{% endif %}">Home</a</li>
    <li><a href="/actualitat/" class="{%if activePage == 'actualitat' %}active{% endif %}">Actualitat</a></li>
    <li><a href="/estils/" class="{%if activePage == 'estils' %}active{% endif %}">Estils</a></li>
    <li><a href="/contacte/" class="{%if activePage == 'contacte' %}active{% endif %}">Contacte</a></li>
</ul>
{% endmacro %}

How should I pass the value of the variable to the macro, so I have my link list with the class “active” added to the link I want?

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
devoidfurycommented, Apr 11, 2018
environment.addGlobal('includeFile', (src) => fs.readFileSync('' + src));
0reactions
CSFlorincommented, Apr 11, 2018

Just added the includeFile function/macro at the top of my comment. I’m not exactly sure how to do that in the template; can you give an example?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variables in Excel VBA (Easy Macros)
This chapter teaches you how to declare, initialize and display a variable in Excel VBA. Letting Excel VBA know you are using a...
Read more >
Define Variables In VBA: Declare Variables And Assign Them ...
Detailed tutorial about defining variables in Excel VBA. Learn now how to declare variables, determine their scope and lifetime, and assign values to...
Read more >
Variables in Macros VBA for Excel - Practical Guide
A variable is a name that you type into a Macro that you can use to store information, be it text or numbers....
Read more >
Pass VBA Variables Between Macros - Excel Dashboards VBA
In Excel you can pass variables (an argument) between macros at run time. This allows you to have multiple modules and pass the...
Read more >
AN INTRODUCTION TO MACRO VARIABLES ... - OARC Stats
changing three lines of code, is not necessary if you use a macro variable as a shortcut to represent the data set name......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found