模版引擎之简单实现
2012-07-18 11:45
302 查看
模版,大家肯定都比较熟悉的一个概念,刚学C#(Java)那会老师就告诉我们,类是对象的模版。今天写这个模版其实是我用于生成js代码的,当然不限于生成js,其实跟codesmith有着差不多的功能,只是没那么强大,下面我写一些思路
我这个模版目前用于生成Ext的Grid和添加,编辑 表单,所以主角的又(怎么会是又呢,呵呵)是数据库表,不过这些信息被我存到前一篇博客里提到的Model里去了(通过特性的方式)。
出于方便,我还是把Model的代码贴一下:
MathTag
using System;
using XDbFramework;
namespace TemplateEngine
{
public class MathTag
{
private string _sourceString;
private TagIndex pkTag;
private TagIndex fkTag;
private TagIndex textTag;
private TagIndex dateTag;
private TagIndex numberTag;
private TagIndex moneyTag;
private TagIndex boolTag;
private TagIndex everyTag;
public MathTag(string sourceString)
{
_sourceString = sourceString;
pkTag = TagIndex.GetTag(sourceString, Generator.PkColumnTagName);
fkTag = TagIndex.GetTag(sourceString, Generator.FkColumnTagName);
textTag = TagIndex.GetTag(sourceString, Generator.TextColumnTagName);
dateTag = TagIndex.GetTag(sourceString, Generator.DateColumnTagName);
numberTag = TagIndex.GetTag(sourceString, Generator.NumberColumnTagName);
moneyTag = TagIndex.GetTag(sourceString, Generator.MoneyColumnTagName);
boolTag = TagIndex.GetTag(sourceString, Generator.BoolColumnTagName);
everyTag = TagIndex.GetTag(sourceString, Generator.EveryColumnTagName);
}
public TagIndex GetTagIndexByColumn(ColumnAttribute m)
{
if (m.KeyType == KeyTypeEnum.PrimaryKey && pkTag.FindThisTag)
{
return pkTag;
}
if ((m.CType == typeof(DateTime) || m.CType == typeof(DateTime?)) && dateTag.FindThisTag)
{
return dateTag;
}
if ((m.CType == typeof(bool) || m.CType == typeof(bool?)) && boolTag.FindThisTag)
{
return boolTag;
}
if ((m.CType == typeof(float) || m.CType == typeof(decimal) || m.CType == typeof(double) || m.CType == typeof(float?) || m.CType == typeof(decimal?) || m.CType == typeof(double?)) && moneyTag.FindThisTag)
{
return moneyTag;
}
if ((m.CType == typeof(int) || m.CType == typeof(int?) || m.CType == typeof(long) || m.CType == typeof(long?)) && numberTag.FindThisTag)
{
return numberTag;
}
if (m.CType == typeof(string) && textTag.FindThisTag)
{
return textTag;
}
if (m.KeyType == KeyTypeEnum.ForeignKey && fkTag.FindThisTag)
{
return fkTag;
} if (everyTag.FindThisTag)
{
return everyTag;
}
return null;
}
}
}
好,这里整个过程也就差不多结束了(不知道怎么替换?当然是Replace)
我这个模版目前用于生成Ext的Grid和添加,编辑 表单,所以主角的又(怎么会是又呢,呵呵)是数据库表,不过这些信息被我存到前一篇博客里提到的Model里去了(通过特性的方式)。
出于方便,我还是把Model的代码贴一下:
MathTag
using System;
using XDbFramework;
namespace TemplateEngine
{
public class MathTag
{
private string _sourceString;
private TagIndex pkTag;
private TagIndex fkTag;
private TagIndex textTag;
private TagIndex dateTag;
private TagIndex numberTag;
private TagIndex moneyTag;
private TagIndex boolTag;
private TagIndex everyTag;
public MathTag(string sourceString)
{
_sourceString = sourceString;
pkTag = TagIndex.GetTag(sourceString, Generator.PkColumnTagName);
fkTag = TagIndex.GetTag(sourceString, Generator.FkColumnTagName);
textTag = TagIndex.GetTag(sourceString, Generator.TextColumnTagName);
dateTag = TagIndex.GetTag(sourceString, Generator.DateColumnTagName);
numberTag = TagIndex.GetTag(sourceString, Generator.NumberColumnTagName);
moneyTag = TagIndex.GetTag(sourceString, Generator.MoneyColumnTagName);
boolTag = TagIndex.GetTag(sourceString, Generator.BoolColumnTagName);
everyTag = TagIndex.GetTag(sourceString, Generator.EveryColumnTagName);
}
public TagIndex GetTagIndexByColumn(ColumnAttribute m)
{
if (m.KeyType == KeyTypeEnum.PrimaryKey && pkTag.FindThisTag)
{
return pkTag;
}
if ((m.CType == typeof(DateTime) || m.CType == typeof(DateTime?)) && dateTag.FindThisTag)
{
return dateTag;
}
if ((m.CType == typeof(bool) || m.CType == typeof(bool?)) && boolTag.FindThisTag)
{
return boolTag;
}
if ((m.CType == typeof(float) || m.CType == typeof(decimal) || m.CType == typeof(double) || m.CType == typeof(float?) || m.CType == typeof(decimal?) || m.CType == typeof(double?)) && moneyTag.FindThisTag)
{
return moneyTag;
}
if ((m.CType == typeof(int) || m.CType == typeof(int?) || m.CType == typeof(long) || m.CType == typeof(long?)) && numberTag.FindThisTag)
{
return numberTag;
}
if (m.CType == typeof(string) && textTag.FindThisTag)
{
return textTag;
}
if (m.KeyType == KeyTypeEnum.ForeignKey && fkTag.FindThisTag)
{
return fkTag;
} if (everyTag.FindThisTag)
{
return everyTag;
}
return null;
}
}
}
好,这里整个过程也就差不多结束了(不知道怎么替换?当然是Replace)
相关文章推荐
- 最简单的基于js实现的模版引擎
- asp.net下的简单的模版引擎实现思路
- 模版引擎之简单实现
- 实现一个最简单的模版引擎
- 简单小巧js模板引擎实现
- JavaScript模版引擎的基本实现方法浅析
- thml5+js 实现简单的2d引擎
- 一个简单的vector模版,实现了vector的简单功能
- c++模版及模版类简单实现
- 简单双向链表(C++模版技术实现)
- 简单链式栈(C++模版技术实现)
- Python实现的简单模板引擎功能示例
- Win32实现简单游戏引擎
- JS模版引擎实现原理
- HTML简单的游戏引擎实现第一篇
- 一、p2物理引擎在egret中的应用:简单贴图的实现
- 【转载】从零实现3D图像引擎:(2)画2D直线不简单
- 简单功能的正则表达式引擎实现
- 【原创】javascript模板引擎的简单实现
- 从零实现3D图像引擎:(2)画2D直线不简单