If I have a reusable T4 template, how can I set parameters when recalling it?
Consider these two example files...
Hello.t4:
<## parameter type="System.String" name="Name" #>
Hello <#= this.Name #>!
HelloWorld.tt:
Testing text templates,
<## include file="Hello.t4" #>
How to pass the string "World" as the "Name" parameter of the Hello.t4 template?
Related
I am using the following line in a text template file
<SettingsFilePath>
<#=GetParameterValue("Blah")#>\Deploy\Settings\DeploymentSetting_<#environment.Name#>_<#=workflow.Name#>.xml
</SettingsFilePath>
When I try to modify this line to insert another expression (environment.Type.ToString()) as follows
<SettingsFilePath>
<#=GetParameterValue("Blah")#>\Deploy\Settings\<#=environment.Type.ToString()#>\DeploymentSetting_<#environment.Name#>_<#=workflow.Name#>.xml
</SettingsFilePath>
I get the following error in Visual Studio compilation of TT Templates
An unexpected start or end tag was found within a block. Make sure that you did not mis-type a start or end tag, and that you do not have any nested blocks in the template.
Does anyone know what I am doing wrong? All the <#= #> blocks are properly matched.
Thanks everyone. I worked around it by using String.format as per Aaron's suggestion
<SettingsFilePath><#=string.Format(#"{0}\Deploy\Settings\{1}\DeploymentSetting_{2}_{3}.xml",GetParameterValue("blah"),environment.Type.ToString(), environment.Name, workflow.Name)#></SettingsFilePath>
I use the following code and I not understand the following :
1. why I don't see the definition string name inside the txt file or the for statement since this is inside the tag
2.if I want to see it do I need to use different tag?
<#
string name = "Sop";
#>
Hello there ,<#=name #>
<#
for (int i = 0; i < 5; i++)
{
#>
Hi!
<#
}
#>
The output is
Hello there ,Sop
Hi!
Hi!
Hi!
Hi!
Hi!
Your code in between evaluation tags <# #> so that it will evaluate and run, but it won't be part of the output. If you wish to generate code, don't use the tags. More info about T4 templates as always available on MSDN. Especially this link pointing to MSDN is quite thorough about code generating from xml file storing definitions.
I am trying to use a Razor template to produce a .aspx page as output. I'm not having luck looking in the documentation for how to do this. The Page and namespace declarations are breaking the template:
<%# Page Language="C#" Title="#Page.Metadata.browser_title" %>
<%# Import Namespace="System.Xml" %>
These are causing this error:
TemplateCompileException: CS1501: No overload for method 'Write' takes 0 arguments Line 27 Column 1: Write();
I assume this is because Razor templates using C# syntax makes the two conflict, since the declarations above use "#". Is there a way to get them to work together so a Razor template can produce an output with C# in the rendered product after the template is run? The example above also shows how the value for "Title" needs to be rendered out of the template.
The #s in the <% are invalid Razor syntax.
You need to escape them by writing <%##.
If you like Razor, maybe you should check out DD4T (http://code.google.com/p/dynamic-delivery-4-tridion/). It allows you to build a web site using ASP.Net MVC with Razor views.
You could write a C# TBB to add the tags after all your Razormediator templates or even better if you could add after the Default Finish Actions(if you're using one).
Quick and dirty sample code...
Item OutputItem = package.GetByName(Package.OutputName);
string OutputText = OutputItem.GetAsString();
// Page tag declaration..
string pagePretags = #"<<TWO LINES OF DECLARATIONs..>>"
string FinalOutputText = pagePretags + OutputText ;
OutputItem.SetAsString(FinalOutputText);
Hope this helps..
Given this powershell code:
$drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]'
$drivers.Add("nitrous","vx")
$drivers.Add("directx","vd")
$drivers.Add("openGL","vo")
Is it possible to initialize this dictionary directly without having to call the Add method. Like .NET allows you to do?
Something like this?
$foo = New-Object 'System.Collections.Generic.Dictionary[String,String]'{{"a","Alley"},{"b" "bat"}}
[not sure what type of syntax this would involve]
No. The initialization syntax for Dictionary<TKey,TValue> is C# syntax candy. Powershell has its own initializer syntax support for System.Collections.HashTable (#{}):
$drivers = #{"nitrous"="vx"; "directx"="vd"; "openGL"="vo"};
For [probably] nearly all cases it will work just as well as Dictionary<TKey,TValue>. If you really need Dictionary<TKey,TValue> for some reason, you could make a function that takes a HashTable and iterates through the keys and values to add them to a new Dictionary<TKey,TValue>.
The C# initializer syntax isn't exactly "direct" anyway. The compiler generates calls to Add() from it.
For those who like to really use Dictionary.
In my case I had to use another c# dll which has a dictionary as parameter:
New-Object "System.Collections.Generic.Dictionary[String,String]"
This was probably not possible back in 2011
$d = [System.Collections.Generic.Dictionary[String,Object]]::new()
#works now.
$d.Add('keyvalue', #{x=1; y='abc'})
#Evaluate dictionary
$d
<# outputs
Key Value
--- -----
keyvalue {y, x}
#>
#evalueate contains key:
$d.Keys.Contains('keyvalue')
<# outputs
True
#>
# Evaluate the value using the key
$d['keyvalue']
<# outputs
Name Value
---- -----
y abc
x 1
#>
#Evaluate the y property of the object
$d['keyvalue'].y
<# outputs
abc
#>
# Evaluate the x property of the object
$d['keyvalue'].x
<# outputs
1
#>
I have a class CURPRFC that contains the following:
static public string CalcRFC(string firstname, string lastname, string middlename, string date)
I'm calling this method/function in my codebehind as follows:
CURPRFC.CalcRFC(txtfirstname.text, txtlastname.text, txtmidlename.text, txtdate.text)
where each of the values are textbox values. However, I receive an error stating:
System.Web.UI.WebControls.TextBox' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?)
My C# is a bit weak. I think I need to change my textbox text values to strings, but am not sure of how to go about this. Am I on the right track?
Your help is most appreciated!
Thanks,
Sid
You need to remember that C# is case-sensitive. TextBox doesn't have a text property, but it does have a Text property:
CURPRFC.CalcRFC(txtfirstname.Text, txtlastname.Text,
txtmidlename.Text, txtdate.Text);
(I'd also suggest that you think about naming clarity - conventionally C# variables use camelCasing to indicate word breaks, and a class named "CURPRFC" is far from self-explanatory.)