Foreach xml translation logic
Closed this issue · 0 comments
elohanlon commented
For rendering an array of $projects
info in field exports, we need to add a new foreach
translation rule.
// Once we call foreach on the $projects variable, each iteration runs in the context of a single project's json, which looks like this:
{
"display_label" => "Great Project",
"uri" : "http://www.example.com/project/123"
}
// The translation logic could look like this:
{
'foreach' : {
"field" : "$projects",
"xml_translation" : [
{
"element": "mods:relatedItem",
"attrs": {
"type": "host",
"displayLabel": "Project"
},
"content": [
{
"element": "mods:titleInfo",
"content": [
{
"element": "mods:title",
"content": "{{display_label}}"
}
]
},
{
"element": "mods:location",
"content": [
{
"element": "mods:url",
"content": "{{uri}}"
}
]
}
]
}
]
}
}
To make this code more generic, it could also apply to normal dynamic fields. This would allow us to do render nested fields like name_role:
// Imagine we're in the context of a name element
{
'foreach' : {
"field" : "name_role",
"xml_translation" : [
{
"element": "mods:role",
"content": [
{
"element": "mods:roleTerm",
"attrs": {
"type": "text",
"valueURI": "{{name_role_term.uri}}",
"authority": "{{name_role_term.authority}}"
},
"content": "{{name_role_term.value}}"
}
]
}
]
}
}