用CodeSmith调用子模板

用CodeSmith调用子模板

  • 方式一:自定义子模板类
  • 方式二:注册子模板

自定义子模板类

1.当前类

  
  public class TemplateHelp {
        CodeTemplate _rootTemplate;

        public TemplateHelp(CodeTemplate codetemplate) {
            _rootTemplate = codetemplate;
        }

        private CodeTemplate TemplateComiler(string path) {
            var compiler = new CodeTemplateCompiler(path);
            compiler.Compile();
            if (compiler.Errors.Count == 0) {
                return compiler.CreateInstance();
            }
            else {
                for (int i = 0; i<compiler.Errors.Count; i++) {
                    _rootTemplate.Response.WriteLine(compiler.Errors[i]);
                }
            }
            return null;
        }

        public CodeTemplate LoadTemplate(string path) {
            return TemplateComiler(path);
        }

        public CodeTemplate LoadTemplateWithCopyProperties(string path) {
            var subTemp = LoadTemplate(path);
            _rootTemplate.CopyPropertiesTo(subTemp);
            return subTemp;
        }

        public void LoadTemplateWithRenderResponse(string path) {
            var subTemp = LoadTemplate(path);
            _rootTemplate.CopyPropertiesTo(subTemp);
            subTemp.Render(_rootTemplate.Response);
        }
    }

2.调用如下
this为当前模板类
子模板路径,我比较喜欢用相对路径

    var temp=new TemplateHelp(this);
    var tmpSG = temp.LoadTemplateWithCopyProperties(@"..\QM\test.cst");

@"..\QM\test.cst"用子模板类路径

二.注册子模板类

先注册子模板
<%@ Register Name="tmp" Template="Business\Test.cst"%>

调用如下:

 
   var scriptSet = this.Create<tmp>();
    this.CopyPropertiesTo(scriptSet);
    scriptSet.Render(this.Response);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值