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

for <condition> {} loop does not work in templates $tmpl() #23447

Open
gechandesu opened this issue Jan 12, 2025 · 7 comments
Open

for <condition> {} loop does not work in templates $tmpl() #23447

gechandesu opened this issue Jan 12, 2025 · 7 comments

Comments

@gechandesu
Copy link
Contributor

gechandesu commented Jan 12, 2025

V version: V 0.4.9 d2b30df, press to see full `v doctor` output
V full version V 0.4.9 5ec9bb5.d2b30df
OS linux, Linux version 6.6.8-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Thu, 21 Dec 2023 19:01:01 +0000
Processor 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P
Memory 5.23GB/15.35GB
V executable /home/ge/.vlang/v
V last modified time 2025-01-12 11:38:38
V home dir OK, value: /home/ge/.vlang
VMODULES OK, value: /home/ge/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/ge/Code/experiments/v_templating
Git version git version 2.47.1
V git status weekly.2024.53-79-gd2b30df1
.git/config present true
cc version cc (GCC) 14.2.1 20240910
gcc version gcc (GCC) 14.2.1 20240910
clang version clang version 18.1.8
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (GNU libc) 2.40

What did you do?
./v -g -o vdbg cmd/v && ./vdbg main.v && main

module main

struct User {
    name string
    age int
}

fn main() {
    mut users := []User{}
    users << User{name: 'John Doe', age: 27}
    users << User{name: 'Jane Doe', age: 16}
    page := $tmpl('users.tmpl')
    println(page)
}

users.tmpl:

@for user in users {
    @{user.name} is @{user.age} years old
}

What did you see?

users.tmpl:20:1: error: unexpected eof, expecting `}`

What did you expect to see?

Generated text with users list:

John Doe is 27 years old
Jane Doe is 16 years old

According to the documentation it should work.

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Copy link

Connected to Huly®: V_0.6-21877

@islonely
Copy link
Contributor

For variables you don't need the curly braces; just @user.name

@gechandesu
Copy link
Contributor Author

For variables you don't need the curly braces; just @user.name

The error is reproduced despite the presence of curly braces. In addition, curly braces solve the parsing problem when the data in the template follows the variable name without spaces (e.g. dot at the end of a sentence).

@jorgeluismireles
Copy link

I have used @for … @end instead using {}

@gechandesu
Copy link
Contributor Author

I have used @for … @end instead using {}

Oh, it worked with:

@for user in users
    @user.name is @user.age years old
@end

V templating is so inconsistent...

How about deprecate @for <condition> {} syntax?

Also both $ and @ signs can be used for variables. It would probably be nice to have one way here too.

@gechandesu gechandesu changed the title for loop does not work in templates $tmpl() for <condition> {} loop does not work in templates $tmpl() Jan 13, 2025
@jorgeluismireles
Copy link

I think V borrowed the template syntax of @ from other frameworks to permit have javascript valid identifiers $, {, } untouched in the template. So for example (code not tested):

<script>
function discount(a) { alert('$' + 0.10*a); }
</script>
<table>
<tr><td>User</td><td>Discount</td></tr>
@for user in users
 <tr>
 <td>@user.name</td>
 <td><button onclick = "discount(@user.payment)">$ Discount</td>
</tr>
@end
</table>

@medvednikov
Copy link
Member

Also both $ and @ signs can be used for variables. It would probably be nice to have one way here too.

I agree. I tried doing that, but it's pretty tricky. The templates need to be re-written with a real parser.

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

4 participants