提交 221e37d6 编写于 作者: JasonWcx's avatar JasonWcx

开始实现模板引擎

上级 684dd61b
......@@ -31,28 +31,28 @@
<div>${goods.Price}</div>
<%end%>
<!--if conditional-->
<% if page==1 %>
<div>if condition</div>
<% else if %>
<div>else if condition</div>
<% end %>
<!--if conditional-->
<% if page==1 %>
<div>if condition</div>
<% else if %>
<div>else if condition</div>
<% end %>
<!--switch statement-->
<% switch(pagecount)%>
<% case %>
<!--switch statement-->
<% switch(pagecount)%>
<% case %>
<% break %>
<% end %>
<% break %>
<% end %>
<!--Math function-->
<p>
<div>$math.abs(-1),$math.ceil(1.2)</div>
</p>
<!--Math function-->
<p>
<div>$math.abs(-1),$math.ceil(1.2)</div>
</p>
<!--format function-->
<p>
<div>$format("{0},{1}",Title,Content)</div>
</p>
<!--format function-->
<p>
<div>$format("{0},{1}",Title,Content)</div>
</p>
</body>
</html>
\ No newline at end of file
......@@ -24,6 +24,6 @@
<p>能看到此页面,证明服务器已正常启动,配置一个默认首页就能使此页面不再显示。服务器使用说明请参看源代码和相关文档。</p>
<p>意见建议请联系软件作者,或提交至Issue。</p>
<hr style="border: 0; border-bottom: 1px solid #dedede;" />
<p><em>版本:v${Info.VersionName}</em></p>
<p><em>$format("版本:v{0}",Info.VersionName)</em></p>
</body>
</html>
\ No newline at end of file
......@@ -262,7 +262,7 @@ namespace Mozi.HttpEmbedded
string doc = DocLoader.Load("Error.html");
PageEngine pc = new PageEngine();
pc.LoadFromText(doc);
pc.SetParameter("Error", new
pc.Set("Error", new
{
Code = StatusCode.InternalServerError.Code.ToString(),
Title = StatusCode.InternalServerError.Text,
......@@ -363,7 +363,7 @@ namespace Mozi.HttpEmbedded
var doc = DocLoader.Load("Home.html");
PageEngine pc = new PageEngine();
pc.LoadFromText(doc);
pc.SetParameter("Info", new
pc.Set("Info", new
{
VersionName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
});
......
......@@ -5,6 +5,6 @@
/// </summary>
public interface IMiddleware
{
void Invoke();
}
}
......@@ -105,7 +105,7 @@
<Compile Include="HttpVersion.cs" />
<Compile Include="Common\Log.cs" />
<Compile Include="Page\BaseApi.cs" />
<Compile Include="Page\AbsPage.cs" />
<Compile Include="Page\BasePage.cs" />
<Compile Include="Serialize\DataSerializeType.cs" />
<Compile Include="Cache\Global.cs" />
<Compile Include="Template\PageEngine.cs" />
......@@ -170,7 +170,7 @@
<ItemGroup>
<EmbeddedResource Include="Document\Home.html" />
<EmbeddedResource Include="Document\Error.html" />
<EmbeddedResource Include="Document\ExamplePage.html" />
<EmbeddedResource Include="Document\Example.html" />
<Content Include="Source\mime.data" />
<Content Include="WebDav\Icon.png" />
</ItemGroup>
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="791px" height="498px" viewBox="-0.5 -0.5 791 498" content="&lt;mxfile host=&quot;app.diagrams.net&quot; modified=&quot;2021-07-10T10:05:01.234Z&quot; agent=&quot;5.0 (Windows)&quot; etag=&quot;zMLJLe5Bo3q3GYsHUGHe&quot; version=&quot;14.8.5&quot; type=&quot;device&quot;&gt;&lt;diagram id=&quot;0SU5S7vlTBd-NNq4Iu0A&quot; name=&quot;Page-1&quot;&gt;3VpLc6M4EP41PiaFJBD2MU4ymdqa3UpNDjtz2lJAAXYwYoQc2/n1K4xkEPIzAePsxWU1enZ//Und0gjdzpYPnOTxnyyk6Qg64XKE7kYQTlwof0vBqhJgACpBxJOwEjUET8kbVUJHSedJSAujomAsFUluCgOWZTQQhoxwzhZmtReWmqPmJKKW4CkgqS39OwlFXEnH0K/lX2kSxXpkgCfVlxnRldVKipiEbNEQofsRuuWMierfbHlL01J3Wi9Vuy87vm4mxmkmjmnweD/9469/HpzIf/09K+By/ibcK4TV5MRKr5iGUgGqyLiIWcQykt7X0iln8yykZbeOLNV1vjGWSyGQwn+pECtlTTIXTIpiMUvVVzljvvqh2q8LP8vCtaeLd8vmx7uVLi0T8UP3If83WslS3ags6DbV+spF7dSbEhVszgO6T1kKf4RHVOypV3WorPBA2YzKRciGnKZEJK/mRIgCaLSpt2n6yBI5RegoX0KeAt1Klx2zi2oBqlWNBPmnMY1atMbHCVjx3EGwomzuGDb39xt9AzBgAsw/hLAB0IL7QQtwHRMtyDO7qCZmoeWGc7JqVMvLCsXucaDTGmfimTR0Wn35p5pBp9BVtngl6Vzp84kFv6h4ovyVcgvWJmgXcSLoU07W1l7IXc4EqOqackGX+0Fj21jrxDd1ArEqL+otB2i9xc3tZtyigCYuDP8/VWN6BkNtDPVe8NNw1N7dFuB+yNvxt5u4Y3fEoDUOaJ0KDs3LrN+POwJs+6OQ2g2+JCktBndHT1HSRie+Z7sj3OKObZt25o0esJRyXm/0B3NHvxd39PVhRocl7nHu2JkH+JYH3OTJ4MjHE1MtHhwa+e54YOTDwZA/7gX53mRg5I8t5D+WMfjQ0PfQpUEfDU36JyF/XXqkPJGLl0fqzt1Bq/5gPIWc7XY+2m8+ZDQ9zQa8v7O5uIAgw5tgA+AIDw7wQRMKZhIJHplQODFj1aUD6PTnIQf4cATzMaN6l2PUY7NEn8CocFhWAxarfRUi/05/z2khLIMfIDPT2h1Qm9sOerfmT5BNbf3lTxD8VJv3OxLrZeH9+/1hFusp8HPKdTXB4vpjs5MdafRTMzHtEFOP02tmxTsuriwt9I0809SED0mTKJP/A2mt0qbT0iuTgKQ36sMsCcMKqbRI3sjzur/S8EoZsnNvOvLu9rm1uqlTjUeb+7EmSPa41E4SuHKuHaSvG5XOr9SYH42ZTHZptWcvLwXt58Zl6OD3XSTiXBCJ9BNDYwjORCLtaP08JDKxSGRriP4pWcTbAQnVvSQRx0MmiaBOSMTsFKBzkQiyI9LKmlKOU6mq6bMMTnEk1mwhxds2jEJw9mvzAgKaBi9ikpf1ZsuofANy/ZKyRRATLq5DGiRFwrIdAW4HRz8M/LYravM1Dn/+xD77tW/Pu0vWo0FYu8uwFp4xBMLHkjnqhcvb9+Rw3Lon74jJQeuOFzn778kP1O+J+S8qagHnA6E76QVc7dtV98zJdtfeyau4vchZVlxA0h2YIN9cRjS4G501J4ntTMe0fEV4AQncNlFdwCMR11KWfabgu8AmNSH6OzmWNFNUjLM+o6wPMLcsZWWYkbGMdmMUgM3Tx7asur/FKKg3m9hvK/iONN3/wwDtt1NjbBkAbzEAPN0Aslg/160oun7zjO7/Aw==&lt;/diagram&gt;&lt;/mxfile&gt;"><defs/><g><path d="M 330 147 L 370.63 147" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 375.88 147 L 368.88 150.5 L 370.63 147 L 368.88 143.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 230 192 L 160 192 L 106.37 192" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 101.12 192 L 108.12 188.5 L 106.37 192 L 108.12 195.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="230" y="57" width="100" height="180" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 98px; height: 1px; padding-top: 147px; margin-left: 231px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">SocketServer</div></div></div></foreignObject><text x="280" y="151" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">SocketServer</text></switch></g><path d="M 577 72 L 577 7 L 267 7 L 267 50.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 267 55.88 L 263.5 48.88 L 267 50.63 L 270.5 48.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="555" y="72" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 102px; margin-left: 556px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">StaticFiles</div></div></div></foreignObject><text x="615" y="106" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">StaticFiles</text></switch></g><path d="M 714 382 L 714 430.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 714 435.88 L 710.5 428.88 L 714 430.63 L 717.5 428.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="654" y="322" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 352px; margin-left: 655px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Api</div></div></div></foreignObject><text x="714" y="356" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Api</text></switch></g><path d="M 554 382 L 554 430.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 554 435.88 L 550.5 428.88 L 554 430.63 L 557.5 428.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="494" y="322" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 352px; margin-left: 495px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Page</div></div></div></foreignObject><text x="554" y="356" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Page</text></switch></g><path d="M 616 222 L 616 242 L 617 242 L 617 243.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 617 248.88 L 613.5 241.88 L 617 243.63 L 620.5 241.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="556" y="162" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 192px; margin-left: 557px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Router</div></div></div></foreignObject><text x="616" y="196" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Router</text></switch></g><path d="M 480 102 L 548.63 102" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 553.88 102 L 546.88 105.5 L 548.63 102 L 546.88 98.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 480 192 L 549.63 192" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 554.88 192 L 547.88 195.5 L 549.63 192 L 547.88 188.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="377" y="57" width="103" height="180" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 101px; height: 1px; padding-top: 147px; margin-left: 378px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">HttpRequest</div></div></div></foreignObject><text x="429" y="151" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">HttpRequest</text></switch></g><path d="M 660.5 275 L 714 275 L 714 315.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 714 320.88 L 710.5 313.88 L 714 315.63 L 717.5 313.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 298px; margin-left: 715px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 11px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; background-color: #ffffff; white-space: nowrap; ">Api</div></div></div></foreignObject><text x="715" y="301" fill="#000000" font-family="Helvetica" font-size="11px" text-anchor="middle">Api</text></switch></g><path d="M 581.5 275 L 554 275 L 554 315.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 554 320.88 L 550.5 313.88 L 554 315.63 L 557.5 313.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 298px; margin-left: 555px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 11px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; background-color: #ffffff; white-space: nowrap; ">Page</div></div></div></foreignObject><text x="555" y="301" fill="#000000" font-family="Helvetica" font-size="11px" text-anchor="middle">Page</text></switch></g><path d="M 617 250 L 656.5 275 L 617 300 L 577.5 275 Z" fill="#ffffff" stroke="#000000" stroke-width="2" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 77px; height: 1px; padding-top: 275px; margin-left: 579px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Page <br />or Api</div></div></div></foreignObject><text x="617" y="279" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Page...</text></switch></g><path d="M 100 102 L 130 102 L 223.63 102" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 228.88 102 L 221.88 105.5 L 223.63 102 L 221.88 98.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 470 467 L 267 467 L 267 243.37" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 267 238.12 L 270.5 245.12 L 267 243.37 L 263.5 245.12 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="470" y="437" width="320" height="60" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 318px; height: 1px; padding-top: 467px; margin-left: 471px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">HttpResponse</div></div></div></foreignObject><text x="630" y="471" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">HttpResponse</text></switch></g><rect x="0" y="57" width="100" height="180" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 98px; height: 1px; padding-top: 147px; margin-left: 1px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Browser</div></div></div></foreignObject><text x="50" y="151" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Browser</text></switch></g><rect x="125" y="162" width="70" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 177px; margin-left: 160px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; "><br />response</div></div></div></foreignObject><text x="160" y="181" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">&#xa;response</text></switch></g><rect x="130" y="83" width="60" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 93px; margin-left: 160px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; ">request</div></div></div></foreignObject><text x="160" y="97" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">request</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Viewer does not support full SVG 1.1</text></a></switch></svg>
\ No newline at end of file
......@@ -3,7 +3,7 @@
/// <summary>
/// 页面抽象类
/// </summary>
public abstract class AbsPage
public abstract class BasePage
{
protected HttpContext Context { get; set; }
/// <summary>
......
......@@ -8,8 +8,6 @@ using System.Text.RegularExpressions;
namespace Mozi.HttpEmbedded.Template
{
//TODO 2021/07/06 考虑将这个模板引擎单独剥离成一个项目
//TODO 需要实现一个模板引擎或考虑通过Razor引擎提高通用性
//2021/06/08 文档模板仅仅实现类建议参数注入功能
/// <summary>
/// 页面生成器
/// </summary>
......@@ -36,9 +34,13 @@ namespace Mozi.HttpEmbedded.Template
internal PageEngine LoadFromStream(Stream stream)
{
Prepare();
return this;
}
/// <summary>
/// 从文本载入模板
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
public PageEngine LoadFromText(string template)
{
_template = template;
......@@ -47,8 +49,23 @@ namespace Mozi.HttpEmbedded.Template
public PageEngine Prepare()
{
_page = _template;
ApplyAll();
return this;
}
/// <summary>
/// 应用所有规则
/// </summary>
/// <returns></returns>
private PageEngine ApplyAll()
{
//首先解析语句
//填充全局变量
InflateGlobal();
//填充变量
InflateValues();
//填充format指令执行结果
InflateExpressionFormat();
return this;
}
/// <summary>
......@@ -70,18 +87,10 @@ namespace Mozi.HttpEmbedded.Template
foreach (var m in matchesParam)
{
var param = m.ToString().Trim(new char[] { '$', '{', '}' });
if (!param.Contains("."))
var paramValue = GetPatternValue(param);
if (paramValue != null)
{
_page = _page.Replace(m.ToString(), GetParameter(param).ToString());
}
else
{
string[] target = param.Split(new char[] { '.' });
object pValue = _params[target[0]];
PropertyInfo props = pValue.GetType().GetProperty(target[1], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
var targetValue = props.GetValue(pValue, null);
_page = _page.Replace(m.ToString(), targetValue.ToString());
_page = _page.Replace(m.ToString(), paramValue);
}
}
return this;
......@@ -106,13 +115,61 @@ namespace Mozi.HttpEmbedded.Template
{
return this;
}
private string GetPatternValue(string pattern)
{
string result =null;
if (!pattern.Contains("."))
{
result=Get(pattern).ToString();
}
else
{
string[] target = pattern.Split(new char[] { '.' });
object pValue = _params[target[0]];
private PageEngine ParseExpressionMath()
PropertyInfo props = pValue.GetType().GetProperty(target[1], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
var targetValue = props.GetValue(pValue, null);
result=targetValue.ToString();
}
return result;
}
private PageEngine InflateExpressionMath()
{
Regex regParam = new Regex("\\$math\\.\\d+\\(.*\\)");
MatchCollection matchesParam = regParam.Matches(_page);
foreach (var m in matchesParam)
{
var pattern = m.ToString().Trim(new char[] { '$', '{', '}' });
var paramValue = GetPatternValue(pattern);
if (paramValue != null)
{
_page = _page.Replace(m.ToString(), paramValue);
}
}
return this;
}
private PageEngine ParseExpressionFormat()
/// <summary>
/// 应用函数format
/// </summary>
/// <returns></returns>
private PageEngine InflateExpressionFormat()
{
Regex regParam = new Regex("\\$format\\(.*\\)");
MatchCollection matchesParam = regParam.Matches(_page);
foreach (var m in matchesParam)
{
var pattern = m.ToString().Replace("$format", "").Trim(new char[] { '(', ')' });
var splitIndex = pattern.IndexOf("\",");
var format = pattern.Substring(1, splitIndex-1);
var paramexp = pattern.Substring(splitIndex + 2);
var pms = paramexp.Split(new char[] { ',' });
var pmvs = new object[pms.Length];
for (int i = 0; i < pms.Length; i++)
{
pmvs[i] = GetPatternValue(pms[i]);
}
_page = _page.Replace(m.ToString(),string.Format(format, pmvs));
}
return this;
}
/// <summary>
......@@ -138,7 +195,7 @@ namespace Mozi.HttpEmbedded.Template
/// </summary>
/// <param name="paramName"></param>
/// <returns></returns>
public object GetParameter(string paramName)
public object Get(string paramName)
{
if (_params.ContainsKey(paramName))
{
......@@ -155,7 +212,7 @@ namespace Mozi.HttpEmbedded.Template
/// <param name="paramName">参数名</param>
/// <param name="paramValue">参数值</param>
/// <returns></returns>
public PageEngine SetParameter(string paramName, object paramValue)
public PageEngine Set(string paramName, object paramValue)
{
if (_params.ContainsKey(paramName))
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册