nunjucks | Use Nunjucks templates with Fractal | REST library
kandi X-RAY | nunjucks Summary
kandi X-RAY | nunjucks Summary
This repository has been moved into Fractal main repository, see packages/nunjucks.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of nunjucks
nunjucks Key Features
nunjucks Examples and Code Snippets
Community Discussions
Trending Discussions on nunjucks
QUESTION
Is there a way of mapping an array by extracting a specific attribute from each element using a Nunjucks filter?
I'd like to do something like the following, but I can't figure out how to do it using the inbuilt Nunjucks operations.
Input
...ANSWER
Answered 2021-May-14 at 09:34const nunjucks = require('nunjucks');
const env = nunjucks.configure();
env.addFilter('map', function (arr, prop, ...etc) {
var f = typeof prop == 'function' ? prop : typeof this.env.filters[prop] == 'function' ? this.env.filters[prop] : (e) => e[prop];
return arr instanceof Array && arr.map(e => f(e, ...etc)) || arr;
});
const foods = [
{name: "apple", colour: "green"},
{name: "plum", colour: "purple"},
{name: "cherry", colour: "red"}
];
const html = env.renderString(`{{ foods | map('name') }}`, {foods});
console.log(html);
QUESTION
I'm using A-Frame and A-Frame template-component.
I use template-component to load templates from <script id="..."> tag like in example.
I want to change the template on the fly using the el.setAttribute analogically like in this example.
My template contains a <sky> element with video as a source.
I can load one template and it works like a charm. I can pause, play it, and so on.
The issue appears when I want to switch the template to a new one that is using the same video resource, or load a new one and then return to the first one.
now I have 2 sounds - one without sync to the video.
One video is displayed but a second sound plays in the background. I also can verify that the sound is not coming from the displayed video. I can still play/pause it and its sound plays over the one in the background.
edited
I want to be able to modify not only the <sky> element but also some others, that's why I wanted to use the template-component. Let's also say that I don't know what assets would be finally used and I don't want to use an asset management system to load tens of Mb of videos/objects and others and suspend the user at a loading screen. As stated here:
The scene won’t render or initialize until the browser fetches (or errors out) all the assets or the asset system reaches the timeout.
To clarify a little bit more my intention but still keeping it simple, I want the user to be able to change the scene by choosing a prefab (assuming 15Mb per video even with 6-7 assets it may almost reach 100Mb), or by uploading one. And I would really like to avoid reloading the page as it is not an elegant solution for the users perspective.
My guess:I think the old video is not disposed of entirely. I tried using the three.js .dispose() method on various elements/ materials/ video itself, but with no luck.
I also tried using <a-video> as a source, and also deleting the elements before switching, but with the same result.
- How to properly remove elements loaded by template so that the video is not stored/ cached anywhere?
- Can I somehow set the autoplay="false" or similar to <sky> video?
- If my approach is entirely wrong, can you point me in the right direction?
ANSWER
Answered 2021-Apr-20 at 15:45QUESTION
On my website, I want to show a list of collection items on pages corresponding to each collection. For example on the Games page, I want to show a list of all game related articles, on the Politics page, I want to show a list of all politics related articles, etc.
What I have right now is one Nunjucks template file for each such page, which I don't particularly like since they are the same except for the collection to show. Here is two of my templates, for my politics articles and for my games articles, where I extend a common boilerplate (base.njk) and in the main block, I first print some Markdown content and then comes the list of collection items:
...ANSWER
Answered 2021-May-06 at 08:18You might be able to get to the collection using computed data, though I'm not sure if you have access to the collections there.
A simpler approach: Set your frontmatter field to the name of the colletion you want to display as a string, then use that to get the collection in the template:
QUESTION
I have the following njk code:
...ANSWER
Answered 2021-Apr-15 at 06:44Try using the nunjucks for loops. This worked for me
This is the code
QUESTION
Here is my code:
...ANSWER
Answered 2021-Apr-02 at 08:18Since you've configured nunjucks to look up the views in the templates
directory (in the nunjucks.configure
call), you should move the server.html
file to the templates
directory.
QUESTION
Some of the test data have img some not.
I am new to Nunjucks and google does not find how to test if the record have key img?
I try the following:
...ANSWER
Answered 2021-Mar-23 at 13:57{% if r.img is defined %} should also do the trick. – Lesha Ogonkov 3 hours ago
QUESTION
I was trying to add a custom filter with nunjucks following the docs. But running the code shows output -- Error: filter not found: my_filter_here
These are my configurations:
index.js
...ANSWER
Answered 2021-Mar-19 at 17:35According to doc: configure
returns an Environment instance. Therefore
QUESTION
I have data from headless CMS of the following type:
...ANSWER
Answered 2021-Mar-09 at 09:06Not sure what you are missing, your code looks fine. You apply a filter in Nunjucks using the pipe operator:
QUESTION
So I have some books data coming from a JSON API. For some of the items in the API, I have created a page in 11ty. I’m trying to list out the items from the API, and check if each item’s slug
field matches a fileSlug
in my local pages collection. If so, then I can output a link to the content page; otherwise, just output the book’s title from the API.
Here’s what I have in Nunjucks:
...ANSWER
Answered 2021-Jan-04 at 15:57The problem is the nested loop. You have one loop for the API books and another one inside that for every local book, so your code is running through all local books for every book from the API. This is why you're seeing additional output, the total number of lines will be the count of API books multiplied by the count of local books (e.g. 2 * 2 = 4
, or 2 * 3 = 6
).
What you want to do is not trivial in Nunjucks. Normally, you could use the in
keyword to check if a book is part of your collection, but that doesn't work when you need to compare object properties.
Instead, I would recommend moving your logic to JavaScript, where you can do that comparison much easier using Array.prototype.find and similar methods. For example, does your apiBooks variable come from a data file? In that case, you could parse the list there and add the reference to the local book file, if available. Another method would be to add a custom filter that takes an apiBook object and the local books collection as parameters and returns the matching book, if one exists.
QUESTION
Okay, first of all: if I could have phrased my question in a less awkwardly obtuse manner, I would have done so. 😜
I'm struggling with how to describe what I'm trying to do.
Goal
I'm attempting to iterate over an object (specifically, the colors
object in the default TailwindCss config) in order to generate a list of tokens I can pull into a nunjucks template. This is proving more challenging than I'd expected.
I'm probably missing something basic, but I don't know what I don't know, ya know?
I've got the following function that just loops over the colors object:
...ANSWER
Answered 2020-Dec-24 at 19:31you can do things like that :
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
Install nunjucks
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page