kirbysayshi / vash

Vash, the 60 billion double-dollar template-maker. Razor syntax, for JavaScript templates
Other
522 stars 60 forks source link

"@expressions are only necessary within markup tags" may be overzealous #59

Closed meandmycode closed 9 years ago

meandmycode commented 9 years ago

Hello,

After updating to 0.8 one of the things I've noticed is logs for this, in most of the places this seems to make sense (although continuing to support this would be desirable), but there appears to be some places where this warning is not correct, for example:

@if (model.content != null) {
    @model.content
}

In this example, removing the @ prefix from model.content will stop the value of model.content from being rendered.

Thanks,

kirbysayshi commented 9 years ago

You are correct that removing the @ will prevent rendering. But it's technically not correct to transition from a code context to an output context, at least as far as how I've been working through vash's modes lately. Do you know if razor itself would allow this (I don't have visual studio on hand at the moment)?

The "proper" way to do this would be:

@if (model.content != null) {
    <text>@model.content</text>
}

or

@if (model.content != null) {
    @:@model.content
}

or

@(model.content != null ? model.content : '')

Do you have any other examples where requiring an explicit transition to markup is problematic?

meandmycode commented 9 years ago

Razor I believe considers these statement expressions (a code block without a keyword like if / else /for) to be treated as an implicit html encoded write operation, the explicit @: I believe is only required when mixing verbatim text, for example:

@if (model.content != null) {
    @(model.content) @:suffix
}

There's a couple of examples here (albeit using foreach instead of if): http://www.mikesdotnetting.com/article/153/inline-razor-syntax-overview

I understand it may be trickier for vash to support this as well as other syntax but its certainly useful in some cases, admittedly these cases are rarer and there are work arounds / alternate syntax.

Thanks for the rapid responses with this and the other minor issues I reported yesterday, it is much appreciated!

kirbysayshi commented 9 years ago

@meandmycode thanks so much for the examples. I'd forgotten that that syntax was valid! I removed the deprecation warnings, and have no plans to deprecate that syntax.

I put the deprecation warning in because I thought it was just a holdover from two years ago when I was initially laying down the razor grammar. But if it's part of razor, it's in.