Comments (5)
Perhaps you saw my note, but to me Value::from_iterator()
seemed incredibly problematic in that it was a single-use value. If I wrote for x in some_iter { ... }
and then wrote for x in some_iter { ... }
again, I would never expect for the second iteration to be completely empty. Or perhaps it didn't work this way and the code just read this way?
In any case, my take-away was that we don't need to store iterators themselves, you need to store the thing that produces the iterator. This is the Value::from_object_iter()
constructor I added.
from minijinja.
Perhaps you saw my note, but to me Value::from_iterator() seemed incredibly problematic in that it was a single-use value. If I wrote for x in some_iter { ... } and then wrote for x in some_iter { ... } again, I would never expect for the second iteration to be completely empty. Or perhaps it didn't work this way and the code just read this way?
Iterators are the only way in which you can stream endless templates. See #416 for the motivation.
I was thinking originally even when writing the changes to introduce iterators to only allow "factories" to be passed to the context which create the iterator and the iterator is hidden, but then you cannot use any of the filters such as |list
without introducing yet another problem.
from minijinja.
Was added in #461
from minijinja.
I was thinking originally even when writing the changes to introduce iterators to only allow "factories" to be passed to the context which create the iterator and the iterator is hidden, but then you cannot use any of the filters such as |list without introducing yet another problem.
What problem does that introduce?
I really feel that the iterator objects being single-use is a huge foot-gun that needs to be resolved. I would never expect that functionality from any value.
from minijinja.
What problem does that introduce?
The original plan I had (pre introducing of streaming iterators) was a bit different. There the idea was that the iterator is created exclusively as part of the {% for %}
loop and to hide it within that loop (completely hidden iterator). That's why |list
and other operators did not work at all. They would just error out saying that an iterator
is not something that can be iterated over. The iterator when held in a Value
was thus non extractable by filters.
As for iterators and footguns: they are sort of. When you want to stream content via the template engine you cannot "restart" without refactoring all of what you have on the Rust side into a factory function that can trigger that stream more than once. The benefit of this is not entirely unclear. At that point it seems a lot more sense to me ask the user to say {% set iter = start_streaming() %}
rather than magically creating the stream from scratch every time someone starts iterating.
However I do agree that iterators made with Value::from_iterator
are not a great idea for the general use and they should be discouraged compared to restart-able iterators as created by Value::from_object_iter
.
I will open a separate issue for the iterator question.
from minijinja.
Related Issues (20)
- 2.0: Non Object Objects
- 2.0: DynObject is not an Object HOT 1
- Don't allocate to `reverse` HOT 1
- 2.0: One-Shot vs Repeatable Iteration
- 2.0: Rename Value::from_serializable HOT 1
- 2.0: Make Syntax extensible and move whitespace control into it
- `Object::len()` and `Object::is_empty()` conflict with inherent `len()`, `is_empty()` HOT 1
- 2.0: Investigate Performance Regressions HOT 2
- Add Protection Against Buffering
- `undeclared_variables()` stuck with `for loop` ({% for ... in ... %})
- String concatenation without "+" operator HOT 1
- Fallible Iteration
- set_lstrip_blocks should not apply to expressions, only to blocks.
- Custom time format strings HOT 5
- UndefinedBehavior::Strict and undefined key in mapping HOT 4
- Option to always chomp whitespace HOT 3
- async support for Object trait HOT 1
- Add support for the `*` operator HOT 2
- Rendering template using yaml data file containing anchors HOT 3
- actix_web使用的时候正确使用示例有吗?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from minijinja.