Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

djhtml is not indenting javascript code if not surrounded by script tag #121

Open
marcodicro-dp opened this issue Jul 22, 2024 · 3 comments

Comments

@marcodicro-dp
Copy link

I have a shell html template that other templates extend from.

In the shell template I have this:

<script type="text/javascript">
  {% block js_includes %}{% endblock %}
</script>

That way, I don't have to include the script tag in every template that extends from the shell.

However, when I run djhtml over those files, the javascript code is not properly indented:

{% block js_includes %}

  $(document).ready(function() {

  $('input:radio[name="reason"][class~="radio-ext"]').change(function (e) {
  var $el = $(e.currentTarget);

  $('input:radio[name="reason"]').each(function(){
  if ($el.attr('id') != $(this).attr('id')) {
  $(this).next().next().addClass('hide');
  } else {
  $(this).next().next().toggleClass('hide');
  }
  });
  });
  })
{% endblock %}

It is properly indented if I add surrounding script tags:

{% block js_includes %}
  <script type="text/javascript">

    $(document).ready(function() {

      $('input:radio[name="reason"][class~="radio-ext"]').change(function (e) {
        var $el = $(e.currentTarget);

        $('input:radio[name="reason"]').each(function(){
          if ($el.attr('id') != $(this).attr('id')) {
            $(this).next().next().addClass('hide');
          } else {
            $(this).next().next().toggleClass('hide');
          }
        });
      });
    })
  </script>
{% endblock %}

How could we fix/workaround this? Can the script tag not be mandatory to properly indent js?

@marcodicro-dp
Copy link
Author

Could we have {# fmt:js_on #} and {# fmt:js_off #} options to force toggle js mode?

@aci2n
Copy link

aci2n commented Jul 29, 2024

I am giving this a shot in aci2n@cfa9250 :D

@JaapJoris
Copy link
Member

Since this is a similar problem to #110, I would like to repeat the comment that I made there:

Unfortunately, I have to conclude (#103 (comment)) that supporting a {# fmt: #} tag is hard without running into inconsistencies in the InsideHTMLTag mode. It's the most complicated (read "hacky") mode in DjHTML, but I like the way it currently performs and I don't want to change it.

It is feasible to implement {# fmt:mode #} inside all modes except that one, though. However, it would not be consistent (read "fair") to support {# fmt:mode #} in some places and not in others, so I'd rather not support it at all. After all, there already exists {# fmt:off #} and {# fmt:on #} as an "escape hatch" for all cases that DjHTML doesn't support.

However, if someone manages to get it to work (including test coverage), I'll of course be happy to merge the PR!

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

No branches or pull requests

3 participants