I'm trying to embed a Phaser game into a Django application, but struggling with the most basic of operations to get started: Making sure the canvas is placed inside a particular div as to center it on the middle of the page.
From what I could gather, for this to work, I should simply specify the parent div. However, whenever I specify the parent div, the canvas is nowhere to be found. When I leave out the line again, it reappears, but outside the layout.
{% extends "app/layout.html" %}
{% block content %}
<h2>{{ title }}.</h2>
<h3>{{ message }}</h3>
<div id='phaser-canvas'></div>
{% endblock %}
<script>
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-canvas',
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload() {
}
function create() {
}
function update() {
}
</script>
What am I not understanding?
Your html file extends your app/layouts.html. That layouts.html has a content block in which the content of your content block gets inserted. But as all the rest of your code is not within that block, it does not get inserted to layouts.html.
Create more blocks in your layouts.html like one for styles, one for javascript etc. Then put your code also in these blocks. Because only what's inside those blocks gets transferred to the template which is to extend.
layout.html:
{% block styles %}
{% endblock %}
{% block content %}
{% endblock %}
{% block javascript %}
{% endblock %}
your template:
{% extends "app/layout.html" %}
{% block content %}
<h2>{{ title }}.</h2>
<h3>{{ message }}</h3>
<div id='phaser-canvas'></div>
{% endblock %}
{% block javascript %}
<script>
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-canvas',
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload() {
}
function create() {
}
function update() {
}
</script>
{% endblock %}
Related
I am new to jekyll and have beginner experience in js. [Jekyll version: 4.2.0]
I have left navigation pane, center content, and right navigation pane.
on left nav I want to show the title of each collection, and when user clicks on it, right nav should show title of each page of that collection.
I'm able to show each collection's name in left nav with following code:
<div class="left_sidebar">
{% for coll in site.collections %}
{% if coll.label != "posts" %}
<a href="{{ coll.docs[0] | relative_url }}" class="col_link" >{{ coll.title }}</a>
{% endif %}
{% endfor %}
</div>
<div class="main">{{ content }} </div>
<div class="right_sidebar"></div>
but I'm not able to find a way to populate right navigation. Page should look something like this:
site_mockup
I tried scripting a bit but no luck so far:
<script>
$(function() {
$( '.col_link' ).on( 'click', function() {
$('.right_sidebar').load('{% include data.html %} #items');
});
});
</script>
_includes/data.html:
{% assign curr_coll = page.collection %}
{% for page in curr_coll.docs %}
{{ page.title }}
{% endfor %}
I'm trying to understand the language of Nunjucks templates.
To generate HTML I use Gulp + plugin gulp-nunjucks-render.
Faced a problem: I can not understand how to implement the generation of meta tags title and description.
Project file structure:
project
|
| -> dist > index.html (compiled)
| page1.html (compiled)
| page2.html (compiled)
|
|
| -> src
|-> pages -> index.njk (home page)
| page1.njk (page 1)
| page2.njk (page 2)
|
|-> templates
| |-> layout.njk
| |
| |-> parts -> header.njk
| footer.njk
|
|-> styles -> style.css
| style.min.css
|
|-> data.json
layout.njk
<!-- layout.njk -->
<html lang="en">
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body class="page">
{% block header %}{% endblock %}
{% block main %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
Which I connect to each page as follows:
{% extends "layout.njk" %}
index.njk
{% extends "layout.njk" %}
{% block header %} {% include "parts/header.njk" %} {% endblock %}
{% block main %}
<main class="main">
<!-- content -->
</main>
{% endblock %}
{% block footer %} {% include "parts/footer.njk" %} {% endblock %}
page1.njk
{% extends "layout.njk" %}
{% block header %} {% include "parts/header.njk" %} {% endblock %}
{% block main %}
<main class="main">
<!-- content page1 -->
</main>
{% endblock %}
{% block footer %} {% include "parts/footer.njk" %} {% endblock %}
page2.njk
{% extends "layout.njk" %}
{% block header %} {% include "parts/header.njk" %} {% endblock %}
{% block main %}
<main class="main">
<!-- content page1 -->
</main>
{% endblock %}
{% block footer %} {% include "parts/footer.njk" %} {% endblock %}
I couldn't find any examples of title output in the documentation, so the most an interesting way to I saw here.
For example, get the title and description from a file .JSON
data.json
"pages": [
{
title: "Hompage"
description: "This is the home page"
},
{
title: "Page1"
description: "This is page 1"
}
{
title: "Page2"
description: "This is page 2"
}
]
gulpfile.js
const gulp = require('gulp');
const nunjucksRender = require('gulp-nunjucks-render');
const data = require('gulp-data');
gulp.task('nunjucks', function() {
return gulp.src('src/pages/**/*.njk')
.pipe(data(function() {
return require('./src/data.json')
}))
.pipe(nunjucksRender({
path: ['src/templates']
}))
.pipe(gulp.dest('docs'))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('watch', function(cb) {
gulp.parallel(
'nunjucks',
)(cb);
gulp.watch('src/**/*.njk', gulp.series('nunjucks'));
});
gulp.task('default', gulp.series('watch'));
I don't know how to extract data from json. Please advise a solution.
If you are hoping to pass the data from data.json file
Step 1: you need to use someway to specify the page name in the data file itself.
Eg: data.json
{
"pages": {
"home": {
"title": "Hompage",
"description": "This is the home page"
},
"page1": {
"title": "Page1",
"description": "This is page 1"
},
"page2": {
"title": "Page2",
"description": "This is page 2"
}
}
}
Step 2: You have to set the page name as a variable in the nunjucks page.
Eg: index.njk
{% set pageName = 'home' %}
{% extends "layout.njk" %}
{% block header %} {% include "parts/header.njk" %} {% endblock %}
{% block main %}
<main class="main">
<!-- content -->
</main>
{% endblock %}
{% block footer %} {% include "parts/footer.njk" %} {% endblock %}
Step 3: Use the pageName variable in any nunjucks page, layout or partial to get the specific data for a specific page.
Eg: layout.njk
<html lang="en">
<head>
<title>{{ pages[pageName].title }}</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body class="page">
{% block header %}{% endblock %}
{% block main %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
I didn't compile and test this code. So make sure all variables and code syntax are correct if you are directly copying this code. The logic must work based on the documentation
Since you extends layout.njk in your pages, you could set the title and the description variables at the top of the document like so:
{% set title = myTitle %}
{% set description = myDescriptionHere %}
Then you do that for every pages (index.njk, page1.njk, page2.njk, etc.)
Alternatively, you could define your variables in nunjucksRender in the gulpfile.js as so:
.pipe(nunjucksRender({
path: ["yourPath"],
title: "yourTitle",
description: "yourDescriptionHere"
}))
// Rest of the code...
I didn't test it, I just deduce that from de documentation and great articles:
Killer features of Nunjucks
and
Building a static website with components using Nunjucks.
When you use extends, you should redefine block to override it
var nunjucks = require('nunjucks');
var env = nunjucks.configure();
var html = `
{% extends "layout.njk" %}
{% block header %}
{{pages[1].title}}
{% endblock %}
{% block main %}
{{pages[1].description}}
{% endblock %}
`
var data = {
pages: [
{
title: "Hompage",
description: "This is the home page"
},
{
title: "Page1",
description: "This is page 1"
},
{
title: "Page2",
description: "This is page 2"
}]
}
res = nunjucks.renderString(html, data);
console.log(res);
I am working on a project that uses twig.
Each page uses
{% extends "_layouts/_master" %}
Inside the _layouts/master there is a body tag
<body class="{% block bodyClass %}{% endblock %}">
Can I add a class to the body tag from a page that is using the include?
You can override parent block (defined in _layouts/_master) in child template (the one that extends parent). In your child template add this:
{% extends "_layouts/_master" %}
{% block bodyClass %}css-body-class another-css-body-class{% endblock %}
You can also include content of parent block and append something to it:
{% extends "_layouts/_master" %}
{% block bodyClass %}{{parent()}} css-body-class another-css-body-class{% endblock %}
You can read more in twig documentation for extends.
I'm trying to internationalize a flask webpage and I did every variable but couldn't succeed at internationalizing the title.
My code is below:
{% extends "base.html" %}{% block title %}Test Title{% endblock %}{% block content %}
I already tried this:
{% extends "base.html" %}{% block title %}_(Test Title){% endblock %}{% block content %}
and this:
{% extends "base.html" %}{% block title %}_("Test Title"){% endblock %}{% block content %}
both of them didn't work. Can you help me?
The third one proposition is the closest, because "Test Title" is a string so it needs quotes, but you must put it into double braces {{. When your string is surrounded by _( ) it becomes a variable and the template needs the double braces to display it.
The result is : {{ _("Test Title") }}
I'm trying to specify a variable in a child template and access it from another template which is included in the parent template.
Here's what I'm trying to do (code trimmed to the bare minimum):
child.html
{% set var = 'foo' %}
{% extends 'base.html' %}
base.html
{% include 'bar.html' %}
bar.html
{{ var }}
At this point, I render the child.html file and nothing is output.
This is what I've tried:
If I specify the variable in the base.html file instead of the child.html file, it works.
If I pass the variable in my render call, it works. template.render(var = 'foo')
Chaging the base.html file to be {% include 'bar.html' with context %} doesn't fix it.
I am able to access the variable in my base.html file just fine, and in fact, I've managed to create a workaround by adding {% set foo = var %} to my base.html, and changing bar.html to {{ foo }}
So concisely put, my question is: Is there a way for a template that's included in a parent template to access a variable set in a child template? (Without having to define a new variable in the parent file like my workaround)
I was able to get this by passing the variable within the child block on the parent template.
Ex. I have active_page declared on my child template pricing.html.
At first, I was not able to access active_page within my included file navbar-alt.html but when I set the variables inside of the navbar block it worked.
{% block wrapper %}
<div id="wrapper">
{% block navbar %}
{% set navigation_bar = [
('/what-we-do', 'what-we-do', 'what we do'),
('/pricing', 'pricing', 'pricing'),
('/demo', 'demo', 'demo'),
('http://indico.readme.io/v1.0/docs', 'docs', 'docs')
] -%}
{% set active_page = active_page|default('index') -%}
{% if "dashboard" not in request.path %}
{% include 'includes/navbar-alt.html' %}
{% else %}
{% include 'includes/navbar.html' %}
{% endif %}
{% endblock navbar %}
{% if "dashboard" in request.path %}
{% block sidebar %}
{% include 'includes/sidebar.html' %}
{% endblock sidebar %}
{% endif %}
{% block content_wrapper %}
<div id="content">
{% block content %}{% endblock content %}
</div>
{% endblock content_wrapper %}
{% include 'includes/flashes.html' %}
</div>
{% endblock wrapper %}
Original inspiration:
Jinja Tricks