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

Why I can't use variable into attribute? #21

Closed
gr1feel opened this issue Apr 19, 2013 · 5 comments
Closed

Why I can't use variable into attribute? #21

gr1feel opened this issue Apr 19, 2013 · 5 comments

Comments

@gr1feel
Copy link

gr1feel commented Apr 19, 2013

Hi kirbysayshi ,
I have a little problem. Why I can't use variable into attribute?
For example, this code always fails https://gist.github.com/gr1feel/5418010

@gr1feel
Copy link
Author

gr1feel commented Apr 19, 2013

This is code also doesn't work https://gist.github.com/gr1feel/5418016
I suppose what problem in parentheses.

@kirbysayshi
Copy link
Owner

I'm not sure I follow, those render just fine for me:

$ echo '<a onclick="alert('@model.text1')">click me!</a> ' | ./node_modules/vash/bin/vash --json '{"debug": false}' --render '{ "text1": "hello" }'
<a onclick="alert(hello)">click me!</a>
drewp@SpottedRadioFreeAir /tmp/vash-test$ node
> var vash = require('vash');
undefined
> var tpl = vash.compile("<a onclick=\"alert('@model.text1')\">click me!</a>")
undefined
> tpl({ text1: "hello" })
'<a onclick="alert(\'hello\')">click me!</a>'

Could you provide more details as to what "fail" means in your case?

@kirbysayshi
Copy link
Owner

Any other details you can provide?

@gr1feel
Copy link
Author

gr1feel commented May 6, 2013

Hi,
Sorry for delay. Check this example (pages /test1 and /test2).

https://docs.google.com/file/d/0B0E2riyj_WiwVDBNMnpvTndBaFU/view

@kirbysayshi
Copy link
Owner

Ok, thanks for the examples, they really help.

The issue is that a <br /> tells Vash to enter and immediately exit markup mode and return to the previous mode, which is this case is a code block. Thus Text1: and Text2: are being interpreted as code instead of markup. Vash requires that any actual content be wrapped in either html tags (or the fake <text> tag) or an @: for a single line.

There is another issue too in the same vein. The parentheses in (@model.text2) are being interpreted as code. When compiled, it looks like this:

(__vbuffer.push(html.escape(model.text2).toHtmlString());)

A semicolon is not valid JS within the grouping parentheses, so this is actually not even compiling. The error reporting is giving you a false sense of where the error is happening because it's not actually at runtime, but rather at compile time. This is just bad reporting that I've been meaning to fix, and have opened an issue to keep track of it (#22).

Your immediate solution:

@html.block('content', function(model){

  @* Remember that <text> tags are not rendered! *@
  <text>
  <br/>Text1: @model.text1
  <br/>Text2: @model.text2

  <br/>@model.child.xxx


  <span>hi</span> (@model.text2)
  </text>
})

There are others, like using @: on each line (pretty gross, just an example):

@html.block('content', function(model){

  @:<br/>Text1: @model.text1
  @:<br/>Text2: @model.text2

  <br/>@model.child.xxx


  @:<span>hi</span> (@model.text2)
})

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

2 participants