Comments (16)
I don't think we offer control over this now, but it might be easy to change the behavior?
On the other hand, not sure if that's always desirable, and if not, how we would spell configuring it.
from askama.
On the other hand, not sure if that's always desirable (...)
Definitely not.
(...) and if not, how we would spell configuring it.
My first thought would be {%+
- that is, it should be within whitespace control territory.
EDIT: forgot {%+
implies whitespace preservation - how about {%=
as in "equal indent"?
from askama.
My first thought would be
{%+
- that is, it should be within whitespace control territory.EDIT: forgot
{%+
implies whitespace preservation - how about{%=
as in "equal indent"?
Sounds somewhat reasonable. So we only allow this for macro definitions, right?
@Kijewski @vallentin any thoughts?
from askama.
I can see why one would want this feature, not only in like in mataha's example, but in regular HTML output, too.
I guess this would be reasonably simple to implement, at least if the indentation level was user supplied by some means. I don't think it would be feasible to figure out the indentation level automatically at compile time.
Then Generator
could have an indentation_stack: Vec<String>
that is pushed to by calls to a macro, and popped from at the end of a macro. In Generator::visit_lit()
the topmost indentation_stack
gets used to prefix new lines.
from askama.
What I've envisioned, though, is figuring out the indentation at compile time. Supplying it would require either some weird syntax ({#{1}+
? I don't know), passing indent level through call
(how?) or defining it globally in the template
attribute (again, how?).
Generator
does have access to whitespace in template here and here, right?
from askama.
We could do something like the whitespace after any newlines in the literal preceding the {% call %}
node. That seems... a little magic and might not always have a very intuitive result but I suppose it would do the desired thing in a bunch of the common cases.
from askama.
That solves my problem in full and - perhaps counterintuitively - is what I would expect.
Besides that, what other cases do you think can occur?
from askama.
Well, all of these could happen:
{%= call foo(bar) %}
{% if true %}{%= call foo(bar) %}{% endif %}
{% if true %}{% else %} bloop {%= call foo(bar) %}{% endif %}
In particular, this last one I think would repeat bloop
for every line that the macro produces.
from askama.
How about make it either:
- indent the contents with whitespace if the preceding characters after the last newline are all whitespace (i.e. match
^\s*
; in the last case would not indent) - indent the contents with whitespace equal to the number of characters preceding a literal after the last newline (in the last case 7, but it would have to account for wide characters and possibly other corner cases...)
from askama.
@Kijewski any thoughts? Inventing a character sequence based on count seems way too magic. But I'm inclined to think the least magic behavior is that =
just preserves the contents preceding the block after the last newline.
from askama.
I don't see any advantage in counting white space characters. If I use tabs, spaces, and some odd unicode characters to indent my code, then this exact indentation should be replicated.
In the end, I think everything between \n
and {%=
should be replicated. Maybe I want to wrap my text in a markdown blockquote >
?
The only problem is if there are nodes {{expr}}
/ {%stmt%}
before the {%=
. I'd say, just make it a syntax error.
from askama.
I don't think we should make that a syntax error -- it would just only reproduce the whitespace after the {{ expr }}
node: I specified the behavior as the "the literal preceding the {% call %}
" node. So
{% if true %}{% else %} bloop {%= call foo(bar) %}{% endif %}
would reproduce bloop
on every line before the macro contents, because that's the literal before the {% call %}
node.
from askama.
Yeah, makes sense.
{% else %} bloop {%=
→bloop
and{% else -%} bloop {%=
→bloop
?
from askama.
Yup.
from askama.
In the end, I think everything between
\n
and{%=
should be replicated. Maybe I want to wrap my text in a markdown blockquote>
?
That makes perfect sense. Just saying that at that point it's no longer "whitespace handling", of course.
from askama.
I think that's okay. Just needs good documentation.
from askama.
Related Issues (20)
- global variables HOT 5
- Associated functions on structs with generics are not callable HOT 2
- How could i get session flash message in a view after redirect HOT 2
- Question: How should I return templates in actix-web HOT 2
- Cannot specify single named argument in macro HOT 5
- 0.12.1 only allow lower-case variable name in template HOT 5
- Keep the template source code portion corresponding to each AST token HOT 9
- Allow to have namespace for macros HOT 1
- Allowing imports and macro declaration everywhere? HOT 2
- I could not give relative path to template on linux but can give on windows HOT 7
- Issue
- Regression in askama_parser 0.2.0 update HOT 14
- Composing templates HOT 3
- 8e03fa6132669383a763d628b7443d1758b50400 HOT 1
- Document the `fmt` filter HOT 3
- Can't render simple template. Axum integration. HOT 2
- Add support for filter section HOT 5
- Document how integration should be imported HOT 1
- Remove the need to import askama itself when using integration crates HOT 3
- Why if block does not work inside another block HOT 4
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 askama.