Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CodeSmith Basic Syntax - CodeTemplate Instruction


May 25, 2021 CodeSmith


Table of contents


Basic syntax - CodeTemplate instructions

The previous articles cover the basics of using CodeSmith templates to automatically generate code and write code templates. It has also been said that the core part of CodeSmith is code templates, starting with this article to introduce the basic syntax of CodeSmith code templates, for Asp.Net programmers, can be said to have met old friends:-) CodeSmith's code templates and Asp.Net Page are almost the same.

This article describes the CodeTemplate directive, which is the only necessary declaration in the template and contains some special properties of the template, including the language in which the template is used, the language in which it is generated, and some descriptions of the template. Like what:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="This is a demo template" %>

Introduction to the parameters:

  • Language: The language in which you develop and write templates, such as C, VB.NET, Jscript, and so on.
  • TargetLanguage: It's just a classification of template code that doesn't affect the generated code language. i s a property of the template that indicates that the template generates the appropriate code based on that language. For example, you can use CodeSmith to generate C-code from any language.
  • Description: For some instructions for a template, the information here is displayed when you select the template in CodeSmith Explorer.
  • Inherits: All CodeSmith templates are inherited by default from CodeSmith.Engine.CodeTemplate, a class that provides some basic functionality for templates, such as page classes on ASP.NET pages, where the properties of inherited classes can be modified, but these new classes must also inherit CodeSmith.Engine.CodeTemplate. CodeSmith can also find this class, and of course you want to introduce a component that contains this class.
  • Src: Src is similar in some ways to inheriting Inherits, which allows you to include some functionality into the template from other classes. The difference between these two properties is that Src can make classes and your templates compile dynamically, while Inherits only allows you to provide a class or component that has already been compiled.
  • Debug: You can determine whether debug symbols can be included in the template. If you set this property to True, you can use the System.Diagnostics.Debugger.Break() method to set break points.
  • LinePragmas: Set to True, the template error will be pointed to the template's source code. Set to False, the template error will be pointed to the compiled source code.
  • ResponseEncoding indicates how the output files of the code template are encoded for all the encoding methods supported by System.Text.Encoding.GetEncoding, and the way the output files are encoded does not change if they already exist and are consistent with the content to be generated.
  • OutputType indicates the output mode of the output file, which can be in three modes:

Normal: Normal mode, the code template output is written to the normal output stream (Response Stream). T race: The output is written to the Trace output stream. None: Control code templates do not output anything, mainly used in the main-from the main template of the template, in some cases without the need for the main template to output anything.

  • NoWarn does not display some compilation warnings, and Warning's ID is separated by a comma, primarily when compiling the VB.Net and the code.
  • ClassName uses the class name that corresponds to Code-Behind, similar to Asp.Net code.
  • Namespace uses the class namespace name that corresponds to Code-Behind.
  • Encoding code templates themselves use the encoding method, the default is UTF-8.