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.
Related
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?
How can I validate Razor template, which is used to render mail template.
string template = #"
My template
#if (Model.Condition { <span>is true</spaM> }"
I made two mistakes: missing closing bracket and wrong closing tag.
I need to validate it and known what should I fix (the best is to known in which line). I would like to use native Razor methods.
If I understand correctly, you want to be notified that the code you've written in the Template is invalid HTML.
If so, I'm afraid there is no easy way. The Template is purely producing text that you specify to go out to the response.
It may not even be HTML - could be JavaScript or a number of other outputs - it's just a string of text.
You may have a Template that produces the start of a table, another that produces the body, and another that produces the footer and end table tags. Each of these would produce invalid HTML on their own, but output one after the other would produce a valid HTML table. (That's a lot of produces there - sorry).
What would make it invalid is the parser of the HTML - i.e. the browser. You would only be able to validate your Template output when it is in a complete document that can then be parsed.
You mean ?
#{
string template = Model.Condition ? "My template <span>is true</span>" : "";
}
string MyString = string.empty;
#if(Model.Condition)
{
MyString ="<span>"+ "is true"+"</span>";
}
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 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
#>