Our template engine is the .NET
compiler itself, this way the generator gives you the ultimate power to write any
code as a template and generate complex code patterns based on provided templates
or based on your coding skills on writing new templates using your favorite language
C# or VB.
Writing your first template
is an exciting and nerve racking time. You may know a lot about computers, coding,
or maybe the benefits of using O2 Broadband, or a fast speed internet connection
with another company. However when it comes to templates you might feel a little
lost. With this step by step guide, you will hopefully be well on your way in no
time at all to having a perfectly coded template.
Simply normal c# or VB code
syntax will be used in your template, check below sample.
<% string myTitle = "Hello world!";
%>
<html>
<head>
<title><%
Response.Write(myTitle);
%></title>
</head>
<body>
<%=myTitle%>
</body>
</html>
The output is
<html>
<head>
<title>Hello world!</title>
</head>
<body>
Hello world!
</body>
</html>
All template code will be written
inside <% %>
the same you do with ASP.NET inline code, and you can write commands like Response.Write()
and it will work.
But you will start writing the
first template you will need to access the database objects. This is why we introduced
this new namespace "GeneratorContext" which contains all the commands
we implemented to help you and to facilitate writing your code templates and to
let you access the database structure and elements.
Lets assume that you have database
table (employees) and you need to generate stored procedure to insert data, below
example will show how to generate generate some SQL code, first of all make sure
that your connection string is valid and the connection is active.
<%
foreach(Table
table in GeneratorContext.Tables)
{
%>
CREATE
PROCEDURE <%=table.Name%>_Insert
(
<%
Response.Write(GeneratorContext.WriteFields(table.Fields,"\n\t@{fn} {st}({ss})",","));
%>
)
AS
BEGIN
-- Generated at <%=DateTime.Now.ToString()%>
-- SET NOCOUNT ON
added to prevent extra result sets from
-- interfering with
SELECT statements.
SET
NOCOUNT ON;
INSERT
INTO <%=table.Name%>
(<%=GeneratorContext.WriteCommaSeperated(table.Fields)%>)
VALUES
(<%=GeneratorContext.WriteCommaSeperated(table.Fields,"@")%>)
End
GO
<%
}
%>
The output is
CREATE PROCEDURE
Employee_Insert
(
@EmployeeID int(4),
@EmployeeName nvarchar(50)
)
AS
BEGIN
-- Generated at 9/24/2009
2:17:07
-- SET NOCOUNT ON added to prevent extra result sets
from
-- interfering with SELECT statements.
SET
NOCOUNT ON;
INSERT
INTO Employee
(EmployeeID,EmployeeName)
VALUES
(@EmployeeID,@EmployeeName)
End
GO