How to render a custom view in a NPM module?
How to render a custom view in a NPM module?
Since pushAsset
does not allow to load external resources, I want to render this template/script of my NPM module:
pushAsset
views/tag.html
https://www.googletagmanager.com/gtag/js?id=%20data.gid%20
NPM modules are kind of different to project level modules, because I cannot directly edit the project files.
This template should render on every page request. This tag should be always at the start of the
head
.– crstn
Jun 27 at 17:08
head
You should be able to just
{% include 'tag.html' %}
right in your block tag, assuming your tag.html lives in lib/modules/apostrophe-templates/views/tag.html
or its fallback equivalent. If its an individual module you would {% include 'my-module:tag.html' %}
– Stuart Romanek
Jun 27 at 17:55
{% include 'tag.html' %}
lib/modules/apostrophe-templates/views/tag.html
{% include 'my-module:tag.html' %}
Sorry, I forgot that this relates to NPM modules. Updated the question and answered it appropriately.
– crstn
Jun 29 at 17:31
1 Answer
1
Apostrophe provides helper functions for Nunjucks. Some of them – like addAfterContextMenu – can be used as hooks to insert templates of NPM modules into other templates.
These hooks are declared inside apos.pages
. Usage:
apos.pages
self.apos.pages.startHead(function(req) {
return self.partial('templateName', { gid: 'UA-123456-1' });
});
startHead
could be a hook that inserts the partial
inside the startHead
block of outerLayoutBase
. Currently, Apostrophe does not provide many hooks for NPM module authors. But this is subject to change.
startHead
partial
startHead
outerLayoutBase
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When / how are you trying to render this tag template? What is the desired flow to get this template in the browser?
– Stuart Romanek
Jun 27 at 14:02