Share via


TextTransformation.PushIndent Method

Adds text to CurrentIndent, which is prefixed to each line of the generated text output.

Namespace:  Microsoft.VisualStudio.TextTemplating
Assembly:  Microsoft.VisualStudio.TextTemplating.11.0 (in Microsoft.VisualStudio.TextTemplating.11.0.dll)

Syntax

'Declaration
Public Sub PushIndent ( _
    indent As String _
)
public void PushIndent(
    string indent
)
public:
void PushIndent(
    String^ indent
)
member PushIndent : 
        indent:string -> unit
public function PushIndent(
    indent : String
)

Parameters

  • indent
    Type: String

    The text to add to CurrentIndent. If CurrentIndent already contains text, indent is appended to the existing text.

Remarks

The CurrentIndent represents text that is prefixed to each line of the generated text output. The indent text can be spaces only, for example "    ", or it can include words. PushIndent adds text to CurrentIndent, and can be called more then once. PopIndent removes the most recently added text from CurrentIndent, and can be called more than once. ClearIndent removes all text from the CurrentIndent.

Examples

The following code examples demonstrate how to call the PushIndent method from a text template. Paste these code examples into any text template file and run the text template transformation to see the results.

This example calls the PushIndent method and adds four spaces as the indent. Notice that the indentation of the WriteLine statements in the code does not affect the indentation of the output.

<#
PushIndent("    ");

WriteLine("Hello");
    WriteLine("How are you?");
        WriteLine("Goodbye");

ClearIndent();
#>
<#
PushIndent("    ")

WriteLine("Hello")
    WriteLine("How are you?")
        WriteLine("Goodbye")

ClearIndent()
#>

This example produces the following output:

    Hello

    How are you?

    Goodbye

The following example calls the PushIndent method multiple times. The first time it adds four spaces as the indent, the second time it adds an additional four spaces as the indent.

<#
PushIndent("    ");
WriteLine("Hello");
WriteLine("How are you?");

PushIndent("    ");
WriteLine("I am fine, thank you. How are you?");

PopIndent();
WriteLine("I am fine too, thank you.");
WriteLine("Goodbye");

PushIndent("    ");
WriteLine("Goodbye");

ClearIndent();
#>
<#
PushIndent("    ")
WriteLine("Hello")
WriteLine("How are you?")

PushIndent("    ")
WriteLine("I am fine, thank you. How are you?")

PopIndent()
WriteLine("I am fine too, thank you.")
WriteLine("Goodbye")

PushIndent("    ")
WriteLine("Goodbye")

ClearIndent()
#>

This example produces the following output:

    Hello

    How are you?

        I am fine, thank you. How are you?

    I am fine too, thank you.

    Goodbye

        Goodbye

The following example calls the PushIndent method and includes words in the indent text.

<#
WriteLine("The first five numbers:");
PushIndent("  Number: ");

for(int i=1; i<6; i++)
{
    WriteLine(i.ToString());
}
ClearIndent();
#>
<#
WriteLine("The first five numbers:")
PushIndent("  Number: ")

For i as integer = 1 To 5

    WriteLine(i.ToString())
Next

ClearIndent()
#>

This example produces the following output:

The first five numbers:

  Number: 1

  Number: 2

  Number: 3

  Number: 4

  Number: 5

.NET Framework Security

See Also

Reference

TextTransformation Class

Microsoft.VisualStudio.TextTemplating Namespace

CurrentIndent

PopIndent

ClearIndent

Other Resources

Code Generation and T4 Text Templates