CodeSmith Community
Your Code. Your Way. Faster!

Code Behind SubTemplates

rated by 0 users
This post has 13 Replies | 2 Followers

Not Ranked
Posts 7
Points 215
Debbie Posted: 12-17-2008 5:14 PM

Is there any way to create and render a subtemplate from a code behind file?

Thanks.

  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

Yes, this is possible, but i would recommend not creating the physical template in a code behind.  Please watch the following video.

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 55
Not Ranked
Posts 7
Points 215

I have created an base class and the inheriting objects need to override certain methods which render different subtemplates depending on the object type. 

The subtemplate name in the register tag is found when used in a runat="template" script but the code behind file doesn't see it.  How can I a register the subtemplate from a code behind?

  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

Could you send me your samples and I'll take a look at it. I've done some updates in CodeSmith 5.0.4 to make this possible theoretically but I'm not sure that it is still 100% supported...

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 35
Not Ranked
Posts 7
Points 215
Hi.  Thanks.  I created a really simple example that uses the same architecture.

As you will see in the main template (AnimalTemplate.cst) there are 3 different ways of creating the subtemplates.  Now it should be working (if it throws an error on the Helper property - just run it again - it works the second time - not sure why that happens either).  Uncomment the other methods one at a time in the main template to see the error I am getting.  I really need to use the object method, but the simple code behind method would help as well. 

Thanks,
Debbie

On Thu, Dec 18, 2008 at 11:49 AM, blake05 <bounce-blake05@codesmithsupport.com> wrote:

Hello,

Could you send me your samples and I'll take a look at it. I've done some updates in CodeSmith 5.0.4 to make this possible theoretically but I'm not sure that it is still 100% supported...

Thanks
-Blake Niemyjski





  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

Thanks, I'l take a look at this.

 P.S I'll be out later today and part of tomorrow.

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 5
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

I don't know why you have your code behind setup like that.. I think that your virtual methods should be in animal template and your cat and dog templates should do the work...

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 55
Not Ranked
Posts 7
Points 215
I want to run one template (lets say animal template) and at run time, it should know which inheriting template to use.  I don't want to run the dog or the cat template itself.

Ignoring the way the code is setup, the simpler question still exists.  How can I create a subtemplate from a code-behind file?  See GenerateAnimal procedure. 

Thanks, Debbie

On Mon, Jan 5, 2009 at 2:56 PM, blake05 <bounce-blake05@codesmithsupport.com> wrote:

Hello,

I don't know why you have your code behind setup like that.. I think that your virtual methods should be in animal template and your cat and dog templates should do the work...

Thanks
-Blake Niemyjski





  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

"How can I create a subtemplate from a code-behind file?  See GenerateAnimal procedure."

I would create three seperate templates and reference an assembly that has a class that inheirts sqlcodetemplate call this AnimalTemplate. Then have your virutal method in that. And then in your logic create the correct template based on what the user selected as a property... There is no reason why you need a helper class do to accomplish this.

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 55
Not Ranked
Posts 7
Points 215
I made the example more simplistic than it really is.  There are actually multiple subtemplates involved.  Is there no way that the code written below can work from a code-behind file?

MyTemplate t = this.Create<MyTemplate>();
t.Render(Response);


On Mon, Jan 5, 2009 at 3:46 PM, blake05 <bounce-blake05@codesmithsupport.com> wrote:

Hello,

"How can I create a subtemplate from a code-behind file?  See GenerateAnimal procedure."

I would create three seperate templates and reference an assembly that has a class that inheirts sqlcodetemplate call this AnimalTemplate. Then have your virutal method in that. And then in your logic create the correct template based on what the user selected as a property... There is no reason why you need a helper class do to accomplish this.

Thanks
-Blake Niemyjski





  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

MyTemplate t = this.Create<MyTemplate>();
t.Render(Response);

That should be no problem. The problem is what your passing into Render() and how your dynamically switching inheirited templates. This should be done by control structures instead of helper classes.

Thanks
-Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 35
Not Ranked
Posts 7
Points 215
OK.  You can take out all of the Helper classes and calls to them.  I am still getting an error on that code.

Error: Example.csp(0,0) Exception has been thrown by the target of an invocation. --> Object reference not set to an instance of an object.   at CodeSmith.Engine.CodeTemplate..ctor()
   at CodeSmith.BaseTemplates.OutputFileCodeTemplate..ctor()
   at CodeSmith.BaseTemplates.SqlCodeTemplate..ctor()
   at Examples.AnimalTemplate..ctor()
   at Examples.DogTemplate..ctor()

I think the code behind does not recognize the register tag in the template file.  Is there a way to register a subtemplate from the code behind?

On Mon, Jan 5, 2009 at 4:51 PM, blake05 <bounce-blake05@codesmithsupport.com> wrote:

Hello,

MyTemplate t = this.Create<MyTemplate>();
t.Render(Response);

That should be no problem. The problem is what your passing into Render() and how your dynamically switching inheirited templates. This should be done by control structures instead of helper classes.

Thanks
-Blake Niemyjski





  • | Post Points: 35
Top 10 Contributor
Posts 1,401
Points 51,130

Hello,

Can you send me your updated templates.

Thank you,
Blake Niemyjski

Blake Niemyjski

CodeSmith Tools, LLC. Software Development Engineer

Blog: http://windowscoding.com/blogs/blake/

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 35
Not Ranked
Posts 7
Points 215
Sure.  Attached.

On Tue, Jan 6, 2009 at 8:41 AM, blake05 <bounce-blake05@codesmithsupport.com> wrote:

Hello,

Can you send me your updated templates.

Thank you,
Blake Niemyjski





  • | Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems