Proposal: Add ZML Views and ZML Pages
See original GitHub issueProposal:
I suggest to support a new ZML views / pages with the file extension .zml. ZML
stands for raZor XML
, where I represented language statements as XML tags.
Benefits:
This can achieve the following:
- Using a more homogeneous code, which is just an XML code!
- Using less
{ } ; @
symbols, so, the code will be more compact and more friendly. - Using one generalized view syntax makes it valid for use for all programming languages.
- This will eliminate the need to create vbxml and fshtml razor views!
- All Razor rules still applies, so the learning curve is low.
- Building ZML views on the top of Razor engine means it will benefit from any future new features added to the Razor engine, which uses C# as the internal language.
- Pages designed in apps written by any language, can be reused in other languages as they are!
The cost to implement ZML in .NET is the cost of witting an easy ZML tags to C# translator and add VS.NET editor support for ZML files.
#Implementation: I add a ZML support in Vazor. I translate ZML tags to C# code to get a C# Razor, then send it to Razor engine to carry the work! This ZML Module contains a simple implementation of ZML using XML literals in VB.NET. It doesn’t do any syntax check, assuming the ZML format is reviewed by the editor. Currently, it supports:
-
<page/>
for@page
-
<model type="mtype" />
for@model mtype
Note: if the type is generic, use VB syntax<model type="List(Of mtype)" />
, because XML literals considersList<mtype>
as an opining tag without the closing one! -
<set object="obj" value="val"/>
for C# Razor:
@{
obj = val;
}
- ZML tag:
<if condition="cond">
block of zml code
</if>
for C# Razor:
@if (cond)
{
block of zml code
}
- ZML tag:
<if condition="cond1">
<then>
block of zml code
</then>
<elseif condition="cond2">
block of zml code
</elseif>
<!--repeate elseif tags as you need -->
<else>
block of zml code
</else>
</if>
for C# Razor:
@if (cond1)
{
block of zml code
}
elseif (cond2)
{
block of zml code
}
else
{
block of zml code
}
Note: you can nest if tags as you need.
- ZML tag:
<foreach var="m" in="Model">
block of zml code
</foreach>
for C# Razor:
@foreach (var m in Model)
{
block of zml code
}
These are the supported ZML tags until now (which are the most used), but every language structure can be represented in the same way.
Example:
ZML code:
<page />
<model type="CatalogModel"/>
<set object="ViewData["Title"]" value="zml sample" />
<set object="ViewData("Message")" value="another syntax" />
<div class="container">
<if condition="CatalogModel.CatalogItems.Any()">
<then>
<partial name="_pagination" for="CatalogModel.PaginationInfo"/>
<div class="esh-catalog-items row">
<foreach var="catalog" in="CatalogItems">
<div class="esh-catalog-item col-md-4">
<partial name="_product" for="@catalog" />
</div>
</foreach>
</div>
<partial name="_pagination" for="CatalogModel.PaginationInfo"/>
</then>
<elseif condition="X>3">
<!--we can have elseif too-->
</elseif>
<else>
<div class="esh-catalog-items row">
THERE ARE NO RESULTS THAT MATCH YOUR SEARCH
</div>
</else>
</if>
</div>
Which can is translated to this C# Razor:
@page
@model CatalogModel
@{
ViewData["Title"] = "zml sample";
ViewData["Message"] = "another syntax";
}
<div class="container">
@if (@Model.CatalogModel.CatalogItems.Any())
{
<partial name="_pagination" for="CatalogModel.PaginationInfo"/>
<div class="esh-catalog-items row">
@foreach (var catalog in @Model.CatalogItems)
{
<div class="esh-catalog-item col-md-4">
<partial name="_product" for="@catalog" />
</div>
}
</div>
<partial name="_pagination" for="@Model.CatalogModel.PaginationInfo"/>
}
elseif (X>3)
{
<!--we can have elseif too-->
}
else
{
<div class="esh-catalog-items row">
THERE ARE NO RESULTS THAT MATCH YOUR SEARCH
</div>
}
</div>
Vazor contains a working simple sample, the index.vbxml.vb file contains this ZML block
<ul>
<foreach var="m" in="Model">
<li>
Id: @m.Id<br/>
Name: @m.Name<br/>
<p>Grade: @m.Grade</p>
</li>
</foreach>
</ul>
Which generates this C# Razor:
<ul>
@foreach (var m in Model)
{
<li>
Id : @m.Id<br />
Name: @m.Name<br />
<p>Grade: @m.Grade</p>
</li>
}
</ul>
Note:
I can’t create ZML editor myself. It is easier for you to modify the Razor editor to deal with ZML tags. If this to happen, I expect there will be no need to use the @ inside the ZML attributes, nor the Model keyword in most cases. The editor can also allow the use of <T>
for generics. I advise to allow using (Of T) or <T>, and ((i)
or [i]
notations, to let VB and C# developers write the syntax they are familiar with.
Conclusion:
ZML is an easy to write, read and learn code, because it is an XML code but very close to C# and VB.NET code. It can add a great value to VS.NET with low cost, and will give VB.NET and F# developers the ability to design web pages with homogeneous XML code. C# programmers also can find this ZML views attractive.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:9 (4 by maintainers)
Hi, we are closing this issue because there are no plans to add additional view engines to ASP.NET Core at this time.
We encourage you to work on this as a community project.
ZML 1.0 now has its own repo and NuGet.