Table of Contents

Interface ITemplateContext

Namespace
HoofMark.CSharpTemplating.Abstractions
Assembly
HoofMark.CSharpTemplating.Abstractions.dll

The context passed to a template's Run method. Provides access to configuration and the ability to write output files.

public interface ITemplateContext

Properties

Config

Configuration values loaded from the sibling .json file (e.g. MyTemplate.json). If no config file exists, this is an empty/no-op implementation — all TryGet calls return false and Get calls return null or default.

ITemplateConfig Config { get; }

Property Value

ITemplateConfig

Methods

ReadFile(string)

Reads a file and returns its content as a string.

string ReadFile(string path)

Parameters

path string

Path to the file to read. If relative, it is resolved against the following locations in order, using the first match found:

  1. The directory containing the template file
  2. The project root (from cstemplate.config.json, if configured)
  3. The output root directory

If absolute, the path is used directly.

Returns

string

The full text content of the file.

Exceptions

FileNotFoundException

Thrown if the file cannot be found at any of the resolved locations.

WriteFile(string, Action<IOutputWriter>)

Write a file by building its content with an IOutputWriter. Useful for code generation where indentation tracking is helpful.

void WriteFile(string relativePath, Action<IOutputWriter> write)

Parameters

relativePath string

Relative path of the file to write.

write Action<IOutputWriter>

An action that receives an IOutputWriter and writes content to it.

WriteFile(string, string)

Write a file with the given content to the output root. The path is relative to the configured output root directory. Intermediate directories are created automatically.

void WriteFile(string relativePath, string content)

Parameters

relativePath string

Relative path of the file to write, e.g. "Models/Order.cs" or "index.html". Forward slashes are normalised to the platform separator automatically.

content string

The full text content of the file.