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

Indent code in Go HTML templates #709

Merged
merged 1 commit into from
Feb 13, 2016
Merged

Indent code in Go HTML templates #709

merged 1 commit into from
Feb 13, 2016

Conversation

jinzhu
Copy link
Contributor

@jinzhu jinzhu commented Feb 3, 2016

Before when write go code in templates, it is not indented correctly, for example:

{{if true}}
hello world
{{end}}

I think this should be indented to:

{{if true}}
  hello world
{{end}}

This pull request did the job, fixed the indention for if/else, range, with, block, define

@fatih
Copy link
Owner

fatih commented Feb 4, 2016

Thanks @jinzhu for your contribution. @cespare do you mind have a look at it ? I'm not using templates recently. It looks good otherwise :)

@jinzhu
Copy link
Contributor Author

jinzhu commented Feb 5, 2016

I have made a test template to try to cover all cases, hope it could be helpful.

<div class="qor-page__body">
  {{render "hello1"}}
  {{render "hello2"}}

  {{if true}}
    <p>
      hello world
      {{if hello}}
        hello world
      {{else}}
        hello world
      {{end}}
    </p>
  {{else}}
  {{end}}

  {{render "hello3"}}
</div>

@cespare
Copy link
Contributor

cespare commented Feb 5, 2016

I can take a look in the next few days.
On Feb 4, 2016 2:00 AM, "Fatih Arslan" notifications@github.com wrote:

Thanks @jinzhu https://github.com/jinzhu for your contribution. @cespare
https://github.com/cespare do you mind have a look at it ? I'm not
using templates recently.


Reply to this email directly or view it on GitHub
#709 (comment).

@fatih
Copy link
Owner

fatih commented Feb 8, 2016

Thanks @cespare 👍

@cespare
Copy link
Contributor

cespare commented Feb 9, 2016

This change doesn't work for me at all, because the HtmlIndent function is not defined.

@fatih
Copy link
Owner

fatih commented Feb 11, 2016

@jinzhu any comments on that ?

@jinzhu
Copy link
Contributor Author

jinzhu commented Feb 12, 2016

Hi @cespare

This method is defined in vim, file indent/html.vim, which is required from here https://github.com/fatih/vim-go/blob/master/indent/gohtmltmpl.vim#L5

image

@jinzhu
Copy link
Contributor Author

jinzhu commented Feb 12, 2016

@fatih
Copy link
Owner

fatih commented Feb 12, 2016

Maybe we should add a clause to check if this exists or not by checking the version and patch level. An example how to do it: http://vi.stackexchange.com/a/2467/2791

@cespare
Copy link
Contributor

cespare commented Feb 12, 2016

BTW I'm using a fairly recent vim (7.4 something, can't check the exact version at the moment).

I suspect that calling this function is the wrong approach in general. When I look at indent files for other templating languages, they don't do it this way. Example: https://github.com/mitsuhiko/vim-jinja/blob/master/indent/htmljinja.vim

@jinzhu
Copy link
Contributor Author

jinzhu commented Feb 12, 2016

I just pushed another commit, fixed it doesn't work on vim 7.3, I have tested new fix with below three versions, all works for me. @cespare could you test it works for you or not?

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Feb 10 2016 14:45:20)
MacOS X (unix) version
Included patches: 1-1257
NVIM v0.1.1-145-g074d3dc (compiled Jan  6 2016 09:09:15)
Build type: Dev
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 27 2015 16:22:14)
Compiled by root@apple.com
Normal version without GUI.  Features included (+) or not (-):

I think vim-jinja is just using another solution to make things works, from github search results count, it is less used that current solution. I haven't found much documents about this when writing the code, so just pick up a simper solution. ;)

@cespare
Copy link
Contributor

cespare commented Feb 12, 2016

Works for me.

LGTM.

" If need to indent based on last line
let last_line = getline(a:lnum-1)
if last_line =~ '^\s*{{\s*\%(if\|else\|range\|with\|define\|block\).*}}'
let ind = ind + sw
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let ind += sw

@fatih
Copy link
Owner

fatih commented Feb 13, 2016

@jinzhu this looks good. Can you squash it into one commit if possible?

@jinzhu
Copy link
Contributor Author

jinzhu commented Feb 13, 2016

@fatih sure, just did this ;)

fatih added a commit that referenced this pull request Feb 13, 2016
Indent code in Go HTML templates
@fatih fatih merged commit 8f3d83f into fatih:master Feb 13, 2016
@fatih
Copy link
Owner

fatih commented Feb 13, 2016

Thanks @jinzhu for the contribution 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants