The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Dotiac::DTL::Tag::block - The {% block NAME %} tag

SYNOPSIS

Template file: (main.html)

<html>
	<head>
		<title>{% block title %}Default title{% endblock title %}</title>
	</head>
	<body>
		<div class="main">{% block pagecontent %}
			This page has no content.
		{% endblock %}</div>
	</body>
</html>

Other template file: (aboutus.html)

{% extends "main.html" %}
{% block title %}About us{% endblock %}
{% block pagecontent %}<h1>About us</h1>Under construction{% endblock %}

Other template file: (aboutus2.html)

{% extends "main.html" %}
{% block pagecontent %}<h1>About us</h1>Under construction{% endblock %}

DESCRIPTION

The "block" tag defines a named block, which can be overwritten or overwrites it.

It is normaly used together with {% extends %}. It defines a block in one template and then overwrites the defined block from another template. This is called "template inheritance". There are some great examples on the original Djagno homepage: http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

Everything from {% block NAME %} till {% endblock [NAME] %} is treated as a block with the name NAME. In another template, which contains an {% extends "abovetemplate" %}, the block NAME can be overwritten.

The previous content of the block can be used in that block via the variable {{ block.super }}

If no new block with the same name is defined, the default text is used.

If no extend is used, the {% block %} tags will just return their content.

Of course all variables in a block will work just as they would outside, even if the block is defined in a different file alltogether.

The above examples will produce:

Rendering just "main.html", the block-tags will disappear:

<html>
	<head>
		<title>Default title</title>
	</head>
	<body>
		<div class="main">
			This page has no content.
		</div>
	</body>
</html>

Rendering "aboutus.html", all block-tags will be replaced:

<html>
	<head>
		<title>About us</title>
	</head>
	<body>
		<div class="main"><h1>About us</h1>Under construction</div>
	</body>
</html>

Rendering "aboutus2.html", one block-tags will be replaced, the other will be left as default:

<html>
	<head>
		<title>Default title</title>
	</head>
	<body>
		<div class="main"><h1>About us</h1>Under construction</div>
	</body>
</html>

SEE ALSO

http://www.djangoproject.com, Dotiac::DTL

BUGS AND DIFFERENCES TO DJANGO

If you find any, please report them.

LEGAL

Dotiac::DTL was built according to http://docs.djangoproject.com/en/dev/ref/templates/builtins/.

AUTHOR

Marc-Sebastian Lucksch

perl@marc-s.de