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

[Feature Request] ignoring new line and tab indents in special tags #63

Closed
muthukrishnan24 opened this issue Aug 29, 2019 · 3 comments
Closed

Comments

@muthukrishnan24
Copy link

I am using QTPL for go code generation.
i see that it is intended for html template. But it can be very useful for all kind of text generation.

issue #27 does not cover my case

below is a sample template func to explain my use case

{% func GenTestFunc() %}
	func TestFunc(a int, b int) int {
		var sum int
		{% for i := 0; i < 10; i++ %}
			sum += {%d i %}
		{% endfor %}
		return sum
	}
{% endfunc %}

I'm getting:

	func TestFunc(a int, b int) int {
		var sum int
		
			sum += 0
		
			sum += 1
		
			sum += 2
		
			sum += 3
		
			sum += 4
		
			sum += 5
		
			sum += 6
		
			sum += 7
		
			sum += 8
		
			sum += 9

		return sum
		
	}

expect something like this:

func TestFunc(a int, b int) int {
	var sum int
	sum += 0
	sum += 1
	sum += 2
	sum += 3
	sum += 4
	sum += 5
	sum += 6
	sum += 7
	sum += 8
	sum += 9
	return sum
}

i tried {% stripspace %} and {% collapsespace %} but they are not sufficient

  1. {% func %}, {% for %}, {% if %} and other tags, take string between as it is, to improve readability, these blocks can ignore first tab/space indent, or special tag to ignore them
  2. also each the block, create a new line before them

i had to write this to get the intended output

{% func GenTestFunc() %}
func TestFunc(a int, b int) int {
	var sum int{% for i := 0; i < 10; i++ %}
	sum += {%d i %}{% endfor %}
	return sum
}
{% endfunc %}
@turutcrane
Copy link
Contributor

My Proposal for this issue
Pull request is this.

Tag opening mark "{%-" strip tail blanks form previous text.
Tag ending mark "-%}" strip blanks to new line from next text.
Below code

{% func GenTestFunc() %}
func TestFunc(a int, b int) int {
	var sum int
	{%- for i := 0; i < 10; i++ -%}
	sum += {%d i %}
	{%- endfor %}
	return sum
}
{% endfunc %}

outputs

func TestFunc(a int, b int) int {
	var sum int
	sum += 0
	sum += 1
	sum += 2
	sum += 3
	sum += 4
	sum += 5
	sum += 6
	sum += 7
	sum += 8
	sum += 9

	return sum
}

valyala added a commit that referenced this issue Oct 29, 2019
- Document `{%-` and `-%}` in README.md
- Add integration test
- Code simplification
@valyala
Copy link
Owner

valyala commented Oct 29, 2019

Merged the PR

@valyala
Copy link
Owner

valyala commented Oct 29, 2019

The feature is available starting from v1.4.0.

@valyala valyala closed this as completed Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants