I want to create some HTML inside view function and then insert it to template, how do I do that? In this case, I want to calculate percentage from project completed/project goal. I’m not using LiveView here and this is a simplified code, also, I have to create HTML this way, I can’t avoid it.
view.ex
def calculate_progress(goal, completed) do
progress = completed / goal * 100 |> round()
# How to create HTML which can be added to template?
<div>#{progress}%</div>
end
Yes, this works for single tags as I wrote in the example but that was just a simplified code… Is there a way to create a whole HTML block like this?
<div class="div-class">
<div class="some-class">PROGRESS FUNCTION %</div>
<p="p-class">some text here</p>
<p="another-p-class">another text here</p>
</div>
This should** work (not tested), might have to put the inner tags in a list
content_tag :div, [class: "div-class"] do
content_tag(:div, [class: "some-class"] do
progress
end
content_tag(:p, [class: "p-class"] do
"some text here"
end
...
end