How can I separate header and footer in twig in Node.Js - node.js

I am trying to dynamic header and footer page but I cannot success. I tried many ways but the header is not loading and I cannot find any sample for Node.Js. No errors occurs but only body content is loaded. All files are at the same directory.
Here the codes are I am tried to.
layout.twig
<!doctype html>
<html lang="en-US">
..
<body>
<header>
<!-- HEADER -->
{% block header %}{% endblock %} //Also tried here in index.
</header>
{% block body %}{% endblock %}
</body>
</html>
index.twig
{% extends 'layout.twig' %}
{% block body %}
<body>
<header>
<!-- HEADER -->
{% block header %}{% endblock %} //Also tried here in layout.
</header>
...
{% endblock %}
header.twig
{% extends 'index.twig' %} //Also tried with {% extends 'layout.twig' %}
{% block header %}
<div class="header js-header js-dropdown">
<div class="container">
...
{% endblock %}
footer.twig is same with header.
EDIT:
Delete extend tags which include on layout.twig and it's solved.

Related

Include the same block twice - within noscript and script elements

I would like to have two different header sections load depending on if JS is enabled or not. However, when I try the following, Twig throws the following error The block 'header' has already been defined
HTML:
<script>
<header id="template-header" class="js">
{% block header %}
{% endblock %}
</header>
</script>
<noscript>
<header id="template-header" class="no-js">
{% block header %}
{% endblock %}
</header>
</noscript>
How can I include the Twig header block twice?

how can I use twig blocks in my twig templates?

I have a problem, I am using twig templates and am trying to use blocks to separate my code but I have the following error:
Twig_Error_Loader: Template "index.html.twig" is not defined in "requestchange/main.twig" at line 1
index.html.twig is my main template layout
I have added in: {% block content %}{% endblock %}
then in my main.twig , I am extending the index.html.twig file and then putting my content within another {% block content %}{% endblock %}
Can someone please help me here?
You should understand how Template Inheritance works in Twig.
Here is simple example:
default.twig as a default layout
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
index.twig as a home page
{% extends 'default.twig' %}
{% block title %}Home page{% endblock %}
{% block content %}
<h1>Hello world!</h1>
{% endblock %}
And be attentive with path that you point in the extends tag.

Twig change meta data

