apostrophecms / apostrophe

A full-featured, open-source content management framework built with Node.js that empowers organizations by combining in-context editing and headless architecture in a full-stack JS environment.
https://apostrophecms.com
Other
4.36k stars 590 forks source link

3.0: nunjucks helpers not found inside of a macro #2692

Closed myovchev closed 3 years ago

myovchev commented 3 years ago

To Reproduce

git clone git@github.com:myovchev/a3-helpers.git && cd $_

Detailed instructions and info are available in the README. I'm providing a copy here as well:

  1. Be sure you have running mongodb on default port. Database a3-helpers will be auto-created.
  2. Run the project in dev mode (npm i && npm run dev). Ignore warnings, no need of user/session secret.
  3. You should see home page rendered with additional Helper Test section.
  4. Open modules/@apostrophecms/home-page/views/page.html and uncoment around line 23 <p>{{ helper.render("Same helper called from inside of a macro will fail") }}</p>
  5. You should see error page, look at the CLI output.

Changes made to the original a3 boilerprate to reproduce the issue:

Expected behavior

Execute the module nunjucks helper without an error.

Describe the bug

Registered via a3 module helpers are not available inside of any nunjucks macro.

Details

An example call apos.moduleName.someHelper() raises error:
Error: Unable to callapos["moduleName"]["someHelper"], which is undefined or falsey

Version of Node.js: v12.8.1

Apostrophe CMS version 3.0.0-alpha.3

Server Operating System:

 > lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

No docker involved. Node env provided by nvm.

boutell commented 3 years ago

(I had to use an https URL to check it out but then I got it.)

This works if you write:

{% import "helper.macro.html" as helper with context %}

However "apos" should probably be injected via "addGlobal" rather than as a template context variable.

Not sure whether this should be the case for "data".

On Wed, Jan 20, 2021 at 5:38 AM Miro Yovchev notifications@github.com wrote:

To Reproduce

git clone git@github.com:myovchev/a3-helpers.git && cd $_

Detailed instructions are available in the README https://github.com/myovchev/a3-helpers. I'm providing a copy here as well:

  1. Be sure you have running mongodb on default port. Database a3-helpers will be auto-created.
  2. Run the project in dev mode (npm i && npm run dev). Ignore warnings, no need of user/session secret.
  3. You should see home page rendered with additional Helper Test section.
  4. Open modules/@apostrophecms/home-page/views/page.html and uncoment around line 23

    {{ helper.render("Same helper called from inside of a macro will fail") }}

  5. You should see error page, look at the CLI output.

Expected behavior

Execute the module nunjucks helper without an error. Describe the bug

Registered via a3 module helpers are not available inside of any nunjucks macro. Details

An example call apos.moduleName.someHelper() raises error: Error: Unable to call apos["moduleName"]["someHelper"], which is undefined or falsey

Version of Node.js: v12.8.1

Server Operating System:

lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.1 LTS Release: 20.04 Codename: focal

No docker involved. Node env provided by nvm.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/apostrophecms/apostrophe/issues/2692, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAH27JKLTOS3G4QCF424G3S22XDPANCNFSM4WKO2J5Q .

--

THOMAS BOUTELL | CHIEF TECHNOLOGY OFFICER APOSTROPHECMS | apostrophecms.com | he/him/his

boutell commented 3 years ago

See PR:

https://github.com/apostrophecms/apostrophe/pull/2698

With this PR "with context" is not required.

boutell commented 3 years ago

Solution has been merged to the 3.0 branch. Thanks for raising the issue!