Function Repository Resource:

TemplateDefaults

Source Notebook

Retrieve the default values from a template

Contributed by: Bob Sandheinrich

ResourceFunction["TemplateDefaults"][template]

extracts the default values for the TemplateSlot slots in template.

ResourceFunction["TemplateDefaults"][template,form]

gives values as specified by form.

Details and Options

Supported values for form include:
Automaticautomatically choose a format
fuse merging function f for each slot
ResourceFunction["TemplateDefaults"][template] is equivalent to ResourceFunction["TemplateDefaults"][template,Automatic]. Automatic attempts to format the values in a suitable way to be provided to TemplateApply. If template has named slots, it attempts to create an Association. If template has indexed slots, it gives a list of values.
Whenever multiple distinct defaults exist for the same slot, the Automatic format will give a list of rules including all default values.
Slots without defaults will give Inherited as the default value.

Examples

Basic Examples (2) 

Retrieve default values for a template:

In[1]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot[1, DefaultValue -> "First"], TemplateSlot[2, DefaultValue -> "Last"]}]]
Out[1]=

Use a template with named slots:

In[2]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot["Name", DefaultValue -> "Bob"], TemplateSlot["Age", DefaultValue -> 41]}]]
Out[2]=

Scope (3) 

Get the defaults for a prompt from the Prompt Repository:

In[3]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][LLMPrompt["Translated"]]
Out[3]=

Use a function prompt:

In[4]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][LLMResourceFunction["PoemVersion"]]
Out[4]=

A string template has no defaults:

In[5]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][StringTemplate["hello ``, my name is ``"]]
Out[5]=

Properties and Relations (2) 

Repeated slots in the template are repeated in the output:

In[6]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot["Name", DefaultValue -> "Mark"], TemplateSlot["Name", DefaultValue -> "Margie"]}]]
Out[6]=

Specify a custom merging function:

In[7]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot["Name", DefaultValue -> "Mark"], TemplateSlot["Name", DefaultValue -> "Margie"]}], StringJoin]
Out[7]=

The order of the indexed values is determined by the slot numbers:

In[8]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot[2, DefaultValue -> "Two"], TemplateSlot[1, DefaultValue -> "One"]}]]
Out[8]=

Positions with no slots in the template give Inherited:

In[9]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 TemplateObject[{TemplateSlot[1, DefaultValue -> "One"], TemplateSlot[3, DefaultValue -> "Three"]}]]
Out[9]=

When all defaults for a slot are deferred by RuleDelayed, the merging function does not evaluate:

In[10]:=
ResourceFunction[
 "TemplateDefaults", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][LLMResourceFunction["PoemVersion"], Identity]
Out[10]=

Publisher

Bob

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 12 August 2024

Related Resources

License Information