模板生成静态页面,模板引擎

    开发网站需要用到模板引擎,自己写了一个。支持foreach嵌套,支持if else嵌套,支持foreach和if else混合嵌套,支持include,支持set,支持cutstr和html2text函数。

1,HTML的Form表单数据按Button提交数据以后,由 Action 指定的服务器端处理程序(.ashx)进行处理后 ,再响应的浏览器。

Project name :Flask_Plan

html模板示例代码如下:

2,我们把 HTML的表单,写到 .ashx 一般处理程序页面中,这样就一般处理程序页面就可以显示 Form表单登陆,并且可以处理是否登陆成功的逻辑部分,

templates:templates

index.html页面:

 也就是把前台显示和后天业务逻辑拼到了一起,如下:

static:static

图片 1图片 2

 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            //渲染一个Form表单
            context.Response.Write("<html>");
            context.Response.Write("<head>一般处理程序登陆页面</head><body><form  action='login.ashx' action='get'><br/><br/>"); //
            context.Response.Write("用户名:<input type='text' name='UserName' /><br/>");
            context.Response.Write("密码:<input type='password' name='Pwd' /><br/>");
            context.Response.Write("提交:<input type='submit' /><br/>");

            string strUserName = context.Request.QueryString["UserName"];
            string strPwd = context.Request.QueryString["Pwd"];
            if (string.IsNullOrEmpty(strUserName) == false)  //登陆名不为空的时候,检查密码是否正确
            {
                if (strUserName == "admin" && strPwd == "123")
                {
                    context.Response.Write("恭喜您,登陆成功");
                }
                else
                {
                    context.Response.Write("登陆失败");
                }
            }
        }

继续上篇的模板

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta content="width=device-width, minimum-scale=1,initial-scale=1, maximum-scale=1, user-scalable=1;" id="viewport" name="viewport" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="keywords" content="${site.keywords}" />
    <meta name="description" content="${site.description}" />
    <link href="theme/css/Int.css" rel="stylesheet" />
    <link href="theme/css/head.css" rel="stylesheet" />
    <link href="theme/css/index.css" rel="stylesheet" />
    <link href="theme/css/lrtk.css" rel="stylesheet" />
    <link href="theme/css/more.css" rel="stylesheet" />
    <script type="text/javascript" src="theme/js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="theme/js/layout.js"></script>
    <script type="text/javascript" src="theme/js/more.js"></script>
    <script src="theme/js/jquery.superslide.2.1.1.js" type="text/javascript"></script>
    <title>首页-${site.siteName}</title>
    <script type="text/javascript">

    </script>
</head>
<body>
    <!--头部开始-->
    <div class="header">
        <div class="w">
            <div class="fl logo">
                <a href="/">
                    <img src="${site.logoUrl}">
                </a>
            </div>
            <div class="sj_language">
                <#if ${site.id}=="1">
                <a href="/en/index.html"><em class="china" style="background: url(themeng.jpg) left center no-repeat;">English</em><i style="background: url(themehina.jpg) left center no-repeat;">中文版</i></a>
                <#else>
                <a href="/cn/index.html"><em class="china">中文版</em><i>English</i></a>
                <#end if>
            </div>
            <!--主菜单-->
            <#include src="/templates/${site.folder}/rootmenu.html" />
            <div class="language">
                <#if ${site.id}=="1">
                <a href="/en/index.html">English<i style="background: url(themehina.jpg) left center no-repeat;">中文版</i></a>
                <#else>
                <a href="/cn/index.html">中文版<i>English</i></a>
                <#end if>
            </div>
            <div class="clear"></div>
        </div>
        <div class="clear"></div>
        <!--子菜单-->
        <#include src="/templates/${site.folder}/submenu.html" />
        <!--头部结束-->
        <div class="SimpoAll">
            <!-- 新的轮播图开始 -->
            <script type="text/javascript">
                var scrollImg;
            </script>
            <div class="mrbox">

                <div class="imgbox">
                    <ul>
                        <#foreach list="banners" model="ban" where="siteId=1" >
                        <li><a href="${ban.linkUrl}" target="_blank">
                            <img src="${ban.imgUrl}" /></a></li>
                        <#/foreach>
                    </ul>
                </div>

            </div>
            <script src="theme/js/slider-H5.js"></script>
            <script type="text/javascript">
                (function () {


                    $(window).resize(function () {
                        var ul = $('.imgbox').clone().html();
                        $('.imgbox').html("");
                        var id = (new Date()).getTime();
                        $(ul).attr('id', id).appendTo($('.imgbox'));

                        var height = $('body').height();
                        var width = $('body').width();
                        var newwidth = $('.imgbox').width();
                        $('.imgbox').css('width', width);
                        $('.imgbox ul li').css('width', width);
                        $('.imgbox ul li img').css('width', width);
                        /*
                            注意:$.mggScrollImg返回的scrollImg对象上有
                                    next,prev,go三个方法,可以实现外部对滚动索引的控制。
                            如:scrollImg.next();//会切换到下一张图片
                                scrollImg.go(0);//会切换到第一张图片
                        */
                        scrollImg = $.mggScrollImg('#'   id, {
                            loop: true,//循环切换
                            auto: true,//自动切换
                            auto_wait_time: 3000,//轮播间隔
                            scroll_time: 300,//滚动时长
                            callback: function (ind) {//这里传过来的是索引值
                                $('#page').text(ind   1);
                            }
                        });

                    });
                    $(window).resize();
                })()
            </script>

            <!-- 新的轮播图结束 -->

            <div class="clear"></div>
            <!--关于我们开始-->
            <div class="about_bg">
                <div class="about">
                    <#set data="channel" model="gywm" where="title='关于我们'" />
                    <#set data="channel" model="qyjj" where="title='企业简介'" />
                    <h1 class="biaot"><a href="/${site.folder}/list${qyjj.id}.html">${gywm.title}</a></h1>
                    <#foreach list="channel" model="subMenu" where="parent.Title='关于我们'"  >
                    <dl>
                        <dt><a href="/${site.folder}/list${subMenu.id}.html">
                            <img src="${subMenu.iconUrl}" /></a></dt>
                        <dd><a href="/${site.folder}/list${subMenu.id}.html">${subMenu.title}</a></dd>
                    </dl>
                    <#/foreach>
                </div>
            </div>
            <!--关于我们结束-->


            <!--业务单元开始-->
            <#set data="channel" model="ywdy" where="title='业务单元'" />
            <div class="Business_bg">
                <a href="#">
                    <div class="Business">
                        <h1 class="biaot">${ywdy.title}</h1>
                        <div class="yw_pic">
                            <img src="themeyw.png" />
                        </div>
                    </div>
                </a>
            </div>
            <!--业务单元结束-->

            <!--投资者关系开始-->
            <div class="investor_bg">
                <div class="investor">
                    <#set data="channel" model="tzzgx" where="title='投资者关系'" />
                    <#set data="channel" model="cwbg" where="title='财务报告'" />
                    <h1 class="biaot"><a href="/${site.folder}/list${cwbg.id}.html">投资者关系</a></h1>
                    <#foreach list="content" model="con" where="channel.title='投资者关系'" page="1" pageSize="1"  >
                    <p>${cutstr(${con.contents},315)}...<a href="/${site.folder}/content${con.id}.html">【详情】</a></p>
                    <#/foreach>
                    <#foreach list="channel" model="subMenu" where="parent.Title='投资者关系'"  >
                    <dl>
                        <dt><a href="/${site.folder}/list${subMenu.id}.html">
                            <img src="${subMenu.iconUrl}" /></a></dt>
                        <dd><a href="/${site.folder}/list${subMenu.id}.html">${subMenu.title}</a></dd>
                    </dl>
                    <#/foreach>
                </div>
            </div>
            <!--投资者关系结束-->

            <!--新闻综合类开始-->
            <div class="information">



                <!--新闻资讯开始-->
                <div class="news">
                    <#set data="channel" model="xwzx" where="title='新闻中心'" />
                    <#set data="channel" model="mtjj" where="title='媒体聚焦'" />
                    <h1><a href="/${site.folder}/list${mtjj.id}.html">${xwzx.title}</a></h1>
                    <a class="current news1"></a><a class="news2"></a>
                    <div class="newbox1">
                        <#foreach list="content" model="con" where="channel.title='媒体聚焦'" page="1" pageSize="3"  >
                        <dl>
                            <dt><a href="/${site.folder}/content${con.id}.html">
                                <img src="${con.imgUrl}" /></a></dt>
                            <dd>
                                <h2><a href="/${site.folder}/content${con.id}.html">${con.title}</a></h2>
                                <label>${con.publishTime}</label>
                                <p>${cutstr(${con.contents},90)}...<a href="/${site.folder}/content${con.id}.html">【详情】</a></p>
                            </dd>
                        </dl>
                        <#/foreach>
                    </div>

                    <div class="newbox2" style="display: none">
                        <#foreach list="content" model="con" where="channel.title='最新公告'" page="1" pageSize="3"  >
                        <dl>
                            <dt><a href="/${site.folder}/content${con.id}.html">
                                <img src="${con.imgUrl}" /></a></dt>
                            <dd>
                                <h2><a href="/${site.folder}/content${con.id}.html">${con.title}</a></h2>
                                <label>${con.publishTime}</label>
                                <p>${cutstr(${con.contents},90)}...<a href="/${site.folder}/content${con.id}.html">【详情】</a></p>
                            </dd>
                        </dl>
                        <#/foreach>
                    </div>

                </div>
                <!--新闻资讯结束-->



                <!--社会责任开始-->
                <div class="responsibility">
                    <#set data="channel" model="shzr" where="title='社会责任'" />
                    <h1><a href="/${site.folder}/list${shzr.id}.html">${shzr.title}</a></h1>
                    <div class="zeren">
                        <div class="border"></div>
                        <div class="zr_list">
                            <ul>
                                <#foreach list="content" model="con" where="channel.title='社会责任'" page="1" pageSize="5"  >
                                <li><#if ${con.curPageSort}==1>
                                    <button class="btn_on btn" type="button"></button>
                                    <#else>
                                    <button class="btn" type="button"></button>
                                    <#end if>
                                    <a href="/${site.folder}/content${con.id}.html">${con.title}</a>

                                    <div class="neir">
                                        <p>${cutstr(${con.contents},60)}...<a href="/${site.folder}/content${con.id}.html">【详情】</a></p>
                                    </div>
                                </li>
                                <#/foreach>
                            </ul>

                        </div>
                    </div>
                </div>
                <!--社会责任结束-->

            </div>
            <!--新闻综合类结束-->

            <!--总裁寄语开始-->
            <div class="President_bg">
                <div class="President">
                    <#set data="channel" model="zcjy" where="title='总裁寄语'" />
                    <h1 class="biaot"><a href="#">${zcjy.title}</a></h1>
                    <div class="Message_box">
                        <div class="Message">
                            <#foreach list="content" model="con" where="channel.title='总裁寄语'" page="1" pageSize="3"  >
                            <#if ${con.curPageSort}==1>
                             <div class="j${con.curPageSort}">
                                 <#else>
                             <div class="j${con.curPageSort}" style="display: none;">
                                 <#end if>

                                ${con.contents}
                             </div>
                                 <#/foreach>
                            <div class="qh">
                                <a class="y1 qh_on"></a>
                                <a class="y2"></a>
                                <a class="y3"></a>
                            </div>
                             </div>
                        </div>
                    </div>
                </div>
                <!--总裁寄语结束-->
                <!--合作伙伴开始-->
                <div class="Partner_bg">
                    <div class="Partner">
                        <h3>合作伙伴</h3>
                        <#foreach list="links" model="lk" where="type=2 and siteId=1" >
                        <a href="${lk.linkUrl}" target="_blank">${lk.title}</a>
                        <#/foreach>
                    </div>
                </div>
                <!--合作伙伴结束-->

                <!--底部开始-->
                <div class="cai_bg"></div>
                <div class="Web_Icon"></div>
                <!--底部结束-->
                <div class="copyright_bottom">
                    ${site.copyright}<br />
                    备案号:${site.siteRecord}
                </div>
                <div style="display: none;">
                    ${site.statScript}
                </div>
