# Templating Use {term}`templating` when a {term}`project` or a {term}`project support file` must contain values that change between solves. A template contains JCM text and optional Python blocks. `jcmsuite` renders the template before the solver or `JCMgeo` uses the generated file. For solve input shapes, see [Solving Projects](../core/solve/index.md). For geometry templates, see [Geo](../core/geo.md). ## Choose A Template File Use the template extension that matches the generated JCM file: | Template | Generated file | Use | | -------- | -------------- | ---------------------------- | | `.jcmt` | `.jcm` | {term}`Project support file` | | `.jcmpt` | `.jcmp` | {term}`Project` entrypoint | For example, `layout.jcmt` renders to `layout.jcm`. A project template named `project.jcmpt` renders to `project.jcmp`. ## Substitute Project Keys Use a placeholder in a template. A placeholder starts with `%(` and ends with a format letter, such as `e`. The name inside the placeholder must match a project key passed with `keys=`: ```jcm Radius = %(radius)e ``` Pass the values to {func}`solve ` with `keys=`: ```python import jcmsuite result = jcmsuite.solve( "project.jcmp", keys={"radius": 0.33}, ) ``` The {func}`solve ` call renders the selected project support files before it runs the project. The source template remains unchanged and is available for the next solve. The renderer supports these replacement formats: | Value type | Placeholder | Format Code | Output format | | -------------- | ---------------------------- | ----------- | ------------------------------------------ | | Integer | `%(count)d` or `%(count)i` | `d` or `i` | Decimal integer | | Floating-point | `%(radius)e` or `%(radius)f` | `e` or `f` | Floating-point value | | Complex | `%(permittivity)g` | `g` | JCMsuite complex value `(real, imaginary)` | | String | `%(name)s` | `s` | String value | The `g` format code formats complex values. For a complex scalar, it renders the real and imaginary parts as a JCMsuite complex pair. For example, `1.967 + 4.443j` renders as `(1.967, 4.443)`. Numeric vectors and matrices can also contain complex values. Each complex element uses the same pair representation. Numeric placeholders can include an optional one- or two-digit precision modifier before the format code. The modifier controls the precision used by the `e`, `f`, and `g` formats: ```jcm Wavelength = %(wavelength)6e Radius = %(radius)3f Permittivity = %(permittivity)8g ``` Without a modifier, the renderer uses its default numeric precision. The modifier is a precision value, not a project key name or a field width. ## Use Arrays And Matrices Numeric project keys can be Python lists, tuples, or NumPy arrays. A list is a sequence in square brackets, such as `[0.0, 1.0, 0.0]`. A one-dimensional value renders as a JCM vector. A two-dimensional value renders as a JCM matrix. For an integer placeholder, every array element must be an integer. ```jcm GlobalPosition = %(position)e RelPermittivity = %(permittivity)e ``` ```python keys = { "position": [0.0, 1.0, 0.0], "permittivity": [[1.0, 0.0], [0.0, 1.0]], } ``` ## Use Nested Project Keys Project keys can also contain nested values. A dictionary groups named values in braces, such as `{"material": {"permittivity": 2.3104}}`. Use a colon-separated path in the placeholder to select a nested project key. Each path component selects one named value, from left to right. Every component before the final component must select another dictionary: ```jcm RelPermittivity = %(material:permittivity)e ``` ```python keys = { "material": { "permittivity": 2.3104, }, } ``` Use the complete path in the placeholder for a value nested more than one level. The following placeholder selects `keys["material"]["substrate"]["permittivity"]`: ```jcm RelPermittivity = %(material:substrate:permittivity)e ``` ```python keys = { "material": { "substrate": { "permittivity": 2.3104, }, }, } ``` Inside a Python block, use square brackets and the value name instead of a colon path: ```jcm ``` If a path component is missing, or if an intermediate value is not a dictionary, the renderer raises {class}`TemplateMappingError `. Use complete {term}`project keys` for every value that changes the rendered input. This keeps a {term}`resultbag entry ` associated with the inputs that produced its result. For a runnable example, see [Keyword Substitution](../getting_started/keyword_substitution.md). ## Render During A Solve {func}`solve ` accepts a `.jcmp` project or a `.jcmpt` project template. For a `.jcmp` project, {func}`solve ` renders the selected `.jcmt` project support files when you pass {term}`project keys` or a {term}`JCMT pattern`. For example, `solve("project.jcmp", keys={"radius": 0.33})` renders `layout.jcmt` as `layout.jcm` before the solver uses the project. For a `.jcmpt` project, {func}`solve ` renders the project template as `.jcmp`. It also renders companion templates. Use `jcmt_pattern=` to select suffixed project support files. For example, `jcmt_pattern="fine"` selects `layout.fine.jcmt` or `layout.fine.jcm` before the unsuffixed `layout.jcmt` or `layout.jcm`. Use `temporary=True` when the rendered files are temporary solve inputs. Use `working_dir=` when you must keep the rendered files in a selected directory. ## Render A Template Directly Use {func}`jcmt2jcm ` when you need a rendered file without running a solve: ```python from jcmsuite.templates import jcmt2jcm rendered_layout = jcmt2jcm( "layout.jcmt", keys={"radius": 0.33}, outputfile="rendered_layout.jcm", ) ``` The {func}`jcmt2jcm ` function returns the absolute path of the rendered file. Pass `keys=None` or omit `keys` when the template does not use external values. Pass `outputfile=` to select the generated file path. If you omit `outputfile=`, {func}`jcmt2jcm ` removes the final `t` from `.jcmt` or `.jcmpt`. The function accepts only `.jcmt` and `.jcmpt` input files. It raises {class}`InvalidConfigurationError ` when the input file does not exist or has an unsupported extension. It raises {class}`TemplateMappingError ` when a required value is missing from project keys. ## Use Python Blocks Use `` or `` blocks for calculations and repeated JCM text. The template receives project keys through `keys`. A Python block can add project keys for later substitutions. `numpy` is available in each Python block. Text outside a Python block remains JCM input text: ```jcm K = [%(wave_number)e 0 0] ``` Use Python blocks to calculate values such as refractive-index-dependent permittivities, wave vectors, and polarization vectors. Keep direct project key substitutions for values that do not require a calculation. ## Repeat JCM Text With Loops Use a Python `for` loop when the template must generate repeated JCM blocks. Close the Python block before the JCM text that the loop repeats. Open another Python block at the same indentation level to continue or close the loop. ```jcm Circle { Name = "Rod %(rod_index)d" } ``` ```{warning} Close every Python loop in a later `` block. Indent the first `?>` tag at the same level as the statements in the active loop body. In the example, the first `?>` has four spaces before it. The final `?>` closes the `for` loop. ``` Nested loops can generate two-dimensional arrays. Project keys can control the number of loop iterations, so one template can generate multiple geometry sizes. ## Render Geometry Templates Use {func}`geo ` with the same project keys to inspect a rendered `layout.jcmt` before you run an electromagnetic solve: ```python import jcmsuite jcmsuite.geo( project_dir="examples/mie2d", keys={"radius": 0.5}, show=True, ) ``` The call renders the selected layout template, creates `grid.jcm`, and requests JCMview to open the grid. The call does not run the electromagnetic solve. JCMview requires a graphical environment. ## Next Steps - Use [Solving Projects](../core/solve/index.md) to choose the results that {func}`solve ` returns. - Use [Geo](../core/geo.md) to render a geometry template before mesh generation. - Use [Keyword Substitution](../getting_started/keyword_substitution.md) for a complete template project example. - Use [Python Code Snippets](../getting_started/code_snippets.md) for template calculations and [Loops](../getting_started/loops.md) for generated geometry.