D
D
donlino2014-05-16 22:21:59
Erlang
donlino, 2014-05-16 22:21:59

How to make Erlydtl render a list of entries into a template?

For example:

-record(usr,{name,email}).
...
Usr1 = #usr{name="John", email="[email protected]"},
Usr2 = #usr{name="Jane", email="[email protected]"},
Usr3 = #usr{name="Alex", email="[email protected]"},
{ok, Result} = template_dtl:render([{users, [Usr1, Usr2, Usr3]}]),
...

I want to use the template like this:
{% block content %}
{% for user in users %}
<a href="mailto:{{user.email}}">Send mail to {{user.name}}</a>
{% endfor %}
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2014-05-17
@donlino

Form the context and send it to render

Source = "My name is {{ name }}.",
{ok, Tpl} = dtl_template:new(Source),
Ctx = dtl_context:new([
    {name, "Lawrence"}
]),
{ok, <<"My name is Lawrence">>} = dtl_template:render(Tpl, Ctx).

https://github.com/oinksoft/dtl#4-basic-usage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question