</body>
</html>

  但是这样去做,网页表单样式等 只有通过这种修改 .ashx一般处理程序的方法去修改。 

我们已经可以静态调用模板,包括继承模板,保证了页面的一致性,但是我们查询的东西,页面还都是在html文件中的。

View Code

  没法给前台美工可以处理的地方,另外如果后天业务逻辑复杂的话,这样的整合就更复杂了。

渲染,就可以让我们把动态的内容,放进静态的模板中。

rootmenu.html页面:

  如果有一个文件 又可以区分前台显示和后台业务逻辑 这样就好了。

修改templates/plan.html 

图片 3图片 4

3,nVelocity是一个基于.NET的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象

图片 5图片 6

<div class="pad-nav">

    <ul>
        <!--主菜单-->
        <#foreach list="channel" model="rootMenu" where="channel.siteId=${site.id} and channel.level=1"  >
        <li><#if ${rootMenu.sort}=="1">
                <#if ${channel.id}==0>
                <a href="index.html" class="cpselect" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                <#else>
                <a href="index.html" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                <#end if>
            <#else>
                <#if ${rootMenu.listType}=="5">
                    <#if ${rootMenu.id}=="${parentChannel.id}">
                    <a href="#" class="cpselect" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#else>
                    <a href="#" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#end if>
                <#else>
                    <#if ${rootMenu.id}=="${parentChannel.id}">
                    <a href="/${site.folder}/list${rootMenu.id}.html" class="cpselect" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#else>
                    <a href="/${site.folder}/list${rootMenu.id}.html" onclick="showPhoneSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#end if>
                <#end if>
            <#end if>
        </li>
        <#/foreach>
    </ul>
    <!--子菜单-->
    <#foreach list="channel" model="rootMenu" where="channel.siteId=${site.id} and channel.level=1"  >
    <div class="two_nav" phone-menu-title="${rootMenu.title}" style="display: none;">
        <ul>
            <#foreach list="channel" model="subMenu" where="parent.Title='${rootMenu.title}'"  >
            <li><a href="/${site.folder}/list${subMenu.id}.html">${subMenu.title}</a></li>
            <#/foreach>
        </ul>
    </div>
    <#/foreach>
</div>
<div class="fl nav">
    <ul>
        <!--主菜单-->
        <#foreach list="channel" model="rootMenu" where="channel.siteId=${site.id} and channel.level=1"  >
        <li>
            <#if ${rootMenu.sort}=="1">
                <#if ${channel.id}==0>
                <a href="index.html" class="cur">${rootMenu.title}</a>
                <#else>
                <a href="index.html" class="smen">${rootMenu.title}</a>
                <#end if> 
            <#else>
                <#if ${rootMenu.listType}=="5">
                    <#if ${rootMenu.id}=="${parentChannel.id}">
                    <a href="#" class="cur" onclick="showSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#else>
                    <a href="#" class="smen" onclick="showSubMenu('${rootMenu.title}')">${rootMenu.title}</a>
                    <#end if>
                <#else>
                    <#if ${parentChannel.id}==0>
                        <#if ${rootMenu.id}=="${channel.id}">
                        <a href="/${site.folder}/list${rootMenu.id}.html" class="cur">${rootMenu.title}</a>
                        <#else>
                        <a href="/${site.folder}/list${rootMenu.id}.html" class="smen">${rootMenu.title}</a>
                        <#end if>
                    <#else>
                        <#if ${rootMenu.id}=="${parentChannel.id}">
                        <a href="/${site.folder}/list${rootMenu.id}.html" class="cur">${rootMenu.title}</a>
                        <#else>
                        <a href="/${site.folder}/list${rootMenu.id}.html" class="smen">${rootMenu.title}</a>
                        <#end if>
                    <#end if>
                <#end if>
            <#end if>
        </li>
        <#/foreach>
    </ul>
</div>

4,nVelocity,下载网址:NVelocity - A .Net Template Engine ,下载后把 dll文件放到项目中,然后 引用添加对dll的引用,注意对应的 .NET Framework版本

{%  extends 'base.html' %}
{% block main1 %}
    <h1>这是main1提交的新内容{{ name1 }}</h1>
{% endblock %}
{% block main2 %}
    <h3>这是main2提交的新内容{{ name2 }}</h3>
{% endblock %}

View Code

图片 7

View Code

list.html页面:

 

修改Flask_Plan.py

图片 8图片 9

 

@app.route('/')
def hello_world():
    return render_template('plan.html',name1='测试name1的渲染',name2 ='name2渲染')   
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta content="width=device-width, minimum-scale=1,initial-scale=1, maximum-scale=1, user-scalable=1;" id="viewport" name="viewport" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="keywords" content="${site.keywords}" />
    <meta name="description" content="${site.description}" />
    <link href="theme/css/Int.css" rel="stylesheet" />
    <link href="theme/css/head.css" rel="stylesheet" />
    <link href="theme/css/index.css" rel="stylesheet" />
    <link href="theme/css/more.css" rel="stylesheet" />
    <script type="text/javascript" src="theme/js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="theme/js/layout.js"></script>
    <script type="text/javascript" src="theme/js/more.js"></script>
    <!--<script src="js/jquery.superslide.2.1.1.js" type="text/javascript"></script> PC端轮播图JS -->
    <title>${channel.title}-${parentChannel.title}-${site.siteName}</title>
    <script type="text/javascript">

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            var topMain = $(".nav_part").height()//对象的的高度
            var nav = $(".nav_part");
            $(window).scroll(function () {
                if ($(window).scrollTop() > topMain) {//如果滚动条顶部的距离大于topMain则就nav导航就添加类.nav_scroll,否则就移除。
                    nav.addClass("nav_scroll");
                }
                else {
                    nav.removeClass("nav_scroll");
                }
            });

        })
    </script>
</head>
<body>
    <!--头部开始-->
    <div class="header">
        <div class="w">
            <div class="fl logo">
                <a href="index.html">
                    <img src="themelogo.png">
                </a>
            </div>
            <div class="sj_language">
                <#if ${site.id}=="1">
                <a href="/en/index.html"><em class="china" style="background: url(themeng.jpg) left center no-repeat;">English</em><i style="background: url(themehina.jpg) left center no-repeat;">中文版</i></a>
                <#else>
                <a href="/cn/index.html"><em class="china">中文版</em><i>English</i></a>
                <#end if>
            </div>
            <!--主菜单-->
            <#include src="/templates/${site.folder}/rootmenu.html" />
            <div class="language">
                <#if ${site.id}=="1">
                <a href="/en/index.html">English<i style="background: url(themehina.jpg) left center no-repeat;">中文版</i></a>
                <#else>
                <a href="/cn/index.html">中文版<i>English</i></a>
                <#end if>
            </div>
            <div class="clear"></div>
        </div>
        <div class="clear"></div>
        <!--子菜单-->
        <#include src="/templates/${site.folder}/submenu.html" />

        <!--头部结束-->
        <!--广告图开始-->
        <div class="gg_pic">
            <img src="themejbg.jpg" />
        </div>
        <!--广告图结束-->
        <!--内容区域开始-->
        <div class="Main_part">
            <!--左侧导航开始-->
            <#include src="/templates/${site.folder}/leftmenu.html" />
            <!--左侧导航结束-->
            <!--右侧内容开始-->
            <div class="nr_part">
                <div class="position">
                    ${channel.title}
                    当前位置:<a href="#"><#if ${parentChannel.id}!=0>${parentChannel.title}><#end if>${channel.title}</a>
                </div>
                <div class="Media_Focus">
                    <h1 class="jujiao">
                        <del>
                            <img alt="图片 10" src="${channel.iconUrl}" /></del>
                        ${channel.title}</h1>
                    <ul>
                        <#foreach main="true" list="content" model="con" channel="${channel.id}" page="${pager.page}" pageSize="10"  >
                        <li>
                            <label>[${channel.title}]</label><a href="/${site.folder}/content${con.id}.html">${con.title}</a>${con.publishTime}</li>
                        <#/foreach>
                    </ul>
                    <div class="pag">
                        <#if ${pager.prePage}==1>
                        <a href="list${channel.id}.html" onclick="">上一页</a>
                        <#else>
                        <a href="list${channel.id}-${pager.prePage}.html" onclick="">上一页</a>
                        <#end if>
                        ${pager.page} / ${pager.pageCount}
                        <#if ${pager.nextPage}==1>
                        <a href="list${channel.id}.html">下一页</a>
                        <#else>
                        <a href="list${channel.id}-${pager.nextPage}.html">下一页</a>
                        <#end if>
                    </div>
                </div>
            </div>
            <!--右侧内容结束-->

        </div>
        <!--内容结束-->


        <!--合作伙伴开始-->
        <div class="Partner_bg">
            <div class="Partner">
                <h3>合作伙伴</h3>
                <#foreach list="links" model="lk" where="type=2 and siteId=1" >
                        <a href="${lk.linkUrl}" target="_blank">${lk.title}</a>
                <#/foreach>
            </div>
        </div>
        <!--合作伙伴结束--

<!--底部开始-->
        <div class="cai_bg"></div>
        <div class="Web_Icon"></div>
        <!--底部结束-->
        <!--<iframe src="footer.html" width="100%" height="365" frameborder="0" scrolling="no"></iframe>-->
        <div class="copyright_bottom">
            ${site.copyright}<br />
            备案号:${site.siteRecord}
        </div>
        <div style="display: none;">
            ${site.statScript}
        </div>
</body>
</html>

 

就这一行要改的。

View Code

 

访问首页的返回用render_template渲染基于base.html的子模板plan.html,并给其中的两个模板变量赋值。

 

 

试试吧。

模板引擎代码如下:

 

 

Tag类:

 

单个模板变量的渲染不过瘾,我们多搞几个,不用这样写一堆变量传进去。

图片 11图片 12

 

修改Flask_Plan.py

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Parser
{
    /// <summary>
    /// 标签
    /// </summary>
    public class Tag
    {
        /// <summary>
        /// 标签开始位置
        /// </summary>
        public int start { get; set; }
        /// <summary>
        /// 标签标记
        /// </summary>
        public Token token { get; set; }
        /// <summary>
        /// 标签字符串
        /// </summary>
        public string tagStr { get; set; }
        /// <summary>
        /// 标签字符串长度
        /// </summary>
        public int length
        {
            get
            {
                return tagStr.Length;
            }
        }
        /// <summary>
        /// 标签结束位置
        /// </summary>
        public int end
        {
            get
            {
                return start   length;
            }
        }

        /// <summary>
        /// 标签
        /// </summary>
        /// <param name="start">标签开始位置</param>
        /// <param name="token">标签标记</param>
        /// <param name="tagStr">标签字符串</param>
        public Tag(int start, Token token, string tagStr)
        {
            this.start = start;
            this.token = token;
            this.tagStr = tagStr;
        }
    }
}

 

图片 13图片 14

View Code

 

@app.route('/')
def hello_world():
    plan = {
        'date': '2017-12-12',
        'train': 'T198',
        'track': 10,
        'start_time': '10:00',
        'end_time': '12:00',
        'plan_end': '8:30',
        'plan_in': '9:30',
        'plan_out': '18:00',
        'plan_start': '20:40'

    }

    return render_template('plan.html', **plan)

Token类:

 

View Code

图片 15图片 16

 

修改templates/plan.html 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Parser
{
    /// <summary>
    /// 标签标记
    /// </summary>
    public enum Token
    {
        /// <summary>
        /// 标签开始
        /// </summary>
        TagStart,
        /// <summary>
        /// 标签结束
        /// </summary>
        TagEnd,

        /// <summary>
        /// else标签
        /// </summary>
        ElseTag
    }
}

6, Login_NVelocity.html和Login_NVelocity.ashx代代码分别如下:

图片 17图片 18

View Code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>一般处理程序登陆页面</title>
</head>
<body>
    <form action="Login_NVelocity.ashx" method="get">
        用户名:<input type="text" name="UserName" value="$name" /><br />
        密码:<input type="password" name="password" value="$pwd" /><br />
        提交:<input type="submit" /><br />
        <p>$message</p>
    </form>
</body>
</html>
{%  extends 'base.html' %}
{% block main1 %}
    <h1>这是main1提交的新内容{{ name1 }}</h1>
{% endblock %}
{% block main2 %}
    <h3>这是main2提交的新内容{{ name2 }}</h3>
    {{ train }}  <br>
    {{ date }}<br>
    {{ track }}<br>
    {{ start_time }}<br>
    {{ end_time }}<br>
    {{ plan_end }}<br>
    {{ plan_in }}<br>
    {{ plan_out }}<br>
    {{ plan_start }}<br>
{% endblock %}

IfTag类:

Login_NVelocity.ashx :  一般处理程序页面实现了对HTML模板页面中变量的控制  $name, $pwd,$message 

View Code

图片 19图片 20

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO;

namespace HttpHandler
{
    /// <summary>
    /// LoginNVelocity 的摘要说明
    /// </summary>
    public class LoginNVelocity : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string strUserName = context.Request["username"];
            string strPwd = context.Request["password"];
            string strMsg = context.Request["message"];

            string strHtml = "";
            if (string.IsNullOrEmpty(strUserName) && string.IsNullOrEmpty(strPwd))
            {
                strHtml = Template_Nvelocity("", "", "");
            }
            else if (strUserName == "admin" && strPwd == "123")
            {
                strMsg = "恭喜您,登陆成功!";
                context.Response.Write(strMsg);
            }
            else
            {
                strMsg = "登陆失败,用户名或者密码错误";
                strHtml = Template_Nvelocity(strUserName, strPwd, strMsg);
            }
            //输出
            context.Response.Write(strHtml);


        }
        //模板引擎方法,传递 登录名、登陆密码、登陆是否成功 这三个变量
        public string Template_Nvelocity(string strUserName, string strPwd, string strMsg)
        {

            //创建NVlecocity模板引擎的实例对象
            VelocityEngine vlEngine = new VelocityEngine();

            //初始化实例对象,定义对象的部分属性
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));  //指定模板文件所在文件夹
            vlEngine.Init(props);

            //替换模板中的数据
            VelocityContext vltContext = new VelocityContext();
            //vltContext.Put("data", data);  //设置参数,在模板中可以通过$Data读取数据
            vltContext.Put("name", strUserName);
            vltContext.Put("pwd", strPwd);
            vltContext.Put("message", strMsg);

            //从文件中读取模板
            Template VltTemp = vlEngine.GetTemplate("Login_NVelocity.html");
            //合并模板
            StringWriter writer = new StringWriter();
            VltTemp.Merge(vltContext, writer);

            //转化为字符串
            string html = writer.GetStringBuilder().ToString();

            return html;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

我们用**plan这个参数,把plan这个字典传入了模板渲染引擎,在plan.html这个模板中直接使用字典索引来显示值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DAL.Parser
{
    /// <summary>
    /// if标签
    /// </summary>
    public class IfTag
    {
        /// <summary>
        /// 开始标签
        /// </summary>
        public Tag startIf { get; set; }
        /// <summary>
        /// else标签,可为null
        /// </summary>
        public Tag elseTag { get; set; }
        /// <summary>
        /// 结束标签
        /// </summary>
        public Tag endIf { get; set; }

        /// <summary>
        /// 页面html
        /// </summary>
        public string pageHtml { get; set; }
        /// <summary>
        /// if分支html
        /// </summary>
        public string ifHtml
        {
            get
            {
                if (elseTag != null)
                {
                    return pageHtml.Substring(startIf.start   startIf.length, elseTag.start - startIf.start - startIf.length);
                }
                else
                {
                    return pageHtml.Substring(startIf.start   startIf.length, endIf.start - startIf.start - startIf.length);
                }
            }
        }
        /// <summary>
        /// else分支html
        /// </summary>
        public string elseHtml
        {
            get
            {
                if (elseTag != null && elseTag.start   elseTag.length < endIf.start)
                {
                    return pageHtml.Substring(elseTag.start   elseTag.length, endIf.start - elseTag.start - elseTag.length);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 结果html
        /// </summary>
        public string resultHtml { get; set; }

        /// <summary>
        /// if标签
        /// </summary>
        /// <param name="startIf">开始标签</param>
        /// <param name="elseTag">else标签</param>
        /// <param name="endIf">结束标签</param>
        /// <param name="pageHtml">页面html</param>
        public IfTag(Tag startIf, Tag elseTag, Tag endIf, string pageHtml)
        {
            this.startIf = startIf;
            this.endIf = endIf;
            this.elseTag = elseTag;
            this.pageHtml = pageHtml;
            this.resultHtml = string.Empty;

            if (!Regex.IsMatch(startIf.tagStr, @"${([^${}] ).([^${}] )}")) //如果没有未解析的Model
            {
                bool compareOr = false;
                foreach (string strOr in startIf.tagStr.Replace("<#if", "").Replace(">", "").Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries)) //遍历or分隔
                {
                    bool compareAnd = true;
                    foreach (string strAnd in strOr.Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries)) //遍历and分隔
                    {
                        Regex reg = new Regex(@"([^s]*)[s]*(==|!=|大于|小于)[s]*([^s]*)", RegexOptions.IgnoreCase);
                        MatchCollection mc = reg.Matches(strAnd);
                        foreach (Match match in mc)
                        {
                            #region 比较
                            switch (match.Groups[2].Value)
                            {
                                case "==":
                                    if (match.Groups[1].Value.Replace(""", "") != match.Groups[3].Value.Replace(""", ""))
                                    {
                                        compareAnd = false;
                                    }
                                    break;
                                case "!=":
                                    if (match.Groups[1].Value.Replace(""", "") == match.Groups[3].Value.Replace(""", ""))
                                    {
                                        compareAnd = false;
                                    }
                                    break;
                                case "大于":
                                    int d;
                                    if (int.TryParse(match.Groups[1].Value, out d) && int.TryParse(match.Groups[3].Value, out d))
                                    {
                                        if (Convert.ToInt32(match.Groups[1].Value.Replace(""", "")) <= Convert.ToInt32(match.Groups[3].Value.Replace(""", "")))
                                        {
                                            compareAnd = false;
                                        }
                                    }
                                    break;
                                case "小于":
                                    if (int.TryParse(match.Groups[1].Value, out d) && int.TryParse(match.Groups[3].Value, out d))
                                    {
                                        if (Convert.ToInt32(match.Groups[1].Value.Replace(""", "")) >= Convert.ToInt32(match.Groups[3].Value.Replace(""", "")))
                                        {
                                            compareAnd = false;
                                        }
                                    }
                                    break;
                                default:
                                    break;
                            }
                            #endregion
                        }
                    } // end //遍历and分隔
                    if (compareAnd)
                    {
                        compareOr = true;
                    }
                } // end //遍历or分隔
                if (compareOr)
                {
                    resultHtml = ifHtml;
                    return;
                }
                resultHtml = elseHtml;
            }

        } //end 构造函数

    } //end IfTag 类

} //end namespace

------------------------------------------------gif动画演示--------------------------------------------------------

 

View Code

图片 21

使用更多的模板变量。

ForeachTag类:

7,项目中新添加一个Person类,我们就可以通过NVolecity 把Person类的属性 在模板中进行调用、显示  

修改Flask_Plan.py

图片 22图片 23

  A.添加Person类

图片 24图片 25

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Models;

namespace DAL.Parser
{
    /// <summary>
    /// Foreach标签
    /// </summary>
    public class ForeachTag
    {
        /// <summary>
        /// 开始标签
        /// </summary>
        public Tag startForeach { get; set; }
        /// <summary>
        /// 结束标签
        /// </summary>
        public Tag endForeach { get; set; }

        /// <summary>
        /// 标签内html
        /// </summary>
        public string innerHtml
        {
            get
            {
                return pageHtml.Substring(startForeach.start   startForeach.length, endForeach.start - startForeach.start - startForeach.length);
            }
        }
        /// <summary>
        /// 页面html
        /// </summary>
        public string pageHtml { get; set; }

        /// <summary>
        /// 列表的标识
        /// </summary>
        public string list { get; set; }
        /// <summary>
        /// 列表的model
        /// </summary>
        public string model { get; set; }
        /// <summary>
        /// where
        /// </summary>
        public string where { get; set; }
        /// <summary>
        /// 当面页数
        /// </summary>
        public int page { get; set; }
        /// <summary>
        /// 每页数据条数
        /// </summary>
        public int pageSize { get; set; }

        /// <summary>
        /// 该foreach是否是当前页面的主列表
        /// </summary>
        public bool main = false;
        /// <summary>
        /// 栏目名称或栏目ID
        /// </summary>
        public string channel { get; set; }

        /// <summary>
        /// Foreach标签
        /// </summary>
        /// <param name="startForeach">开始标签</param>
        /// <param name="endForeach">结束标签</param>
        /// <param name="pageHtml">页面html</param>
        public ForeachTag(Tag startForeach, Tag endForeach, string pageHtml)
        {
            this.startForeach = startForeach;
            this.endForeach = endForeach;
            this.pageHtml = pageHtml;

            Regex reg = new Regex(@"([^s] )s*=s*""([^""] )""", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(startForeach.tagStr);
            foreach (Match m in mc)
            {
                switch (m.Groups[1].Value)
                {
                    case "list":
                        this.list = m.Groups[2].Value;
                        break;
                    case "model":
                        this.model = m.Groups[2].Value;
                        break;
                    case "where":
                        this.where = m.Groups[2].Value;
                        break;
                    case "page":
                        if (Regex.IsMatch(m.Groups[2].Value, @"^d $"))
                        {
                            this.page = Convert.ToInt32(m.Groups[2].Value);
                        }
                        break;
                    case "pageSize":
                        if (Regex.IsMatch(m.Groups[2].Value, @"^d $"))
                        {
                            this.pageSize = Convert.ToInt32(m.Groups[2].Value);
                        }
                        break;
                    case "channel":
                        this.channel = m.Groups[2].Value;
                        break;
                    case "main":
                        this.main = true;
                        break;
                }
            }
        } // end 构造函数

    } // end ForeachTag 类
} // end namespace

图片 26图片 27

def hello_world():
    class plan(object):
        P_end = '8:45'
        P_in = '9:30'
        W_start = '10:00'
        W_end = '12:00'
        P_out = '18:00'
        P_start = '20:40'

    p1 = plan()

    plan = {
        'date': '2017-12-12',
        'train': 'T198',
        'track': 10,
        'time': p1,

    }

    return render_template('plan.html', **plan)

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HttpHandler
{
    public class Person
    {
        public string Name { get; set; }        //姓名
        public int Age { get; set; }            //年龄

        public Person Father { get; set; }      //父亲
    }
}

View Code

PageNum类:

View Code

修改templates/plan.html 

图片 28图片 29

     B.添加一般处理程序 Person_Class_NVelocity.ashx ,把Person对象的属性 通过模板引擎 传递到Person.html页面中

图片 30图片 31

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Parser
{
    /// <summary>
    /// 分页数字类
    /// </summary>
    public class PageNum
    {
        /// <summary>
        /// 分页数字
        /// </summary>
        public int num { get; set; }
        /// <summary>
        /// 是否非数字,1:非数字,0数字,用来显示省略号
        /// </summary>
        public int nan { get; set; }

        /// <summary>
        /// 分页数字
        /// </summary>
        /// <param name="num">分页数字</param>
        /// <param name="nan">是否非数字,1:非数字,0数字,用来显示省略号</param>
        public PageNum(int num, int nan = 0)
        {
            this.num = num;
            this.nan = nan;
        }
    }
}

图片 32图片 33

{%  extends 'base.html' %}
{% block main1 %}
    <h1>这是main1提交的新内容{{ name1 }}</h1>
{% endblock %}
{% block main2 %}
    <h3>这是main2提交的新内容{{ name2 }}</h3>
{{ date }}
{{ time.P_start }}
{% endblock %}

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO;


namespace HttpHandler
{
    /// <summary>
    /// Person_Class_NVelocity 的摘要说明
    /// </summary>
    public class Person_Class_NVelocity : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";


            Person me = new Person();
            me.Name = "Peter";
            me.Age = 18;

            Person Dad = new Person();
            Dad.Name = "Da Peter";
            Dad.Age = 50;

            me.Father = Dad;

            string strHtml = Template_Nvelocity_P(me);

            //输出
            context.Response.Write(strHtml);
        }

        //模板引擎方法
        public string Template_Nvelocity_P(Person per)
        {

            //创建NVlecocity模板引擎的实例对象
            VelocityEngine vlEngine = new VelocityEngine();

            //初始化实例对象,定义对象的部分属性
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));  //指定模板文件所在文件夹
            vlEngine.Init(props);

            //替换模板中的数据
            VelocityContext vltContext = new VelocityContext();
            //vltContext.Put("data", data);  //设置参数,在模板中可以通过$Data读取数据
            vltContext.Put("p", per);


            //从文件中读取模板
            Template VltTemp = vlEngine.GetTemplate("Person.html");
            //合并模板
            StringWriter writer = new StringWriter();
            VltTemp.Merge(vltContext, writer);

            //转化为字符串
            string html = writer.GetStringBuilder().ToString();

            return html;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

View Code

 

View Code

这次我们引入了类的概念,把time赋值为实例化的plan,在模板中用 time.***来渲染。

模板引擎TemplateDal类:

  C.Person.html页面中直接调用传递过来的Person对象的属性

 

图片 34图片 35

图片 36图片 37

我们也可以用字典的嵌套。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using DAL.Parser;
using Models;

namespace DAL
{
    /// <summary>
    /// 模板引擎
    /// </summary>
    public class TemplateDal
    {
        #region 变量
        /// <summary>
        /// 分页数字列表
        /// </summary>
        public static List<PageNum> pageNumList = null;
        #endregion

        #region 载入模板
        /// <summary>
        /// 载入模板
        /// </summary>
        public static string LoadTemplate(HttpServerUtilityBase server, string url)
        {
            string result = string.Empty;
            StreamReader sr = new StreamReader(server.MapPath(url), Encoding.UTF8);
            result = sr.ReadToEnd();
            sr.Close();
            return result;
        }
        #endregion

        #region 写入模板
        /// <summary>
        /// 写入模板
        /// </summary>
        public static void WriteTemplate(HttpServerUtilityBase server, string url, string html)
        {
            string result = string.Empty;
            string path = server.MapPath(url);
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }
            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.Write(html);
            sw.Close();
            fs.Close();
        }
        #endregion

        #region 替换页面model
        /// <summary>
        /// 替换页面model
        /// </summary>
        public static string ReplaceModels(string pageHtml, Dictionary<string, object> data)
        {
            Regex reg = new Regex(@"${([^${}] ).([^${}] )}", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                if (data.Keys.Contains<string>(m.Groups[1].Value))
                {
                    object model = data[m.Groups[1].Value];
                    Type type = model.GetType();
                    PropertyInfo propertyInfo = type.GetProperty(m.Groups[2].Value);
                    object obj = propertyInfo.GetValue(model, null);
                    string val = string.Empty;
                    if (obj != null)
                    {
                        if (obj.GetType() == typeof(DateTime))
                        {
                            val = ((DateTime)obj).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            val = obj.ToString();
                        }
                    }
                    pageHtml = pageHtml.Replace("${"   m.Groups[1].Value   "."   m.Groups[2].Value   "}", val);
                }
            }
            return pageHtml;
        }
        #endregion

        #region 替换列表的model
        /// <summary>
        /// 替换列表的model
        /// </summary>
        public static string ReplaceModels(string html, string model, object data)
        {
            Regex reg = new Regex(@"${[^${}] .([^${}] )}", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(html);
            foreach (Match m in mc)
            {
                Type type = data.GetType();
                PropertyInfo propertyInfo = type.GetProperty(m.Groups[1].Value);
                object obj = propertyInfo.GetValue(data, null);
                string val = string.Empty;
                if (obj != null)
                {
                    if (obj.GetType() == typeof(DateTime))
                    {
                        val = ((DateTime)obj).ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        val = obj.ToString();
                    }
                }
                html = html.Replace("${"   model   "."   m.Groups[1].Value   "}", val);
            }
            return html;
        }
        #endregion

        #region 判断页面是否存在 foreach 标签
        /// <summary>
        /// 判断页面是否存在 foreach 标签
        /// </summary>
        public static bool HasForeach(string html)
        {
            Regex reg = new Regex(@"<!--[s]{0,1}#[s]{0,1}foreach[s] [^<>${}]*-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            return reg.IsMatch(html);
        }
        #endregion

        #region 替换foreach
        /// <summary>
        /// 替换foreach
        /// </summary>
        public static string ReplaceLists(string pageHtml, Dictionary<string, object> data)
        {
            #region 生成标签集合
            Regex regStartForeach = new Regex(@"<!--[s]{0,1}#[s]{0,1}foreach[s] [^<>]*>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Regex regEndForeach = new Regex(@"<!--[s]{0,1}#[s]{0,1}/foreach[s]{0,1}-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            MatchCollection mcStartForeach = regStartForeach.Matches(pageHtml);
            MatchCollection mcEndForeach = regEndForeach.Matches(pageHtml);
            List<Tag> tagList = new List<Tag>();
            foreach (Match mStartForeach in mcStartForeach)
            {
                Tag tag = new Tag(mStartForeach.Index, Token.TagStart, mStartForeach.Value);
                tagList.Add(tag);
            }
            foreach (Match mEndForeach in mcEndForeach)
            {
                Tag tag = new Tag(mEndForeach.Index, Token.TagEnd, mEndForeach.Value);
                tagList.Add(tag);
            }
            tagList.Sort((a, b) => a.start - b.start);
            #endregion

            for (int i = 0; i < tagList.Count; i  ) //遍历标签集合
            {
                Tag tag = tagList[i];
                int depth = 0;
                if (tag.token == Token.TagStart)
                {
                    for (int j = i   1; j < tagList.Count; j  )
                    {
                        if (tagList[j].token == Token.TagEnd && depth == 0)
                        {
                            string resultHtml = string.Empty;
                            ForeachTag foreachTag = new ForeachTag(tag, tagList[j], pageHtml);

                            #region 替换
                            switch (foreachTag.list)
                            {
                                case "channel":
                                    StringBuilder sb = new StringBuilder();
                                    ChannelDal m_ChannelDal = new ChannelDal();
                                    List<cms_channel_ext> channelList = m_ChannelDal.GetList(foreachTag.where);
                                    foreach (cms_channel_ext channel in channelList)
                                    {
                                        sb.Append(TemplateDal.ReplaceModels(foreachTag.innerHtml, foreachTag.model, channel));
                                    }
                                    resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   sb.ToString()   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    break;
                                case "content":
                                    sb = new StringBuilder();
                                    ContentDal m_ContentDal = new ContentDal();
                                    PagerModel pager = new PagerModel();
                                    pager.rows = foreachTag.pageSize;
                                    pager.page = foreachTag.page;
                                    List<cms_content_ext> contentList = m_ContentDal.GetList(ref pager, foreachTag.channel, foreachTag.where);
                                    foreach (cms_content_ext content in contentList)
                                    {
                                        sb.Append(TemplateDal.ReplaceModels(foreachTag.innerHtml, foreachTag.model, content));
                                    }
                                    resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   sb.ToString()   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    break;
                                case "links":
                                    sb = new StringBuilder();
                                    LinksDal m_LinksDal = new LinksDal();
                                    List<cms_links_ext> linksList = m_LinksDal.GetList(foreachTag.where);
                                    foreach (cms_links_ext links in linksList)
                                    {
                                        sb.Append(TemplateDal.ReplaceModels(foreachTag.innerHtml, foreachTag.model, links));
                                    }
                                    resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   sb.ToString()   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    break;
                                case "banners":
                                    sb = new StringBuilder();
                                    BannersDal m_BannersDal = new BannersDal();
                                    List<cms_banners_ext> bannersList = m_BannersDal.GetList(foreachTag.where);
                                    foreach (cms_banners_ext banners in bannersList)
                                    {
                                        sb.Append(TemplateDal.ReplaceModels(foreachTag.innerHtml, foreachTag.model, banners));
                                    }
                                    resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   sb.ToString()   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    break;
                                case "pages":
                                    if (data.ContainsKey(foreachTag.list))
                                    {
                                        sb = new StringBuilder();
                                        List<PageNum> pageNumList = data[foreachTag.list] as List<PageNum>;
                                        foreach (PageNum pageNum in pageNumList)
                                        {
                                            sb.Append(TemplateDal.ReplaceModels(foreachTag.innerHtml, foreachTag.model, pageNum));
                                        }
                                        resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   sb.ToString()   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    }
                                    break;
                                default:
                                    resultHtml = foreachTag.pageHtml.Substring(0, foreachTag.startForeach.start)   foreachTag.pageHtml.Substring(foreachTag.endForeach.start   foreachTag.endForeach.length);
                                    break;
                            }
                            #endregion

                            return resultHtml;
                        }
                        if (tagList[j].token == Token.TagStart)
                        {
                            depth  ;
                        }
                        if (tagList[j].token == Token.TagEnd)
                        {
                            depth--;
                        }
                    }
                }
            }
            return pageHtml;
        }
        #endregion

        #region 判断页面是否存在 if else 标签
        /// <summary>
        /// 判断页面是否存在 if else 标签
        /// </summary>
        public static bool HasIfElse(string html)
        {
            Regex reg = new Regex(@"<!--[s]{0,1}#[s]{0,1}if[s] ([^<>${}] )[s]*-->", RegexOptions.IgnoreCase);
            return reg.IsMatch(html);
        }
        #endregion

        #region 替换 if else end if
        /// <summary>
        /// 替换 if else end if
        /// </summary>
        public static string ReplaceIfElse(string pageHtml)
        {
            #region 生成标签集合
            Regex regStartIf = new Regex(@"<!--[s]{0,1}#[s]{0,1}if[s] ([^<>] )[s]*-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Regex regElseTag = new Regex(@"<!--[s]{0,1}#[s]{0,1}else[s]{0,1}-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Regex regEndIf = new Regex(@"<!--[s]{0,1}#[s]{0,1}end if[s]{0,1}-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            MatchCollection mcStartIf = regStartIf.Matches(pageHtml);
            MatchCollection mcElseTag = regElseTag.Matches(pageHtml);
            MatchCollection mcEndIf = regEndIf.Matches(pageHtml);
            List<Tag> tagList = new List<Tag>();
            foreach (Match mStartIf in mcStartIf)
            {
                Tag tag = new Tag(mStartIf.Index, Token.TagStart, mStartIf.Value);
                tagList.Add(tag);
            }
            foreach (Match mEndIf in mcEndIf)
            {
                Tag tag = new Tag(mEndIf.Index, Token.TagEnd, mEndIf.Value);
                tagList.Add(tag);
            }
            foreach (Match mElseTag in mcElseTag)
            {
                Tag tag = new Tag(mElseTag.Index, Token.ElseTag, mElseTag.Value);
                tagList.Add(tag);
            }
            tagList.Sort((a, b) => a.start - b.start);
            #endregion

            for (int i = 0; i < tagList.Count; i  ) //遍历标签集合
            {
                Tag tagStartIf = tagList[i];
                Tag tagElseTag = null;
                int depth = 0;
                if (tagStartIf.token == Token.TagStart) //是if标签
                {
                    for (int j = i   1; j < tagList.Count; j  ) //遍历if标签后面的标签
                    {
                        if (depth == 0)
                        {
                            if (tagList[j].token == Token.ElseTag) //是else标签
                            {
                                tagElseTag = tagList[j];
                            }
                            if (tagList[j].token == Token.TagEnd)
                            {
                                IfTag ifTag = new IfTag(tagStartIf, tagElseTag, tagList[j], pageHtml);
                                if (!Regex.IsMatch(ifTag.startIf.tagStr, @"${([^${}] ).([^${}] )}")) //如果没有未解析的Model
                                {
                                    return pageHtml.Substring(0, ifTag.startIf.start)   ifTag.resultHtml   pageHtml.Substring(ifTag.endIf.start   ifTag.endIf.length);
                                }
                            }
                        }
                        if (tagList[j].token == Token.TagStart)
                        {
                            depth  ;
                        }
                        if (tagList[j].token == Token.TagEnd)
                        {
                            depth--;
                        }
                    }
                }
            }

            return pageHtml;
        }
        #endregion

        #region 替换页面function
        /// <summary>
        /// 替换页面function
        /// </summary>
        public static string ReplaceFunctions(string pageHtml)
        {
            #region 替换cutstr函数
            Regex reg = new Regex(@"${cutstr((.*),([d] ))}", RegexOptions.IgnoreCase);

            MatchCollection mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                Regex reg2 = new Regex(@"(?:<[^<>] [s] [^<>] [s]*/>)|(?:<[^<>] [s] [^<>] [s]*>)|(?:</[^<>] >)|(?:&nbsp;)", RegexOptions.IgnoreCase);
                string html = reg2.Replace(m.Groups[1].Value, "");
                if (html.Length > Convert.ToInt32(m.Groups[2].Value))
                {
                    pageHtml = pageHtml.Replace(m.Value, html.Substring(0, Convert.ToInt32(m.Groups[2].Value)));
                }
                else
                {
                    pageHtml = pageHtml.Replace(m.Value, html);
                }
            }
            #endregion

            #region 替换html2text函数
            reg = new Regex(@"${html2text((. ),([d] ))}", RegexOptions.IgnoreCase);

            mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                Regex reg2 = new Regex(@"(?:<[^<>] [s] [^<>] [s]*/>)|(?:<[^<>] [s] [^<>] [s]*>)|(?:</[^<>] >)|(?:&nbsp;)", RegexOptions.IgnoreCase);
                string html = reg2.Replace(m.Groups[1].Value, "");
                pageHtml = pageHtml.Replace(m.Value, html);
            }
            #endregion

            #region 替换replace函数
            reg = new Regex(@"${replace(([^(),] ),([^(),] ),([^(),] ))}", RegexOptions.IgnoreCase);

            mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                pageHtml = pageHtml.Replace(m.Value, m.Groups[1].Value.Replace(m.Groups[2].Value.Replace("'", ""), m.Groups[3].Value.Replace("'", "")));
            }
            #endregion

            return pageHtml;
        }
        #endregion

        #region 替换页面include
        /// <summary>
        /// 替换页面include
        /// </summary>
        public static string ReplaceIncludes(HttpServerUtilityBase server, string pageHtml)
        {
            Regex reg = new Regex(@"<!--[s]{0,1}#[s]{0,1}include[s] src[s]*=[s]*""([^""<>${}] )""[s]*-->", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                StreamReader sr = new StreamReader(server.MapPath(m.Groups[1].Value), Encoding.UTF8);
                string html = sr.ReadToEnd();
                sr.Close();
                pageHtml = pageHtml.Replace(m.Value, html);
            }
            return pageHtml;
        }
        #endregion

        #region 替换页面set
        /// <summary>
        /// 替换页面set
        /// </summary>
        public static string ReplaceSets(string pageHtml)
        {
            Regex reg = new Regex(@"<!--[s]{0,1}#[s]{0,1}set[s] ([^<>] )[s]*-->", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(pageHtml);
            foreach (Match m in mc)
            {
                Regex regSub = new Regex(@"([^s] )s*=s*""([^""] )""", RegexOptions.IgnoreCase);
                MatchCollection mcSub = regSub.Matches(m.Groups[1].Value);
                string data = string.Empty;
                string model = string.Empty;
                string where = string.Empty;
                foreach (Match mSub in mcSub)
                {
                    switch (mSub.Groups[1].Value)
                    {
                        case "data":
                            data = mSub.Groups[2].Value;
                            break;
                        case "model":
                            model = mSub.Groups[2].Value;
                            break;
                        case "where":
                            where = mSub.Groups[2].Value;
                            break;
                    }
                }

                switch (data)
                {
                    case "channel":
                        ChannelDal m_ChannelDal = new ChannelDal();
                        cms_channel channel = m_ChannelDal.Get(where);
                        Dictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add(model, channel);
                        pageHtml = pageHtml.Replace(m.Value, "");
                        pageHtml = TemplateDal.ReplaceModels(pageHtml, dic);
                        break;
                }
            }
            return pageHtml;
        }
        #endregion

        #region 获取head内容
        /// <summary>
        /// 获取head内容
        /// </summary>
        public static string GetHeadHtml(string pageHtml)
        {
            Regex reg = new Regex(@"<head>([Ss]*)</head>", RegexOptions.IgnoreCase);
            Match m = reg.Match(pageHtml);
            return m.Groups[1].Value;
        }
        #endregion

        #region 获取body内容
        /// <summary>
        /// 获取body内容
        /// </summary>
        public static string GetBodyHtml(string pageHtml)
        {
            Regex reg = new Regex(@"<body>([Ss]*)</body>", RegexOptions.IgnoreCase);
            Match m = reg.Match(pageHtml);
            return m.Groups[1].Value;
        }
        #endregion

        #region 获取一个ForeachTag
        /// <summary>
        /// 获取一个ForeachTag
        /// </summary>
        public static ForeachTag GetForeachTag(string pageHtml, string list = "content")
        {
            #region 生成标签集合
            Regex regStartForeach = new Regex(@"<!--[s]{0,1}#[s]{0,1}foreach[s] [^<>]*-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Regex regEndForeach = new Regex(@"<!--[s]{0,1}#[s]{0,1}/foreach[s]{0,1}-->", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            MatchCollection mcStartForeach = regStartForeach.Matches(pageHtml);
            MatchCollection mcEndForeach = regEndForeach.Matches(pageHtml);
            List<Tag> tagList = new List<Tag>();
            foreach (Match mStartForeach in mcStartForeach)
            {
                Tag tag = new Tag(mStartForeach.Index, Token.TagStart, mStartForeach.Value);
                tagList.Add(tag);
            }
            foreach (Match mEndForeach in mcEndForeach)
            {
                Tag tag = new Tag(mEndForeach.Index, Token.TagEnd, mEndForeach.Value);
                tagList.Add(tag);
            }
            tagList.Sort((a, b) => a.start - b.start);
            #endregion

            for (int i = 0; i < tagList.Count; i  ) //遍历标签集合
            {
                Tag tag = tagList[i];
                int depth = 0;
                if (tag.token == Token.TagStart)
                {
                    for (int j = i   1; j < tagList.Count; j  )
                    {
                        if (tagList[j].token == Token.TagEnd && depth == 0)
                        {
                            ForeachTag foreachTag = new ForeachTag(tag, tagList[j], pageHtml);
                            if (foreachTag.main && foreachTag.list == list)
                            {
                                return foreachTag;
                            }
                        }
                        if (tagList[j].token == Token.TagStart)
                        {
                            depth  ;
                        }
                        if (tagList[j].token == Token.TagEnd)
                        {
                            depth--;
                        }
                    }
                }
            } //end 遍历标签集合 

            return null;
        }
        #endregion

    } //end of class TemplateDal

} //end of namespace DAL
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
   父亲的年龄是: $p.Father.Age &nbsp; 父亲的姓名: $p.Father.Name<br />
    我的年龄是: $p.Age &nbsp; 我的姓名: $p.Name<br />
</body>
</html>

修改Flask_Plan.py

View Code

View Code

图片 38图片 39

 

----------------------------------------------Gif动画演示----------------------------------------------------------

@app.route('/')
def hello_world():
    class plan(object):
        P_end = '8:45'
        P_in = '9:30'
        W_start = '10:00'
        W_end = '12:00'
        P_out = '18:00'
        P_start = '20:40'

    p1 = plan()

    plan = {
        'train': {

            'date': '2017-12-12',
            'train': 'T198',
            'track': 10,
        },
        'time': p1,

    }

    return render_template('plan.html', **plan)

使用模板引擎生成静态页面:

图片 40

View Code

图片 41图片 42

8, HTML中直接通过 对象的索引访问对象

修改templates/plan.html 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Common;
using Common.Utils;
using DAL;
using DAL.Parser;
using DBHelper;
using Models;

namespace NetCMS.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 前台更新
    /// </summary>
    public class FrontPageManageController : Common.ControllerBase
    {
        #region 变量
        private SiteDal m_SiteDal = new SiteDal();
        private ChannelDal m_ChannelDal = new ChannelDal();
        private ContentDal m_ContentDal = new ContentDal();
        /// <summary>
        /// 进度总数
        /// </summary>
        private static double total = 0;
        /// <summary>
        /// 当前进度
        /// </summary>
        private static int cur = 0;
        /// <summary>
        /// 错误信息
        /// </summary>
        private static string errMsg = string.Empty;
        #endregion

        #region 替换
        /// <summary>
        /// 替换
        /// </summary>
        private string Replace(string templateHtml, Dictionary<string, object> dic)
        {
            templateHtml = TemplateDal.ReplaceSets(templateHtml);
            templateHtml = TemplateDal.ReplaceModels(templateHtml, dic);
            templateHtml = TemplateDal.ReplaceIncludes(Server, templateHtml);
            templateHtml = TemplateDal.ReplaceModels(templateHtml, dic);
            while (TemplateDal.HasIfElse(templateHtml))
            {
                templateHtml = TemplateDal.ReplaceIfElse(templateHtml);
            }
            while (TemplateDal.HasForeach(templateHtml))
            {
                templateHtml = TemplateDal.ReplaceLists(templateHtml, dic);
            }
            while (TemplateDal.HasIfElse(templateHtml))
            {
                templateHtml = TemplateDal.ReplaceIfElse(templateHtml);
            }
            templateHtml = TemplateDal.ReplaceFunctions(templateHtml);
            string headHtml = TemplateDal.GetHeadHtml(templateHtml);
            string bodyHtml = TemplateDal.GetBodyHtml(templateHtml);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<!DOCTYPE html>rn<html>rn<head>rn{0}rn</head>rn<body>rn{1}rn</body>rn</html>rn", headHtml, bodyHtml);
            return sb.ToString();
        }
        #endregion

        #region Index页面
        [Permission(Code = "Index")]
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region Update更新
        [HttpPost]
        [Permission(Code = "Index")]
        public ActionResult Update()
        {
            try
            {
                new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        UpdateTheme();
                        UpdateIndex();
                        UpdateError();
                        UpdateComments();
                    }
                    catch (Exception ex)
                    {
                        errMsg  = ex.Message   "<br />"   ex.StackTrace.Replace("n", "<br />");
                    }
                })).Start();
                new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        UpdateList();
                    }
                    catch (Exception ex)
                    {
                        errMsg  = ex.Message   "<br />"   ex.StackTrace.Replace("n", "<br />");
                    }
                })).Start();
                new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        UpdateContent();
                    }
                    catch (Exception ex)
                    {
                        errMsg  = ex.Message   "<br />"   ex.StackTrace.Replace("n", "<br />");
                    }
                })).Start();

                return Content("OK");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
        #endregion

        #region 更新theme
        /// <summary>
        /// 更新theme
        /// </summary>
        public void UpdateTheme()
        {
            List<cms_site_ext> siteList = m_SiteDal.GetListAll();
            foreach (cms_site_ext site in siteList) //遍历站点
            {
                string fromPath = Server.MapPath(string.Format("/templates/{0}/theme/", site.folder));
                string toPath = Server.MapPath(string.Format("/{0}/theme/", site.folder));
                CopyThemeFiles(fromPath, toPath);
            }
            cur  ;
        }
        /// <summary>
        /// 更新theme中的文件
        /// </summary>
        private void CopyThemeFiles(string fromPath, string toPath)
        {
            if (!Directory.Exists(toPath))
            {
                Directory.CreateDirectory(toPath);
            }
            foreach (string filePath in Directory.GetFiles(fromPath))
            {
                System.IO.File.Copy(filePath, toPath   Path.GetFileName(filePath), true);
            }
            foreach (string dirPath in Directory.GetDirectories(fromPath))
            {
                int pos = dirPath.LastIndexOf("\");
                CopyThemeFiles(dirPath, toPath   dirPath.Substring(pos   1)   "\");
            }
        }
        #endregion

        #region 更新index页面
        /// <summary>
        /// 更新index页面
        /// </summary>
        public void UpdateIndex()
        {
            List<cms_site_ext> siteList = m_SiteDal.GetListAll();
            int i = 0;
            foreach (cms_site_ext site in siteList) //遍历站点
            {
                string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/index.html", site.folder));
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("site", site);
                dic.Add("parentParentChannel", new cms_channel());
                dic.Add("parentChannel", new cms_channel());
                dic.Add("channel", new cms_channel());
                TemplateDal.WriteTemplate(Server, string.Format("/{0}/index.html", site.folder), Replace(templateHtml, dic));
                if (i   == 0)
                {
                    string fromPath = Server.MapPath(string.Format("/templates/{0}/theme/", site.folder));
                    string toPath = Server.MapPath(string.Format("/theme/", site.folder));
                    CopyThemeFiles(fromPath, toPath);
                    TemplateDal.WriteTemplate(Server, "/index.html", Replace(templateHtml, dic));
                }
            }
            cur  ;
        }
        #endregion

        #region 更新list页面
        /// <summary>
        /// 更新list页面
        /// </summary>
        public void UpdateList()
        {
            List<cms_site_ext> siteList = m_SiteDal.GetListAll();
            foreach (cms_site_ext site in siteList) //遍历站点
            {
                List<cms_channel_ext> channelList = m_ChannelDal.GetList(string.Format("channel.siteId={0}", site.id));
                foreach (cms_channel_ext channel in channelList) //遍历栏目
                {
                    if (!string.IsNullOrWhiteSpace(errMsg)) break; //有错误则跳出

                    #region 获取父栏目
                    cms_channel parentParentChannel;
                    cms_channel parentChannel;
                    if (channel.parentId == -1)
                    {
                        parentChannel = new cms_channel();
                        parentChannel.parentId = -1;
                    }
                    else
                    {
                        parentChannel = m_ChannelDal.Get(channel.parentId);
                    }
                    if (parentChannel.parentId == -1)
                    {
                        parentParentChannel = new cms_channel();
                        parentParentChannel.parentId = -1;
                    }
                    else
                    {
                        parentParentChannel = m_ChannelDal.Get(parentChannel.parentId);
                    }
                    #endregion

                    if (channel.listType == (int)Enums.ChannelListType.文字列表
                        || channel.listType == (int)Enums.ChannelListType.图片列表
                        || channel.listType == (int)Enums.ChannelListType.父级栏目)
                    {
                        #region 取模板
                        string templateFileName = string.IsNullOrWhiteSpace(channel.template) ? "list.html" : channel.template;
                        string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/list/{1}", site.folder, templateFileName));
                        #endregion

                        #region 设置分页参数
                        ForeachTag foreachTag = TemplateDal.GetForeachTag(templateHtml);
                        PagerModel pager = new PagerModel();
                        if (foreachTag != null)
                        {
                            pager.rows = foreachTag.pageSize;
                        }
                        else
                        {
                            pager.rows = 20;
                        }
                        pager.page = 1;
                        #endregion

                        do
                        {
                            List<cms_content_ext> contentList = m_ContentDal.GetList(ref pager, channel.id.ToString(), null);

                            Dictionary<string, object> dic = new Dictionary<string, object>();
                            dic.Add("site", site);
                            dic.Add("parentParentChannel", parentParentChannel);
                            dic.Add("parentChannel", parentChannel);
                            dic.Add("channel", channel);
                            dic.Add("pager", pager);
                            dic.Add("pages", GetPageList(pager.page, pager.pageCount));

                            string filePath;
                            if (pager.page == 1)
                            {
                                filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                                TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                            }
                            filePath = string.Format("/{0}/list{1}{2}.html", site.folder, channel.id, "-"   pager.page.ToString());
                            TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                        } while (pager.page   <= pager.pageCount); //遍历每页数据
                    }
                    if (channel.listType == (int)Enums.ChannelListType.单篇文章)
                    {
                        cms_content content = m_ContentDal.GetByChannelId(channel.id);
                        if (content == null)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                            sb.AppendFormat("<script type="text/javascript">window.location = "error.html";</script>");
                            sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                            string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                            TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                        }
                        else
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                            sb.AppendFormat("<script type="text/javascript">window.location = "/{0}/content{1}.html";</script>", site.folder, content.id);
                            sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                            string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                            TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                        }
                    }
                    if (channel.listType == (int)Enums.ChannelListType.页面链接)
                    {
                        string url = string.Empty;
                        if (channel.pageUrl == "comments.html")
                        {
                            url = string.Format("/{0}/{1}", site.folder, channel.pageUrl);
                        }
                        else
                        {
                            url = channel.pageUrl;
                        }
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                        sb.AppendFormat("<script type="text/javascript">window.location = "{0}";</script>", url);
                        sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                        string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                        TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                    }
                    cur  ;
                }  //end 遍历 channelList
            } //end 遍历 siteList
        }
        #endregion

        #region 更新单个list页面
        /// <summary>
        /// 更新单个list页面
        /// </summary>
        public ActionResult UpdateListSingle(int id)
        {
            try
            {
                cms_channel channel = m_ChannelDal.Get(id);
                cms_site site = m_SiteDal.Get(channel.siteId);

                #region 获取父栏目
                cms_channel parentParentChannel;
                cms_channel parentChannel;
                if (channel.parentId == -1)
                {
                    parentChannel = new cms_channel();
                    parentChannel.parentId = -1;
                }
                else
                {
                    parentChannel = m_ChannelDal.Get(channel.parentId);
                }
                if (parentChannel.parentId == -1)
                {
                    parentParentChannel = new cms_channel();
                    parentParentChannel.parentId = -1;
                }
                else
                {
                    parentParentChannel = m_ChannelDal.Get(parentChannel.parentId);
                }
                #endregion

                if (channel.listType == (int)Enums.ChannelListType.文字列表
                    || channel.listType == (int)Enums.ChannelListType.图片列表
                    || channel.listType == (int)Enums.ChannelListType.父级栏目)
                {
                    #region 取模板
                    string templateFileName = string.IsNullOrWhiteSpace(channel.template) ? "list.html" : channel.template;
                    string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/list/{1}", site.folder, templateFileName));
                    #endregion

                    #region 设置分页参数
                    ForeachTag foreachTag = TemplateDal.GetForeachTag(templateHtml);
                    PagerModel pager = new PagerModel();
                    if (foreachTag != null)
                    {
                        pager.rows = foreachTag.pageSize;
                    }
                    else
                    {
                        pager.rows = 20;
                    }
                    pager.page = 1;
                    #endregion

                    do
                    {
                        List<cms_content_ext> contentList = m_ContentDal.GetList(ref pager, channel.id.ToString(), null);

                        Dictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add("site", site);
                        dic.Add("parentParentChannel", parentParentChannel);
                        dic.Add("parentChannel", parentChannel);
                        dic.Add("channel", channel);
                        dic.Add("pager", pager);
                        dic.Add("pages", GetPageList(pager.page, pager.pageCount));

                        string filePath;
                        if (pager.page == 1)
                        {
                            filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                            TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                        }
                        filePath = string.Format("/{0}/list{1}{2}.html", site.folder, channel.id, "-"   pager.page.ToString());
                        TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                    } while (pager.page   <= pager.pageCount); //遍历每页数据
                }
                if (channel.listType == (int)Enums.ChannelListType.单篇文章)
                {
                    cms_content content = m_ContentDal.GetByChannelId(channel.id);
                    if (content == null)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                        sb.AppendFormat("<script type="text/javascript">window.location = "error.html";</script>");
                        sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                        string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                        TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                        sb.AppendFormat("<script type="text/javascript">window.location = "/{0}/content{1}.html";</script>", site.folder, content.id);
                        sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                        string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                        TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                    }
                }
                if (channel.listType == (int)Enums.ChannelListType.页面链接)
                {
                    string url = string.Empty;
                    if (channel.pageUrl == "comments.html")
                    {
                        url = string.Format("/{0}/{1}", site.folder, channel.pageUrl);
                    }
                    else
                    {
                        url = channel.pageUrl;
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                    sb.AppendFormat("<script type="text/javascript">window.location = "{0}";</script>", url);
                    sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                    string filePath = string.Format("/{0}/list{1}.html", site.folder, channel.id);
                    TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                }

                var json = new
                {
                    ok = "OK",
                    url = string.Format("/{0}/list{1}.html", site.folder, channel.id)
                };
                return Json(json);
            }
            catch (Exception ex)
            {
                var json = new
                {
                    ok = ex.Message
                };
                return Json(json);
            }
        }
        #endregion

        #region 更新content页面
        /// <summary>
        /// 更新content页面
        /// </summary>
        public void UpdateContent()
        {
            List<cms_content_ext> contentListAll = m_ContentDal.GetListAll();
            foreach (cms_content_ext content in contentListAll) //遍历内容详情
            {
                if (!string.IsNullOrWhiteSpace(errMsg)) break; //有错误则跳出

                #region 获取下一篇、上一篇、栏目、站点、父栏目等
                cms_content_ext nextContent = m_ContentDal.GetNext(content.id);
                if (nextContent == null)
                {
                    nextContent = new cms_content_ext();
                    nextContent.isNull = 1;
                }
                cms_content_ext preContent = m_ContentDal.GetPre(content.id);
                if (preContent == null)
                {
                    preContent = new cms_content_ext();
                    preContent.isNull = 1;
                }
                cms_channel channel = m_ChannelDal.Get(content.channelId);
                cms_site site = m_SiteDal.Get(channel.siteId);
                cms_channel parentParentChannel;
                cms_channel parentChannel;
                if (channel.parentId == -1)
                {
                    parentChannel = new cms_channel();
                    parentChannel.parentId = -1;
                }
                else
                {
                    parentChannel = m_ChannelDal.Get(channel.parentId);
                }
                if (parentChannel.parentId == -1)
                {
                    parentParentChannel = new cms_channel();
                    parentParentChannel.parentId = -1;
                }
                else
                {
                    parentParentChannel = m_ChannelDal.Get(parentChannel.parentId);
                }
                #endregion

                if (content.isPageUrl == 1)
                {
                    string filePath = string.Format("/{0}/content{1}.html", site.folder, content.id);
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                    sb.AppendFormat("<script type="text/javascript">window.location = "{0}";</script>", content.pageUrl);
                    sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                    TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                }
                else
                {
                    #region 取模板
                    string templateFileName = string.IsNullOrWhiteSpace(content.template) ? "content.html" : content.template;
                    string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/content/{1}", site.folder, templateFileName));
                    #endregion

                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("site", site);
                    dic.Add("parentParentChannel", parentParentChannel);
                    dic.Add("parentChannel", parentChannel);
                    dic.Add("channel", channel);
                    dic.Add("content", content);
                    dic.Add("preContent", preContent);
                    dic.Add("nextContent", nextContent);

                    string filePath = string.Format("/{0}/content{1}.html", site.folder, content.id);
                    TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                }
                cur  ;
            }
        }
        #endregion

        #region 更新单个content页面
        /// <summary>
        /// 更新单个content页面
        /// </summary>
        public ActionResult UpdateContentSingle(int id)
        {
            try
            {
                cms_content content = m_ContentDal.Get(id);

                #region 获取下一篇、上一篇、栏目、站点、父栏目等
                cms_content_ext nextContent = m_ContentDal.GetNext(content.id);
                if (nextContent == null)
                {
                    nextContent = new cms_content_ext();
                    nextContent.isNull = 1;
                }
                cms_content_ext preContent = m_ContentDal.GetPre(content.id);
                if (preContent == null)
                {
                    preContent = new cms_content_ext();
                    preContent.isNull = 1;
                }
                cms_channel channel = m_ChannelDal.Get(content.channelId);
                cms_site site = m_SiteDal.Get(channel.siteId);
                cms_channel parentParentChannel;
                cms_channel parentChannel;
                if (channel.parentId == -1)
                {
                    parentChannel = new cms_channel();
                    parentChannel.parentId = -1;
                }
                else
                {
                    parentChannel = m_ChannelDal.Get(channel.parentId);
                }
                if (parentChannel.parentId == -1)
                {
                    parentParentChannel = new cms_channel();
                    parentParentChannel.parentId = -1;
                }
                else
                {
                    parentParentChannel = m_ChannelDal.Get(parentChannel.parentId);
                }
                #endregion

                if (content.isPageUrl == 1)
                {
                    string filePath = string.Format("/{0}/content{1}.html", site.folder, content.id);
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<!DOCTYPE html>rn<html>rn<head>rn");
                    sb.AppendFormat("<script type="text/javascript">window.location = "{0}";</script>", content.pageUrl);
                    sb.AppendFormat("</head>rn<body>rn</body>rn</html>rn");
                    TemplateDal.WriteTemplate(Server, filePath, sb.ToString());
                }
                else
                {
                    #region 取模板
                    string templateFileName = string.IsNullOrWhiteSpace(content.template) ? "content.html" : content.template;
                    string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/content/{1}", site.folder, templateFileName));
                    #endregion

                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("site", site);
                    dic.Add("parentParentChannel", parentParentChannel);
                    dic.Add("parentChannel", parentChannel);
                    dic.Add("channel", channel);
                    dic.Add("content", content);
                    dic.Add("preContent", preContent);
                    dic.Add("nextContent", nextContent);

                    string filePath = string.Format("/{0}/content{1}.html", site.folder, content.id);
                    TemplateDal.WriteTemplate(Server, filePath, Replace(templateHtml, dic));
                }

                var json = new
                {
                    ok = "OK",
                    url = string.Format("/{0}/content{1}.html", site.folder, content.id)
                };
                return Json(json);
            }
            catch (Exception ex)
            {
                var json = new
                {
                    ok = ex.Message
                };
                return Json(json);
            }
        }
        #endregion

        #region 更新error页面
        /// <summary>
        /// 更新error页面
        /// </summary>
        public void UpdateError()
        {
            List<cms_site_ext> siteList = m_SiteDal.GetListAll();
            foreach (cms_site_ext site in siteList) //遍历站点
            {
                string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/error.html", site.folder));
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("site", site);
                dic.Add("parentParentChannel", new cms_channel());
                dic.Add("parentChannel", new cms_channel());
                dic.Add("channel", new cms_channel() { id = -999 });
                TemplateDal.WriteTemplate(Server, string.Format("/{0}/error.html", site.folder), Replace(templateHtml, dic));
            }
            cur  ;
        }
        #endregion

        #region 更新comments页面
        /// <summary>
        /// 更新comments页面
        /// </summary>
        public void UpdateComments()
        {
            List<cms_site_ext> siteList = m_SiteDal.GetListAll();
            foreach (cms_site_ext site in siteList) //遍历站点
            {
                List<cms_channel_ext> channelList = m_ChannelDal.GetList(string.Format("channel.siteId={0}", site.id));
                cms_channel_ext channel = channelList.Find(a => a.pageUrl == "comments.html");

                if (channel != null)
                {
                    cms_channel parentParentChannel;
                    cms_channel parentChannel;
                    if (channel.parentId == -1)
                    {
                        parentChannel = new cms_channel();
                        parentChannel.parentId = -1;
                    }
                    else
                    {
                        parentChannel = m_ChannelDal.Get(channel.parentId);
                    }
                    if (parentChannel.parentId == -1)
                    {
                        parentParentChannel = new cms_channel();
                        parentParentChannel.parentId = -1;
                    }
                    else
                    {
                        parentParentChannel = m_ChannelDal.Get(parentChannel.parentId);
                    }
                    cms_content_ext content = ModelHelper.Convert<cms_content_ext>(m_ContentDal.GetByChannelId(channel.id));
                    if (content == null)
                    {
                        content = new cms_content_ext();
                        content.isNull = 1;
                    }
                    string templateHtml = TemplateDal.LoadTemplate(Server, string.Format("/templates/{0}/comments.html", site.folder));
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("site", site);
                    dic.Add("parentParentChannel", parentParentChannel);
                    dic.Add("parentChannel", parentChannel);
                    dic.Add("channel", channel);
                    dic.Add("content", content);
                    TemplateDal.WriteTemplate(Server, string.Format("/{0}/comments.html", site.folder), Replace(templateHtml, dic));
                }
            }
            cur  ;
        }
        #endregion

        #region GetProcess获取更新进度
        [HttpPost]
        [Permission(Code = "Index")]
        public ActionResult GetProcess()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(errMsg))
                {
                    if (total == 0)
                    {
                        total = GetTotal();
                        return Content("0%");
                    }
                    else
                    {
                        int percent = (int)(cur / total * 100);
                        if (percent == 100)
                        {
                            cur = 0;
                            total = 0;
                        }
                        return Content(string.Format("{0}%", percent));
                    }
                }
                else
                {
                    return Content("错误:"   errMsg);
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
        #endregion

        #region GetTotal获取进度总数
        /// <summary>
        /// 获取进度总数
        /// </summary>
        /// <returns></returns>
        public int GetTotal()
        {
            int channelCount = m_ChannelDal.GetList(null).Count;
            int contentCount = m_ContentDal.GetAllCount();
            return channelCount   contentCount   4;
        }
        #endregion

        #region GetPageList生成分页数字列表
        /// <summary>
        /// 生成分页数字列表
        /// </summary>
        private List<PageNum> GetPageList(int currentPage, int pageCount)
        {
            List<PageNum> result = new List<PageNum>();
            result.Add(new PageNum(1));
            if (currentPage > 4)
            {
                result.Add(new PageNum(0, 1));
            }
            for (int i = currentPage - 2; i <= currentPage   2; i  )
            {
                if (i > 1 && i < pageCount)
                {
                    result.Add(new PageNum(i));
                }
            }
            if (pageCount - currentPage > 3)
            {
                result.Add(new PageNum(0, 1));
            }
            result.Add(new PageNum(pageCount));
            return result;
        }
        #endregion

    }
}

  A.添加City_Class_NVelocity.ashx一般处理程序,定义Diationary 键值对并设置对应的值

图片 43图片 44

View Code

图片 45图片 46

{%  extends 'base.html' %}
{% block main1 %}
    <h1>这是main1提交的新内容{{ name1 }}</h1>
{% endblock %}
{% block main2 %}
    <h3>这是main2提交的新内容{{ name2 }}</h3>
{{ train.date }}
{{ time.P_start }}
{% endblock %}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO;


namespace HttpHandler
{
    /// <summary>
    /// City_Class_NVelocity 的摘要说明
    /// </summary>
    public class City_Class_NVelocity : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            //定义键值对
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["BJ"] = "北京";
            dict["SH"] = "上海";
            dict["GZ"] = "广州";

            string strHtml = Template_Nvelocity_C(dict);


            context.Response.Write(strHtml);
        }


        //模板引擎方法
        public string Template_Nvelocity_C(Dictionary<string, string> dict)
        {

            //创建NVlecocity模板引擎的实例对象
            VelocityEngine vlEngine = new VelocityEngine();

            //初始化实例对象,定义对象的部分属性
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));  //指定模板文件所在文件夹
            vlEngine.Init(props);

            //替换模板中的数据
            VelocityContext vltContext = new VelocityContext();
            //vltContext.Put("data", data);  //设置参数,在模板中可以通过$Data读取数据
            vltContext.Put("c", dict);


            //从文件中读取模板
            Template VltTemp = vlEngine.GetTemplate("City.html");
            //合并模板
            StringWriter writer = new StringWriter();
            VltTemp.Merge(vltContext, writer);

            //转化为字符串
            string html = writer.GetStringBuilder().ToString();

            return html;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

View Code

View Code

字典的嵌套,我们把date ,train ,track嵌套在train中

  B.添加City.html, html中通过$c.BJ 来访问 .ashx中定义的对象属性

在模板中使用train.date train.train train.track来渲染。

图片 47图片 48

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    索引获得一般处理程序中的键、值对:<br />
    c.BJ=  $c.BJ<br />
    c.SH=  $c.SH<br />
    c.GZ=  $c.GZ<br />
</body>
</html>

View Code

----------------------------------------------Gif动画演示----------------------------------------------------------

 图片 49

 9,对集合进行遍历   #foreach($item in $list)  ... #end

      City_Class_NVelocity.ashx 中添加 string 数组 和 泛型后,在HTML模板中 遍历读取属性值:

  分别更新,City_Class_NVelocity.ashx和City.html如下:

图片 50图片 51

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO;


namespace HttpHandler
{
    /// <summary>
    /// City_Class_NVelocity 的摘要说明
    /// </summary>
    public class City_Class_NVelocity : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            //定义键值对
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["BJ"] = "北京";
            dict["SH"] = "上海";
            dict["GZ"] = "广州";

            string strHtml = Template_Nvelocity_C(dict);


            context.Response.Write(strHtml);
        }


        //模板引擎方法
        public string Template_Nvelocity_C(Dictionary<string, string> dict)
        {

            //定义数组
            string[] strs = new string[] { "字符串11111", "字符串22222", "字符串33333" };
            //定义泛型 Person对象
            List<Person> persons = new List<Person>();
            persons.Add(new Person { Name = "习大大", Age = 60 });
            persons.Add(new Person { Name = "彭妈妈", Age = 60 });


            //创建NVlecocity模板引擎的实例对象
            VelocityEngine vlEngine = new VelocityEngine();

            //初始化实例对象,定义对象的部分属性
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));  //指定模板文件所在文件夹
            vlEngine.Init(props);

            //替换模板中的数据
            VelocityContext vltContext = new VelocityContext();
            //vltContext.Put("data", data);  //设置参数,在模板中可以通过$Data读取数据
            vltContext.Put("c", dict);
            //添加数组和泛型参数
            vltContext.Put("str", strs);
            vltContext.Put("per", persons);


            //从文件中读取模板
            Template VltTemp = vlEngine.GetTemplate("City.html");
            //合并模板
            StringWriter writer = new StringWriter();
            VltTemp.Merge(vltContext, writer);

            //转化为字符串
            string html = writer.GetStringBuilder().ToString();

            return html;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

View Code

图片 52图片 53

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    索引获得一般处理程序中的键、值对:<br />
    c.BJ=  $c.BJ<br />
    c.SH=  $c.SH<br />
    c.GZ=  $c.GZ<br />

    <br />

    foreace遍历读取字符串数组中的数据: 
    <ul>
        #foreach($s in $str)
        <li>$s</li>
        #end
    </ul>
    <br />
    foreace遍历读取泛型Person中的数据: 
    <ul>
        #foreach($p in $per)
        <li>$p.Name的年龄是$p.Age</li>
        #end
    </ul>
</body>
</html>

View Code

----------------------------------------------Gif动画演示----------------------------------------------------------

图片 54

10,条件语句: #if() #else if() #end ,并且可以嵌套到ForEach中, 例如把上面city.html一段foreach代码中添加 if条件语句:

 <ul>
        #foreach($p in $per)
            #if($p.Age>30)
            <li style="color: red">$p.Name的年龄是$p.Age</li>
            #else
            <li style="color: black">$p.Name的年龄是$p.Age</li>
            #end
        #end
    </ul>

11,模板中调用其他模板 用 #include 

  A. 项目中添加两个模板文件: head.html 和 foot.html

图片 55图片 56

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <b>网站Head部分</b><br /><br />

View Code