i need to get meta data of the page in twig, for replace them by my values, but i don't find how get them.
For now i have :
{% if spec.name matches '{meta}' %}
{% set getdatas = app.request.server.get('REQUEST_URI') %} // use uri for test, i need the same but with meta_title, meta_description...
{{ dump(uri|replace({'domaine': spec.name})) }}
{% endif %}
If you want to override anything in Twig, you need to create a block for it. As an example:
base.html.twig
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
{% block meta %}{% endblock %}
<title>{% block title %}{% endblock %}</title>
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
parent.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
# My extra styles...
{% endblock %}
{% block body %}
<div>
{% block content %}{% endblock %}
</div>
{% endblock %}
child.html.twig
{% extends 'parent.html.twig' %}
{% block title %}My page{% endblock %}
{% block meta %}
<meta key="value">
{% endblock %}
{% block content %}My content{% endblock %}
my parent template :
{% extends 'DesignBundle::Front/layout.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('bundles/extension/css/flag-icon.min.css') }}">
<link rel="stylesheet" href="{{ asset('bundles/extension/css/main.css') }}">
{% endblock %}
{% block title %}
{{ _("Caractéristiques des noms de domaine") ~ " ." ~ extension.nomExt|idna_decode }}</code>
{% if extension.countryName is not empty %}
{{ " - " ~ extension.countryName|utf8_fix }}
{% endif %}
{% endblock %}
{% block content %} // view in first post is call here
{% include 'ExtensionBundle:New_fiche:show_content.html.twig' %}
{% endblock %}

Extend Twig template without blocks

I have layout template
<html>
<body>
{% block content %}{% endblock %}
</body>
</html>
And many child templates like this
{% extends 'layout/default.twig' %}
{% block content %}
<p>content</p>
{% endblock %}
And it's very annoying that every single child template in Twig must include {% block content %}...{% endblock %} to be extended by parent block, otherwise there will be error: A template that extends another one cannot have a body.
Is there any solution to bind all child template output(that is not located in any block) in some variable, and then use it to paste in parent template? Like this:
Layout
<html>
<body>
{{ _context.childOutput }
</body>
</html>
Child
{% extends 'layout/default.twig' %}
<p>content</p>
It will make child templates code more compact and there will be no dependency from parent templates blocks name.
UPD Submitted new issue on Twig's GitHub https://github.com/twigphp/Twig/issues/2027
The 2 lines you have in each template allows you aswell to redefine many blocks in one template. I can't see how the solution you want can do that.
<html>
<head>
{% block meta %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
you can see include and embed but if you really have only one block in your templates twig is maybe not the solution you need
While seeing in this GitHub issue that you define variables in the controller, I had the following idea. I'll assume that the child template only contain static code since you didn't describe this point.
You can modify the function in your controller in order to fetch the content of the child template then pass it to the parent template directly:
function acmeAction()
{
// …
return $this->render(
'AcmeBundle:layout:default.html.twig',
array(
'title' => $title,
'description' => $description,
'content' => file_get_contents(
$this->container->get('kernel')->locateResource(
'#AcmeBundle/Resources/views/layout/child.html.twig'
)
)
)
);
}
And the parent template:
<head>
<title>{% block title %}{{ title }}{% endblock %}</title>
<meta name="description" content="{% block description %}{{ description }}{% endblock %}" />
</head>
<body>
{% block body %}{{ content }}{% endblock %}
</body>
This way you won't need to define the parent in the child template.
You can define some variables in the child and display them in the parent:
Layout
<html>
<body>
{{ myValue }
</body>
</html>
Child
{% set myValue %}
<p>content</p>
{% endset %}
{% include 'layout/default.twig' %}
This works because:
Included templates have access to the variables of the active context.
Source: http://twig.sensiolabs.org/doc/tags/include.html
And it's very annoying that every single child template in Twig must include {% block content %}...{% endblock %} to be extended by parent block
While it may sound annoying when you only have one variable, you will see the benefits of this approach when you will have to define also the title of the page, the JavaScript code, etc. In this case the use of multiple {% block … %} is really useful.
See this example:
Layout
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
{% block javascript %}{% endblock %}
</body>
</html>
Child
{% extends 'layout/default.twig' %}
{% block title %}
My title
{% endblock %}
{% block content %}
<p>content</p>
{% endblock %}
{% block javascript %}
<script>…</script>
{% endblock %}

Jinja2 extends not working?

This is my base.html:
<html>
<head>
{% block head %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
This is my meta.html:
{% extends 'templates/base.html' %}
{% block head %}
{% if page == 'index' %}
<meta name="mysite.com" content="{{page}}"></meta>
{% else %}
<meta name="mysite.com" content="other page"></meta>
{% endblock %}
This is my index.html:
{% extends 'templates/base.html' %}
{% block body %}
......
{% endblock %}
This is my view:
#view_config(route_name='index', renderer='templates/index.html', permission='view')
def index(request):
return dict(page="index")
Extending the body works, but the meta tags are not showing up at all. What am I doing wrong?
As per Jinja2 documentation on extends, "The extends tag can be used to extend a template from another one. You can have multiple of them in a file but only one of them may be executed at the time."
When you load index.html from your view, the templating engine assesses index.html. It sees {% extends 'templates/base.html' %} in index.html and uses the blocks within index.html to replace same-name blocks of the parent/extended template (base.html).
So index.html says to use base.html as the skeleton template, but to replace {% block body %} in the parent (base.html) with its own {% block body %}.
Nowhere in this instruction (neither in base.html, nor index.html, nor the view code) does meta.html get mentioned. The templating engine isn't analyzing all the templates in your directory structure to see how it can plug things in. It is only using your explicit directives.
To be explicit about using meta.html, you can use the include directive (documentation found here) in base.html. Your base.html would then look like this:
<html>
<head>
{% include 'templates/meta.html' %}
{% block head %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
This would pull in meta.html and replace the same-name blocks of the skeleton (base.html) with the ones that were retrieved from the included template (meta.html).
In addition, the extends statement within meta.html needs to be removed, so it doesn't create a circular template inclusion.

Resources