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

Errors using conditionals in vweb templates #19475

Open
bee-plus opened this issue Sep 29, 2023 · 2 comments
Open

Errors using conditionals in vweb templates #19475

bee-plus opened this issue Sep 29, 2023 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: vweb Bugs/feature requests, that are related to the `vweb`.

Comments

@bee-plus
Copy link

bee-plus commented Sep 29, 2023

Describe the bug

I am getting a couple different errors when I try to use conditionals inside templates.

While troubleshooting, I also noticed that templates don't handle HTML comments. Commenting out the conditional section of the template with <!--<...>--> does not prevent the compiler from trying to process the code inside, and it still fails. I can open another issue for this if you'd like.

Reproduction Steps

main.v

module main

import vweb

struct App {
	vweb.Context
}

fn main() {
	mut app := &App{}
	vweb.run(app, 8081)
}

['/']
pub fn (mut app App) hello() vweb.Result {
	content := 'hello!'
	optional := true
	base := $tmpl('templates/base.html')
	return app.html(base)
}

templates/base.html

Single-line:

<html>

<body>
  <main>
    <p>${content}</p>
    @if optional { <p>optional</p> }
  </main>
</body>

</html>

Multi-line:

<html>

<body>
  <main>
    <p>${content}</p>
    @if optional {
    <p>optional</p>
    }
  </main>
</body>

</html>

Expected Behavior

Code should compile and produce:

<html>

<body>
  <main>
    <p>hello!</p>
    <p>optional</p>
  </main>
</body>

</html>

Current Behavior

The single-line version fails with:

templates/base.html:16:15: error: invalid expression: unexpected token `<`

The multi-line version fails with:

templates/base.html:30:1: error: unexpected eof, expecting `}`

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.1 1476daf

Environment details (OS name and version, etc.)

V full version: V 0.4.1 9929e95.1476daf
OS: macos, macOS, 14.0, 23A344
Processor: 10 cpus, 64bit, little endian, Apple M1 Max

getwd: /Users/user/src/vweb-if-repro
vexe: /Users/user/src/v/v
vexe mtime: 2023-09-29 18:25:11

vroot: OK, value: /Users/user/src/v
VMODULES: OK, value: /Users/user/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.42.0
Git vroot status: weekly.2023.38-62-g1476daf8 (2 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.0.40.1)
thirdparty/tcc status: thirdparty-macos-arm64 a668e5a0

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@bee-plus bee-plus added the Bug This tag is applied to issues which reports bugs. label Sep 29, 2023
@ArtemkaKun ArtemkaKun added the Unit: vweb Bugs/feature requests, that are related to the `vweb`. label Sep 30, 2023
@jacksonmowry
Copy link
Contributor

jacksonmowry commented Oct 4, 2023

Did you try

@if condition
    <div>Hi mom</div>
@end

I think this is the correct syntax. You can also you and @else in the same block.

@bee-plus
Copy link
Author

bee-plus commented Oct 6, 2023

I did not, but it works! Thank you. Is there a way to do it inline?

Regarding documentation: vweb's vdoc page only briefly mentions template directives, and contains a link that points here. Because that link is dead, I looked around a bit and found this, but it seems to no longer be accurate.

I now see an example @for loop in the vdoc page that matches your syntax, maybe that could be called out more explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: vweb Bugs/feature requests, that are related to the `vweb`.
Projects
None yet
Development

No branches or pull requests

3 participants