图片 57图片 58

<b>网站底部Foot部分,版权声明</b>
</body>
</html>

View Code

  B. #include("head.html") 这样就可以引用模板文件了

图片 59图片 60

#include("head.html")


    索引获得一般处理程序中的键、值对:<br />
c.BJ=  $c.BJ<br />
c.SH=  $c.SH<br />
c.GZ=  $c.GZ<br />

<br />

foreace遍历读取字符串数组中的数据: 
    <ul>
        #foreach($s in $str)
        <li>$s</li>
        #end
    </ul>
<br />
foreace遍历读取泛型Person中的数据: 
    <ul>
        #foreach($p in $per)
            #if($p.Age>30)
            <li style="color: red">$p.Name的年龄是$p.Age</li>
        #else
            <li style="color: black">$p.Name的年龄是$p.Age</li>
        #end
        #end
    </ul>


#include("foot.html")

View Code

12,#parse, 除了实现调用其他模板功能以外 还可以解析NVelocity中的对象的属性。 

  A. 更新Head.html , 直接调用.ashx中的对象   

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <b>网站Head部分</b><br />
    <b>Parse,除了实现Include的包含功能,还可以解析 NVelocity中的元素的值,如 c.BJ=$c.BJ</b> <br />

    <br />

  B.city.html中即可显示出对象属性值:

图片 61

 

总结: 以上为NVelocity的用法,以及HTML模板中对一般处理程序中对象调用方法。

 

 附件: NVelocity DLL    Demo下载

本文由亚洲必赢娱乐游戏发布于亚洲必赢网站登录,转载请注明出处:模板生成静态页面,模板引擎

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。