Welcome to the CodeSmith Community!

RE: Using sub-templates now

Support Forums

Feel free to ask any questions about CodeSmith here.

Using sub-templates now

  • rated by 0 users
  • This post has 7 Replies |
  • 0 Followers
  • Joe,

    Instead of calling MySubTemplate.Render(Response) call MySubTemplate.RenderToFile("<somepath>").

    Thanks,
    Eric J. Smith

    Eric J. Smith
    Founder / CEO
    CodeSmith Tools, LLC

  • Joe,

    Thanks for converting the sub-template sample to VB!

    Also, on your VB conversion be sure to put <Browsable(false)> _ in front of your MySubTemplate property or you won't be able to save and load your property set.

    Thanks,
    Eric J. Smith

    Eric J. Smith
    Founder / CEO
    CodeSmith Tools, LLC

  • Eric,
    I added the <Browsable(False)> _  attribute as you suggested. It works fine.
    I found a strange bug while doing this though. In an earlier post I mentioned that I had a problem with the C# template complaining about an invalid path to the subtemplate. I think I figured out what is going on.
    I have templates stored in 2 different folders. Your samples in one and my own in another. When I loaded the property set from XML I loaded it from one of my folders but the sample templates were loaded from your folders. So the program became confused about which Path to use and it used the one to my folders. But there was no subtemplate over there and I got the error. To prove it I copied the subtemplate to the same folder that the XML file is in and it ran fine.
    I have uploaded the changed file.
    Joe





    PS - Old template was downloaded twice.
    See latest posting for latest sample.

    Post Edited (Joe Fallon) : 10/16/2003 5:22:54 PM GMT

     
  • Joe,

    You should specify the fully qualified path to the template like this:

    Dim compiler As New CodeTemplateCompiler(Me.CodeTemplateInfo.DirectoryName + "subtemplate.cst")

    Thanks,
    Eric J. Smith

    Eric J. Smith
    Founder / CEO
    CodeSmith Tools, LLC

  • Eric,
    I modified the template to include this "hidden feature" ...  <g>
    Dim compiler As New CodeTemplateCompiler(Me.CodeTemplateInfo.DirectoryName + "subtemplate.cst")
    It resolved the issue that arises when loading the property set from an XML file in a separate folder.
    I have removed the older sample code too.
    Joe



     PS - Downloaded once.
    See latest post for latest version.



    Post Edited (Joe Fallon) : 10/16/2003 7:50:36 PM GMT

     
  • Eric,
    I tried this MySubTemplate.RenderToFile("<somepath>") but it failed.

    The error mentioned a missing parameter so I added a 2nd string: "".
    Then I got a type conversion error for Boolean so I changed my 2nd parameter to True.
    Then it worked fine.

    Apparently the 2nd parameter is used for overwriting (True) or NOT overwriting (False) an existing file with that name.

    Joe
     
  • VB-SubTemplateSample.zip
    Eric,
    I added the ability to output the results to individual files.
    I was looking for a way to do this so I can loop through many tables and create 1 class per file instead of one huge file with many classes in it. This works perfectly.
     
    Thanks for your help!
     
    I think I am done playing with this sample now.
    Sorry about the frequent revisions! rolleyes
     
    Joe
     
     
     
  • I've tried using your samples to create my files (1 file per class), but I've got a NullReferenceException raised.

    It seems it's raised by my subTemplate...

    The problem is that if I take my tables one per one and run the template, I've got no errors... But when using your samples, I4ve got this NullReference for the 3rd Table.

    Because the NullReferenceException is raised at execution time with my subTemplate compiled, I'm unable to watch the code to understand what is wrong...

    If you have any suggestion ? :/
  • I've found the solution... It was because of a *** public variable used in my SubTemplate, who wasn't reinitialized between uses of the subtemplate...

    Many thanks for this usefull sample :)
  • Funny story.

    The first time I used a a template to do batch updates, I couldn't figure out where I was inserting a section of code that should only have been in the first one.

    It seems that I was forgetting to null out all of my flags, collections, and temporary arrays at the end of the template :O

    Took me a while to piece that together.

    So, now in templates I intend to use for subtemplates I put in two methods before I do anything else. InitializeTemplate and CleanupTemplate. I then immediately put calls to them at the end and beginning of the template. Only after that do I actually start on the template, and slowly add things to those two functions as I introduce temporary helpers.

    It's a lot more work, but it saves time when you're wondering how a piece of code got snuck in somewhere :)
  • Jason,

    I've been meaning to update this post. What really should be happening is you should create a new instance of the template for each rendering. That way you are guaranteed to be in a clean and consistent state. Instead of caching the CodeTemplate instance, I've started to cache the CodeTemplateCompiler instance after compiling the template and then I call CreateInstance on it to get my CodeTemplate instance. I will try and update the post today.

    Thanks,
    Eric J. Smith

    Eric J. Smith
    Founder / CEO
    CodeSmith Tools, LLC

  • Ah yes, that does make a lot more sense and is definately safer.

    I think most of the expense is in compiling the template, so creating new instances should be fast?


    Just a question, but how does studio handle compiling and running templates? Since it's impossible to unload a class, I'm imagining that it would compile/load them into a seperate appdomain and then unload the appdomain to clean up?

    Is it possible to "pre-compile" a group of templates into an assembly to use in code elsewhere? I seem to remember there is a method on the compiler that will create an assembly on the filesystem, but I think that was only for one compiled template?

    (I know I could cut n paste the generated source into a project, but I was wondering if there was a programatic way to do it :)
  • Jason,

    Yes, most of the expense is in compiling and creating an instance of the template is very fast and cheap.

    Currently, the templates are running in the same AppDomain. I know this is an issue, but I ran into some trouble trying to get them working in a seperate AppDomain. I am currently in the process of trying to resolve those issues. If your interested, the problems I'm having are that the PropertyGrid control doesn't seem to work with proxied objects. I tried implementing ICustomTypeDescriptor in CodeTemplate, but that didn't seem to work.

    There isn't currently a way to compile a group of templates into an assembly. I am planning to add this at some point and then allow the template explorer control to read an assembly as if it was a folder. That would provide a good mechanism to allow for closed source commercial templates.

    Thanks,
    Eric J. Smith

    Eric J. Smith
    Founder / CEO
    CodeSmith Tools, LLC

  • Page 2 of 2 (28 items) 12