本文整理汇总了C#中StringTemplate类的典型用法代码示例。如果您正苦于以下问题:C# StringTemplate类的具体用法?C# StringTemplate怎么用?C# StringTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringTemplate类属于命名空间,在下文中一共展示了StringTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Basic
static void Basic()
{
StringTemplate stringTemplate = new StringTemplate("hello <data>");
stringTemplate.Add("data", "world");
Console.WriteLine(stringTemplate.Render());
}
示例2: Apply
//= null
public static string Apply(string template, Dictionary<string, object> @params )
{
StringTemplate st = new StringTemplate(template);
AddDefaultAttributes(st);
if (@params != null) AddAttributes(st, @params);
return st.ToString();
}
示例3: StringTemplateView
public StringTemplateView(StringTemplate template)
{
//null check
if (template == null) throw new ArgumentNullException("template");
//set template
_template = template;
}
示例4: TestStringTemplate
static void TestStringTemplate()
{
string text = "$entity.Account$您好!<br/> 请点击连接重置您的秘密<br><a href=\"$url$\">$url$</a>";
StringTemplate st = new StringTemplate(text);
st.SetAttribute("entity", new Demo() { Account = "fengpengbin" });
st.SetAttribute("url", "http://www.aporeboy.com/vail");
Console.WriteLine(st.ToString());
}
示例5: Populate
public string Populate(ParameterSet parameterSet)
{
var t = new StringTemplate()
{
Text = Text,
};
return t.Populate(parameterSet.Parameters);
}
示例6: Export
public string Export(IList dbObjects, string templatePath)
{
StringTemplate template = new StringTemplate();
var text = File.ReadAllText(templatePath);
//remove all tabs - it used only to format template code, not output code
template.Template = text.Replace("\t", "");
template.SetAttribute("dbObjects", dbObjects);
var output = template.ToString();
return output;
}
示例7: Template
public Template(string name)
{
var assembly = Assembly.GetCallingAssembly();
var stream = assembly.GetManifestResourceStream(name);
if (stream == null)
{
var candidates = string.Join(", ", assembly.GetManifestResourceNames());
throw new FileNotFoundException(string.Format("cannot find embedded resource {0} in assembly {1}, candidate templates include [{2}]", name, assembly.GetName(), candidates));
}
var contents = new StreamReader(stream).ReadToEnd();
template = new StringTemplate(contents);
}
示例8: Generate
public static string Generate(string input, Dictionary<string, object> data)
{
var template = new StringTemplate(input);
foreach (var d in data)
template.SetAttribute(d.Key, d.Value);
string result = template.ToString();
;
template.Reset();
data.Clear();
template = null;
return result;
}
示例9: ListIterator_MultipleItems
public void ListIterator_MultipleItems()
{
// Arrange
var template = new StringTemplate("$user:{ u | <p>$u.FirstName$ is a super user? $u.IsSuperUser$. But his last name is $u.LastName$.</p>}$");
var user = new { FirstName = "Ian", LastName = "Robinson", IsSuperUser = false };
var userList = (new[] {user}).ToList();
userList.Add(new { FirstName = "Gertrude", LastName = "Schmuckbucket", IsSuperUser = true});
template.SetAttribute("user", userList);
// Act
var renderedText = template.ToString();
// Assert
Assert.AreEqual(renderedText, "<p>Ian is a super user? False. But his last name is Robinson.</p><p>Gertrude is a super user? True. But his last name is Schmuckbucket.</p>");
}
示例10: ListIterator_SingleItem
public void ListIterator_SingleItem()
{
// Arrange
var template = new StringTemplate("$user:{ u | $u.FirstName$ is a super user? $u.IsSuperUser$. But his last name is $u.LastName$.}$");
template.SetAttribute("user", new
{
FirstName = "Ian",
LastName = "Robinson",
IsSuperUser = false
});
// Act
var renderedText = template.ToString();
// Assert
Assert.AreEqual(renderedText, "Ian is a super user? False. But his last name is Robinson.");
}
示例11: GetParserCreationTemplate
public override StringTemplate GetParserCreationTemplate()
{
StringTemplate createParserST =
new StringTemplate(
" Profiler2 profiler = new Profiler2();\n" +
" $parserName$ parser = new $parserName$(tokens,profiler);\n" +
" profiler.setParser(parser);\n"
);
if ( !Debug )
{
createParserST =
new StringTemplate(
" $parserName$ parser = new $parserName$(tokens);\n"
);
}
return createParserST;
}
示例12: NestedProperties
public void NestedProperties()
{
// Arrange
IDictionary<string, object> ret = new Dictionary<string, object>();
ret["elem1"] = true;
ret["elem2"] = false;
var nestedObj = new Dictionary<string, object>();
nestedObj["nestedProp"] = 100;
ret["elem3"] = nestedObj;
var template = new StringTemplate("$elem1$ or $elem2$ and value: $elem3.nestedProp$") { Attributes = ret };
// Act
var renderedText = template.ToString();
// Assert
Assert.AreEqual(renderedText, "True or False and value: 100");
}
示例13: AddAttributes
public static void AddAttributes(StringTemplate st, Dictionary<string, object> @params)
{
foreach(string key in @params.Keys) {
object val = @params[key];
if (val != null) {
if (val is string && String.IsNullOrEmpty(val as string))
continue;
/*
if (val is ArrayList)
{
//st.SetAttribute(key, (val as ArrayList).ToArray());
ArrayList al = val as ArrayList;
foreach (object o in al)
st.SetAttribute(key, o);
}
else
*/
st.SetAttribute(key, val);
}
}
}
示例14: GetLexerTestFileTemplate
public override StringTemplate GetLexerTestFileTemplate()
{
StringTemplate outputFileST = new StringTemplate(
"import org.antlr.runtime.*;\n" +
"import org.antlr.runtime.tree.*;\n" +
"import org.antlr.runtime.debug.*;\n" +
"\n" +
"class Profiler2 extends Profiler {\n" +
" public void terminate() { ; }\n" +
"}\n" +
"public class Test {\n" +
" public static void main(String[] args) throws Exception {\n" +
" CharStream input = new ANTLRFileStream(args[0]);\n" +
" $lexerName$ lex = new $lexerName$(input);\n" +
" CommonTokenStream tokens = new CommonTokenStream(lex);\n" +
" System.out.println(tokens);\n" +
" }\n" +
"}"
);
return outputFileST;
}
示例15: StringTemplatAddDuplicateSameValuesTest
public void StringTemplatAddDuplicateSameValuesTest()
{
target = new StringTemplate(String.Empty);
target.Add("test", "one");
try
{
target.Add("test", "one");
Assert.Fail("No exception thrown");
}
catch (ArgumentException exception)
{
Assert.AreEqual("Element has already been added.\r\nParameter name: element", exception.Message);
Assert.AreEqual("element", exception.ParamName);
}
catch
{
Assert.Fail("ArgumentException not thrown");
}
}