I have a FAQ component in my Phoenix 1.7 application. It asks and answers multiple questions. The answers are rendered as HEEx templates. This all works.
My problem is the generation of JSON-LD for a structured schema. I can’t call the answer_wann_sind_ferien_in/1
function from within the JSON-LD schema. The <%= @location.name %>
part works.
Is there a way to use <.answer_wann_sind_ferien_in location={@location} />
within the JSON HEEx template?
Here’s the code:
attr :location, :string, required: true
defp answer_wann_sind_ferien_in(assigns) do
~H"""
<div>Example answer.</div>
"""
end
def json_ld_location_faq(assigns) do
~H"""
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Wann sind Ferien in <%= @location.name %>?",
"acceptedAnswer": {
"@type": "Answer",
"text": "<.answer_wann_sind_ferien_in
location={@location}
/>"
}
}
}
]
}
</script>
"""
end
Here’s the resulting HTML:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Wann sind Ferien in Baden-Württemberg?",
"acceptedAnswer": {
"@type": "Answer",
"text": "<.answer_wann_sind_ferien_in
location={@location}
/>"
}
}
}
]
}
</script>