Get the TemplateId in a custom LPT template

Posts   
 
    
basik
User
Posts: 123
Joined: 20-Aug-2012
# Posted on: 07-Aug-2014 14:20:35   

Is this possible? Using LLBLGenPro 4.2

I'd like to retrieve the id of the template or even the filename at runtime. Of course, as it is a custom template I could just create a variable to do this and write it out as a comment in the generated source.

Thank you.


<%
    Project currentProject = _executingGenerator.ProjectDefinition;

        //Something like this
        //string templateId = _executingGenerator.GetTemplateId();

    TypedListDefinition typedList = (TypedListDefinition)_activeObject;
    var fields = typedList.Fields;

    // typed lists can't have private fields, as the context has to fetch the data.
    bool publicGettersSetters = currentProject.GetRealBoolSettingValue("PublicPropertyGettersAndSetters");
    string propertyAccessor =  publicGettersSetters ? "public" : "internal";
    string setterAccessor = publicGettersSetters ? "internal" : string.Empty;
    var nullableAliases = typedList.GetAllNullableEntityAliasesInSourceSet();
    _executingGenerator.StoreValueInRunQueueCache("TypedListClassesNamespace", _executingGenerator.RootNamespaceToUse + ".POCOTypedListClasses");
    
%>

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 08-Aug-2014 09:38:33   

The templateID is in the variable '_templateID' like the IGenerator is in _executingGenerator, so you can just refer to _templateID which is a string.

The filename is a bit difficult. The output is written by the StreamWriter __outputWriter (2 underscores). Through this streamwriter you can get the BaseStream which is a FileStream so you can cast that to a FileStream and through that get the filename.

What is the use case exactly to know the filename the template is written to btw? As you can't do anything with the filename anyway?

Frans Bouma | Lead developer LLBLGen Pro
basik
User
Posts: 123
Joined: 20-Aug-2012
# Posted on: 08-Aug-2014 10:15:07   

Actually the _templateId is just what I need. The filename is not so important. The use case is that we have created a fair number of custom templates to implement a service layer repository pattern.

Sometimes its just hard to find the template that needs tweaking because there are so many generated classes and projects. I'm always looking at the generated code and having to go back to the preset tasks to see what the templateId is and then finding it in the Template Bindings.

By outputing this to the generated files as a comment makes it easy to find the template in the template binding tab. As the templates become established, changes will be less frequent and the comments will be a useful reminder.

It is a convenience only and by no means a feature request candidatewink Thank you.