Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

[feature request] multi-line django template tags #45

Closed
AndreCimander opened this issue Dec 12, 2016 · 7 comments
Closed

[feature request] multi-line django template tags #45

AndreCimander opened this issue Dec 12, 2016 · 7 comments

Comments

@AndreCimander
Copy link

First of all: Thank you for picking up maintenance of the package! 👍

We just noticed in #583c0a3 with that we can't use multi-line django template tags anymore... which was neat to increase readability. e.g.

{% my_tag
   so=True
   many='VeryLongStringIdWhatever'
   params=somevar
   ...
%}

Any chance in getting this feature back?

@rowanseymour
Copy link
Member

Hmm not sure about this. It worked in the past because even tho %} starts with an element prefix, the element parser would ignore anything that didn't match its regex. Now we properly parse elements so that something like %span(class="foo"} generates an error rather than being silently ignored.

To make this work again we'd have to go back to ignoring malformed elements or add some kind of exception for lines beginning %}. Maybe the latter option is ok but it's a bit of hack.

Ideally we'd find a more Haml way to do what you want - something that adheres to Haml's use of indentation to determine structure. Users shouldn't be resorting to {% foo ... %} syntax because we have -foo syntax to generate Django tags.

I think in actual Haml you'd use | to split this over multiple lines (http://haml.info/docs/yardoc/file.REFERENCE.html#multiline), but that's pretty ugly. Element attributes are an exception (they can span multiple lines with |), but as they are contained inside {...} or (...) it's easy to parse over multiple lines.

If you need an immediate workaround I think your example would work as...

{% my_tag
   so=True
   many='VeryLongStringIdWhatever'
   params=somevar %}

@rowanseymour
Copy link
Member

Another way to do this is to use \ to escape the %:

{% my_tag
   so=True
   many='VeryLongStringIdWhatever'
   params=somevar 
\%}

@rowanseymour
Copy link
Member

Been brainstorming with @nicpottier about this. It's challenging to support consistent mixing of Django and Haml outside of Haml's hierarchical node structure, but would be relatively straightforward to support Django tags as a special node type. So..

{% 
  trans "hello" 
%}
%span
  {% 
    trans "hello" 
  %}

becomes

{% 
  trans "hello" 
%}
<span>
  {% 
    trans "world" 
  %}
</span>

The requirement here is that the {% starts a new line. The parser sees that as starting a new "native tag node" and knows to parse it up to %}. Would that work for you?

@nicpottier
Copy link

to be clear, this would work as well ya?

.foo
  {% trans 
      "hello world"
  %}

@AndreCimander
Copy link
Author

We tried to use %} and same-line "%}"-closing tag, both workarounds didn't work, but I hadn't much time to play around as I was supposed to do a code review, I might try it later tonight or tomorrow.

The newline requirement to handle the native tags would be perfectly fine for us :)

@rowanseymour
Copy link
Member

So having slept on this I've come back to letting the node parser just ignore lines beginning %} and convinced myself it's not actually hacky (see PR #46) - basically we want the parser to treat native Django tags like plain text (i.e. part of the haml tree, but not transformed), and users should be able to use native tags anywhere they put plain text.

Ideally tho users shouldn't have to revert to native tags and the fact that they do (even we do) points to deficiencies in the -tag syntax. Maybe in future this syntax needs a way to span multiple lines or appear inline with an element.

@rowanseymour
Copy link
Member

@AndreCimander #46 is merged now and should fix your issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants