I'm trying to create an express app using Pug for templating. I cannot figure out why when I am using an unbuffered variable interpolation, the object is undefined but when I remove the dash it seems to work just fine (minus that it literally displays the object)
//index.pug
extends layout
block campground
- var campgrounds = '#{camps}'
.row
each campground in campgrounds
div.col-md-3.col-sm-6
div
img(src=`${campground.image}` class='img-thumbnail')
a(href=`/campgrounds/${campground._id}`)
h4.caption.text-center #{campground.name}
//app.js
app.get('/campgrounds', async (req, res) => {
try {
const camps = await Camp.find();
res.render('index', { camps: camps });
} catch (ex) {
res.status(500).send('internal error');
}
});
Syntax 1:
- var campgrounds = '#{camps}'
Produces:
Syntax 2:
var campgrounds = '#{camps}'
Produces:
I believe that you can:
Remove the row - var campgrounds = '#{camps}' and replace the row each campground in campgrounds on each campground in camps (see the code snippet below).
Or you also can replace - var campgrounds = '#{camps}' on - var campgrounds = camps (you don't need interpolation in code rows).
$(function() {
// in your code you get `camps` from DB
// const camps = await Camp.find();
const camps = [{name: 'Camp1'}, {name: 'Camp2'}, {name: 'Camp3'}];
var json = {
camps: camps
};
// jade compile
// instead of your function res.render(...);
$("#so").html(jade.compile($("#jadehi").html())(json));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jade/0.27.7/jade.min.js"></script>
<div id="so">SEO(please enable javascript~)</div>
<pre id="jadehi" style="display:none">
.content
.row
each campground in camps
div.col-md-3.col-sm-6
div.
Campground: #{campground.name}
<!-- img(src=`${campground.image}` class='img-thumbnail') -->
<!-- a(href=`/campgrounds/${campground._id}`) -->
<!-- h4.caption.text-center #{campground.name} -->
</pre>
Related
I found a codepen.io example (https://codepen.io/srees/pen/pgVLbm) I want to play around with in the context of a .vue app I'm working on, and I need some help transferring the <script> section over.
I copied the HTML chunk into a <template> and the CSS into a <style>. I've confirmed the .vue file works within the broader context (content loads when the <script> is commented out. I also placed <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" /> immediately before my <script> to resolve the $ not defined error I was getting. Is there something I need to import into App.vue or into this particular .vue file? When I leave <script> uncommented, I simply get a blank page loaded.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" />
<script>
var hidWidth;
var scrollBarWidths = 40;
...
you could define a method like this:
methods: {
renderStuff: function () {
var hidWidth;
var scrollBarWidths = 40;
var widthOfList = function(){
var itemsWidth = 0;
$('.list li').each(function(){
var itemWidth = $(this).outerWidth();
itemsWidth+=itemWidth;
});
return itemsWidth;
};
var widthOfHidden = function(){
return (($('.wrapper').outerWidth())-widthOfList()-getLeftPosi())-
scrollBarWidths;
};
var getLeftPosi = function(){
return $('.list').position().left;
};
var reAdjust = function(){
if (($('.wrapper').outerWidth()) < widthOfList()) {
$('.scroller-right').show();
}
else {
$('.scroller-right').hide();
}
if (getLeftPosi()<0) {
$('.scroller-left').show();
}
else {
$('.item').animate({left:"-="+getLeftPosi()+"px"},'slow');
$('.scroller-left').hide();
}
}
reAdjust();
$(window).on('resize',function(e){
reAdjust();
});
$('.scroller-right').click(function() {
$('.scroller-left').fadeIn('slow');
$('.scroller-right').fadeOut('slow');
$('.list').animate({left:"+="+widthOfHidden()+"px"},'slow',function(){
});
});
$('.scroller-left').click(function() {
$('.scroller-right').fadeIn('slow');
$('.scroller-left').fadeOut('slow');
$('.list').animate({left:"-="+getLeftPosi()+"px"},'slow',function(){
});
});
}
}
and run the method on mount like this:
mounted() {
this.renderStuff();
}
Side note, var is not ideal in this day and age. Recommend converting these to let.
Refer to this JSFiddle demo.
http://jsfiddle.net/MalcollmS/kLt3pfrc/2/
Why isn't my window.onload running?
How do I run the code editor.initialise?
Malcolm
var editor = (function () {
var editorData = {"Weeks":[{"Days":[{"Date":"\/Date(1417611600000)\/","DayIndex":1,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1417698000000)\/","DayIndex":2,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1417784400000)\/","DayIndex":3,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1417870800000)\/","DayIndex":4,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1417957200000)\/","DayIndex":5,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1418043600000)\/","DayIndex":6,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0},{"Date":"\/Date(1418130000000)\/","DayIndex":7,"StartHour":0,"StartMin":0,"FinishHour":0,"FinishMin":0,"LunchHour":0}]}],"NumWeeks":1,"WeekEnding":"\/Date(1418130000000)\/","StartDate":"\/Date(1417611600000)\/"}
initialise = function (data) {
//editorData = data;
$("#starthourselector div").live("click", function () { updateEditor("#starthourselector div", this); });
};
function updateEditor(selector, div) {
alert(selector);
$(selector).css('background-color','red');
$(div).css('background-color','white');
//var idx = $(selector).index(div));
}
return {
initialise: initialise
};
}());
window.onload = function() {
alert('hi');
editor.initialise(null);
}
In the jsfiddle option click no wrap head option and it should work properly
select no wrap head on the left hand side under on the same dropbox as onLoad
JSFIDDLE
i have been working for hours on the live search concept, and i am having problems with just one part of the Code.
html
<input id="searchs" autocomplete="off" />
<div class="livesearch" ></div>
javascript
$(function () {
$("#searchs").keyup(function () {
var searchs = $(this).val();
$.get("livesearch.php?searchs=" + searchs, function (data) {
if (searchs) {
$(".livesearch").html(data);
} else {
$(".livesearch").html("");
}
});
});
$(".page").live("click", function () {
var searchs = $("#searchs").val();
var page = $(this).attr("id");
$(".livesearch").load("livesearch.php?searchs=" + searchs + "&page=" +page);
});
});
the part var page = $(this).attr("id"); is not working. The page shows the error below
Notice: Undefined index: page in C:\xamp\...
and this error comes from the livesearch.php file which intends to use the index.
I am new to this way of scripting.
what could be the problem?
the part where the error is coming from on livesearch.php
if($_GET["page"]){
$pagenum = $_GET["page"];
} else {
$pagenum = 1;
}
Try this:
$(".livesearch").load("livesearch.php", {
searchs: searchs,
page: page
});
You weren't properly encoding the search string, and it could cause problems parsing the URL. jQuery will do that for you if you put the parameters in an object.
I'm a beginner in dojo, and I'm trying to print the output to console using dojo code. But I don't what's the problem in the following code, and how can I print the output to the console?
<html>
<head>
<script type = "text/javascript" src = "dojo/dojo.js" data-dojo-config = "async: true, isDebug : true" >
</script>
</head>
<body>
<h1 id = "greeting">Hello</h1>
<script>
define(["dojo/dom"],function(dom) {
var Twitter = declare(null, {username:"defaultusername",
say :function(msg)
{
console.log("Hello "+msg);
}
});
var myInstance = new Twitter();
myInstance.say("Dojo");
});
</script>
</body>
</html>
Use require instead of define:
<script>
require(["dojo/dom", "dojo/_base/declare"], function(dom, declare) {
var Twitter = declare(null, {
username: "defaultusername",
say :function(msg) {
console.log("Hello "+msg);
}
});
var myInstance = new Twitter();
myInstance.say("Dojo");
});
</script>
Console works, but your code inside callback function in declare is not being executed until you require it.
You cannot define in inline script code, that is meant to be a class define, put in the topmost line of a class-file, meaning define maps the filename to the returned value of its function.
This means, if you have
dojo_toolkit /
dojo/
dijit/
dojox/
libs/
myWidgets/
foo.js
And foo.js reads
define(["dijit._Widget"], function(adijit) {
return declare("libs.myWidgets.foo", [adijit], function() {
say: function(msg) { console.log(msg); }
});
});
Then a new module is registered, called libs / myWidgets / foo. You should make sure that the returned declare's declaredClass inside each define matches the file hierachy.
That being said, reason why define does not work for you is the explaination above. It is inline and has no src to guess the declaredClass name from. Rewrite your code to define("aTwitterLogger", [":
define("aTwitterLogger", ["dojo/_base/declare", "dojo/dom"],function(declare, dom) {
var Twitter = declare(null, {
username:"defaultusername",
say :function(msg)
{
console.log("Hello "+msg);
}
});
var myInstance = new Twitter();
myInstance.say("Dojo");
});
I can't figure out why this is not working. I'm trying to get different DIV tags to have different text content in a cycle loop.
<script type="text/javascript">
var text1 = ['foo', 'bar', 'baz'];
var i1 = 0,
var text2 = ['hug', 'kiss001', 'bank22'];
var i2 = 0,
$(document).ready(function()
{
setInterval(function (){
$("#DIV1").fadeOut(function ()
{
$("#DIV1").text(text[i1++ % text1.length]).fadeIn();
});
}, 1000);
setInterval(function (){
$("#DIV2").fadeOut(function ()
{
$("#DIV2").text(text[i2++ % text2.length]).fadeIn();
});
}, 1000);
});
</script>
<div id="DIV1"><div>
<div id="DIV2"><div>
Seems to be a bunch of syntax errors (text instead of text1/text2, no closing /div, etc. Works fine here: http://jsbin.com/obaqi4/3