getting error in EJS file while running res.render - node.js

getting this error:
missing ) after argument list in C:\Users\yoniy\Desktop\YelpCamp\views\campgrounds\index.ejs while compiling ejs
and it's the index.ejs file :
<!-- <%- include "../partials/header" %> -->
<header class="jumbotron">
<div class="container">
<h1><span class="glyphicon glyphicon-tent"></span> Welcome To YelpCamp!</h1>
<p>View campgrounds from all over the world</p>
<p>
<a class="btn btn-primary btn-lg" href="/campgrounds/new">Add New Campground</a>
</p>
<p>
<form action="/campgrounds" method="GET" class="form-inline" id="campground-search">
<div class="form-group">
<input type="text" name="search" placeholder="Campground search..." class="form-control">
</div>
</form>
</p>
</div>
</header>
<div class="row text-center flex-wrap" id="campground-grid">
<%- campgrounds.forEach(function(campground){ %>
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="<%= campground.image %>">
<div class="caption">
<h4><%= campground.name %></h4>
</div>
<p>
More Info
</p>
</div>
</div>
<%- }); %>
</div>
<!-- <%- include "../partials/footer" %> -->
can't find the syntax error that I get.
thanks for help !

You need to use flow control tags <% for non output parts of your template. In this case the following lines need to be changed:
<%- campgrounds.forEach(function(campground){ %>
Should instead be:
<% campgrounds.forEach(function(campground){ %>
And this line:
<%- }); %>
Should be:
<% }); %>
Update for additional Error
Your formatting for including additional files is incorrect:
<!-- <%- include "../partials/header" %> -->
Should be:
<%- include("../partials/header.ejs") %>
Same for the footer:
<!-- <%- include "../partials/footer" %> -->
Should be:
<%- include("../partials/footer.ejs") %>
Though I cannot validate your paths are correct as I do not know your directory structure.

Related

Issue while using ejs syntax

This is my ejs file(_post.ejs):
<link rel="stylesheet" href="/css/home.css">
<div id="home-container">
<% if(locals.user){%>
<section id="feed-posts">
<h4>Posts</h4>
<form action="/posts/create" method="post" id="new-post-form">
<textarea name="content" cols="30" rows="3" placeholder="Type here..." required></textarea>
<input type="submit" value="Post">
</form>
</section>
<%}%>
<section id="posts-wall" >
<ul>
<% for(post of Posts){ %>
<%- include('_post') -%>
<hr>
<% } %>
</ul>
</section>
<section id="user-friends">
<h4>Friends</h4>
<% for(u of all_users){%>
<p>
<%= u.username %>
</p>
<%}%>
</section>
</div>
<script src="/js/home_posts.js"></script>
Error :
Error: /Users/nishantbhatia/Desktop/nodews/codial/views/home.ejs:17
15| <ul>
16| <% for(post of Posts){ %>
>> 17| <%- include('_post') -%>
18| <hr>
19| <% } %>
20| </ul>
I have included the closing tags as well. Really not sure abt the error. Any help would be appreciated.I tried to read the documentation still no clue.
The syntax should be ,remove - from closing tag. See the docs
<section id="posts-wall" >
<ul>
<% for(post of Posts){ %>
<%- include('_post') %>
<hr>
<% } %>
</ul>
</section>

problem while accessing to partial views in express

here's my views file: views/profile.ejs
<body>
<% include partial/nav.ejs %>
<!-- <%- include('partial/nav.ejs'); %> -->
<h1> Welcome to <%= person %> profile</h1>
<p>
<strong>The age: </strong><%= proObj.age%>
</p>
<p>
<strong>Relationship Status: </strong><%= proObj.relation_status%>
</p>
<div>
<p>
<strong>Hobbies: </strong>
<% hobbies.forEach(function(item){ %>
<li> <%= item %> </li>
<% }); %>
</p>
</div>
</body>
and this partial views file: views/partial/nav.ejs
<nav>
<ul>
<li>Home</li>
<li>Contact</li>
when i reload the website i got this error popped into the server
SyntaxError: Unexpected identifier in C:\Users\Fo0ola\Desktop\SERVER-SIDE\views\profile.ejs while compiling ejs

How can I use JSON image file object on ejs

I want to use an image on my ejs code, but I use loop with JSON array, so I want to make image url to object of JSON.
I put the JSON object way on the source code.
{
"champion" :
[
{
"id": "266",
"key": "Aatrox",
"name": "Aatrox",
"origin": ["Demon"],
"class": ["Blademaster"],
"cost": 3,
"img" : "https://ddragon.leagueoflegends.com/cdn/9.17.1/img/champion/Aatrox.png",
This json file's local data name is "cdata".
<!-- Champion Data -->
<% for(var i=0; i<cdata.champion.length; i++){ %>
<div class="col-xl-12 col-md-6 mb-12">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-3"><h3><%= cdata.champion[i].name %></h3></div>
<img src= <%= "cdata.champion[0].img" %> class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
<div class="h5 mb-0 font-weight-bold text-gray-800"><%= cdata.champion[i].key %></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<% } %>
My website shows broken image https://ykyuen.files.wordpress.com/2015/10/svg-image-broken.png
after that, I changed the code to
<img src= "<%= cdata.champion[0].img %>" class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
and
<img src= <%= cdata.champion[0].img %> class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
but it still doesn't work.
You can simplify your code from using for loop to use .forEach Here is docs
<% cdata.champion.forEach(function(champion){ %>
<div class="col-xl-12 col-md-6 mb-12">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-3"><h3><%= champion.name %></h3></div>
<img src="<%= champion.img %>" class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
<div class="h5 mb-0 font-weight-bold text-gray-800"><%= champion.key %></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<% }) %>
In my case, I've used <img src="<%= image %>" and it worked.
Of course, the code should work only in case of correctly parsed JSON.

Why is my JS logic appearing on index (using ejs/express)

im having an issue understanding why my JS is appearing on my layout. Im currently following along with a tutorial on sitepoint creating forms with in express and when i render the layout.ejs + index.ejs with contact as a partial, the logic on partial is appearing on the page as text.
here is the code:
<div class="form-header">
<% if (Object.keys(errors).length === 0) { %>
<h2>Send us a message aa</h2>
<% } else { %>
<h2 class="errors-heading">Oops, please correct the following:</h2>
<ul class="errors-list">
<% Object.values(errors).forEach(error => { %>
<li><%= error.msg %></li>
<% }) %>
</ul>
<% } %>
</div>
<form method="post" action="/contact" novalidate>
<div class="form-field <%= errors.message ? 'form-field-invalid' : ''
%>">
<label for="message">Message</label>
<textarea class="input" id="message" name="message" rows="4"
autofocus><%= data.message %></textarea>
<% if (errors.message) { %>
<div class="error"><%= errors.message.msg %></div>
<% } %>
</div>
<div class="form-field <%= errors.email ? 'form-field-invalid' : '' %>">
<label for="email">Email</label>
<input class="input" id="email" name="email" type="email" value="<%=
data.email %>" />
<% if (errors.email) { %>
<div class="error"><%= errors.email.msg %></div>
<% } %>
</div>
<div class="form-actions">
<button class="btn" type="submit">Send</button>
</div>
</form>

SilverStripe (3.6.2) Search returning assets folder contents

I haven't enabled Search on SilverStripe before, but it seems pretty easy. I've followed steps from 2 other projects (although they are 3.5 version projects but not sure that makes a difference or not) that have search enabled as well as the tutorial offered on SilverStripe's site, and for some reason, I'm getting asset folder items (i.e. images) in my search results. It only seems to happen if I click to search and nothing has been entered into the search field.
There should be no asset items returned at any time for search, and if there is no search query, then there should be a message saying nothing was entered or something. I noticed that using the default $SearchForm setup provided by the basic install gives me the desired results, but not for the form I'm using (which does work on 2 other SilverStripe sites--I checked and confirmed).
I'm not sure what I'm missing? I feel like everything is done correctly, and I would like to use the setup I have now to give me more styling ability:
From _config.php:
FulltextSearchable::enable();
From my Header.ss file:
<!-- SEARCH BAR -->
<form class="navbar-form navbar-left nav-right-left search-form" id="SearchForm_SearchForm" action="/home/SearchForm" method="get" enctype="application/x-www-form-urlencoded">
<fieldset style="font-size: 0;">
<div class="field text nolabel search-holder">
<input name="Search" placeholder="Search" class="form-control search-field text nolabel active search-box" />
</div>
<div class="ja-search-box">
<button class="icon search-button smiths-search-btn" type="submit"><i class="glyphicon glyphicon-search pull-right"></i></button>
</div>
</fieldset>
</form>
The Search Results page:
<div class="main" role="main">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="Content" class="searchResults">
<h1 class="brand-red">$Title</h1>
<% if $Query %>
<p class="searchQuery">You searched for "{$Query}"</p>
<% end_if %>
<% if $Results %>
<ul id="SearchResults">
<% loop $Results %>
<li>
<h4>
<a href="$Link">
<% if $MenuTitle %>
$MenuTitle
<% else %>
$Title
<% end_if %>
</a>
</h4>
<% if $Content %>
<p>$Content.LimitWordCountXML</p>
<% end_if %>
<a class="readMoreLink" href="$Link" title="Read more about "{$Title}"">Read more about "{$Title}"...</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>Sorry, your search query did not return any results.</p>
<% end_if %>
<% if $Results.MoreThanOnePage %>
<div id="PageNumbers">
<div class="pagination">
<% if $Results.NotFirstPage %>
<a class="prev" href="$Results.PrevLink" title="View the previous page">←</a>
<% end_if %>
<span>
<% loop $Results.Pages %>
<% if $CurrentBool %>
$PageNum
<% else %>
$PageNum
<% end_if %>
<% end_loop %>
</span>
<% if $Results.NotLastPage %>
<a class="next" href="$Results.NextLink" title="View the next page">→</a>
<% end_if %>
</div>
<p>Page $Results.CurrentPage of $Results.TotalPages</p>
</div>
<% end_if %>
</div>
</div>
</div>
</div>
</div>
By default, the full text search will search array('SiteTree', 'File')
http://api.silverstripe.org/en/3.1/class-FulltextSearchable.html
I would try changing your FulltextSearchable::enable(); line to FulltextSearchable::enable(array('SiteTree'));
I haven't tried this before and am not sure if it will work.

Resources