ciclos y condicionales
otra de las ventajas la momento de trabaja con motores de plantillas es el uso de ciclos y condicionales
erb
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>
pug
//- condicionales
- var friends = 10
case friends
when 0
p you have no friends
when 1
p you have a friend
default
p you have #{friends} friends
- var user = {description: 'foo bar baz'}
- var authorised = false
#user
if user.description
h2.green Description
p.description= user.description
else if authorised
h2.blue Description
p.description.
User has no description,
why not add one...
else
h2.red Description
p.description User has no description
- var friends = 1
case friends
when 0: p you have no friends
when 1: p you have a friend
default: p you have #{friends} friends
//- ciclos
- for (var x = 0; x < 3; x++)
li item
- var list = ["Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis"]
each item in list
li= item
//- condicionales
- var friends = 10
case friends
when 0
p you have no friends
when 1
p you have a friend
default
p you have #{friends} friends
- var user = {description: 'foo bar baz'}
- var authorised = false
#user
if user.description
h2.green Description
p.description= user.description
else if authorised
h2.blue Description
p.description.
User has no description,
why not add one...
else
h2.red Description
p.description User has no description
- var friends = 1
case friends
when 0: p you have no friends
when 1: p you have a friend
default: p you have #{friends} friends
//- ciclos
- for (var x = 0; x < 3; x++)
li item
- var list = ["Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis"]
each item in list
li= item
template
{{! conditionals }}
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
{{! loops }}
<ul>
{{#cities}}
<li>{{name}}</li>
{{/cities}}
</ul>
{{! conditionals }}
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
{{! loops }}
<ul>
{{#cities}}
<li>{{name}}</li>
{{/cities}}
</ul>