Is there any way to write this code in a more didactic way? [closed]

-5

Basically, the code will loop through the database for posts, but it will only post if the scheduled date is less than or equal to the current date. It searches through the bank for all posts and lists, but the ones that are scheduled, it will not show. And when there are no scheduled posts or there is nothing in the bank, it says it has no posts.

There are two elses. Hence the question: Is there just one?

I have tried in many ways and I can not. An else is for when there is nothing in the bank, the other for when there are no scheduled posts. One else does not recognize the other. I would like an else only.

<div class="container">
    <% var valor = "No posts!"; %>

    <% if(noticias.length > 0) { %>
        <% for(var i = 0; i < noticias.length; i++) { %>
            <% if(noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm')) { %>
            <div class="row">
                <div class="col-md-12">
                    <div class="noticia_wrapper">
                        <%= noticias[i].publicacao_agendada %>
                        <span class="noticia_autor"><%=noticias[i].autor%></span>
                        <a href="noticia?id_noticia=<%= noticias[i].id_noticia %>" class="noticia_titulo"><%=noticias[i].titulo%></a>
                        <span class="noticia_data"><%= moment(noticias[i].data_noticia).format(data) %></span>
                        <br />
                        <p class="noticia_resumo">
                            <%=noticias[i].noticia%>
                        </p>
                    </div>
                </div>
            </div>
            <% } else { %>
                <h1><%= valor %></h1>
        <% } %>
    <% } %>
    <% } else { %>
                <h1><%= valor %></h1>
        <% } %>
</div>

To facilitate understanding, I, @LeoCaracciolo, put something simpler

if(noticias.length > 0) {
    for(var i = 0; i < noticias.length; i++) {
        if(noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm')) { 

        } else {

        }
    }
} else {

}
    
asked by anonymous 04.04.2018 / 15:12

1 answer

0

I think this improves a little:

<div class="container">
<% 
var valor = "No posts!";

if (noticias.length <= 0) 
{
%>
    <h1><%= valor %></h1>
<%
} else {
    for(var i = 0; i < noticias.length; i++) {
        if (noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm'))
        {
        %>
            <div class="row">
                <div class="col-md-12">
                    <div class="noticia_wrapper">
                        <%= noticias[i].publicacao_agendada %>
                        <span class="noticia_autor"><%=noticias[i].autor%></span>
                        <a href="noticia?id_noticia=<%= noticias[i].id_noticia %>" class="noticia_titulo"><%=noticias[i].titulo%></a>
                        <span class="noticia_data"><%= moment(noticias[i].data_noticia).format(data) %></span>
                        <br />
                        <p class="noticia_resumo"><%=noticias[i].noticia%></p>
                    </div>
                </div>
            </div>
        <%
        }
        else
        {
        %>
            <h1><%= valor %></h1>
        <%   
        }
    }
} 
%>

What I did was basically reverse the first if, throwing the biggest code to the end and making it more readable, and I used the keys at the same level, which makes it easier to use the tags (though I do not think it's the correct PHP).

This code appears to be from a html rendering template in PHP, where you usually can not get away from the tag mess too much. Now if this is not a code just to generate html, I advise you to uncouple vision control and separate this structure into methods, which makes reading a lot easier.

See:

04.04.2018 / 16:18