Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
61 views
in Python by (119k points)
Explain Template Filter - wordwrap

Please log in or register to answer this question.

1 Answer

0 votes
by (119k points)

The wordwrap template filter is a useful tool in many programming languages and template engines for wrapping long lines of text or paragraphs to fit within a specified width. It ensures that the text does not exceed the desired line length by inserting line breaks at appropriate positions.

Here's an example of how the wordwrap filter can be used in different programming languages and template engines:

  1. Python with Jinja2 template engine:
from jinja2 import Template

template_string = """
{{ text|wordwrap(width=30) }}
"""

template = Template(template_string)
rendered_text = template.render(text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae turpis sit amet velit egestas convallis nec et turpis.")

print(rendered_text)
 

Output:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Sed
vitae turpis sit amet velit
egestas convallis nec et turpis.
 
  1. Django template engine (used in Django web framework):
{{ text|wordwrap:30 }}
 
  1. PHP with Twig template engine:
<?php
require_once 'vendor/autoload.php';

$loader = new \Twig\Loader\ArrayLoader([
    'template' => '{{ text|wordwrap(30) }}',
]);

$twig = new \Twig\Environment($loader);
$template = $twig->load('template');

echo $template->render(['text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae turpis sit amet velit egestas convallis nec et turpis.']);
?>
 
  1. Ruby with Liquid template engine:
require 'liquid'

template_string = <<~LIQUID
{{ text | wordwrap: 30 }}
LIQUID

template = Liquid::Template.parse(template_string)
rendered_text = template.render('text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae turpis sit amet velit egestas convallis nec et turpis.')

puts rendered_text
 

Output:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Sed
vitae turpis sit amet velit
egestas convallis nec et turpis.
 

In each of these examples, the wordwrap filter is applied to the text variable or value with a specified width parameter of 30. The filter automatically inserts line breaks at appropriate positions to ensure that each line of the text is no longer than 30 characters. This helps in maintaining readable and well-formatted text output, especially when dealing with long paragraphs or blocks of text.

Related questions

0 votes
1 answer
asked Jul 3, 2023 in Python by kvdevika (119k points)
0 votes
1 answer
asked Jul 3, 2023 in Python by kvdevika (119k points)
0 votes
1 answer
asked Jul 3, 2023 in Python by kvdevika (119k points)
0 votes
1 answer
asked Jul 3, 2023 in Python by kvdevika (119k points)
0 votes
1 answer
asked Jul 3, 2023 in Python by kvdevika (119k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...