فیلترهای مربوط به متن (String Filters)


String Filters در جنگو برای دست‌کاری، فرمت‌دهی و تغییر محتوای متنی به کار می‌روند. این فیلترها به ما اجازه می‌دهند داده‌های متنی که به تمپلیت ارسال شده‌اند را بدون تغییر در سطح پایگاه داده یا لاجیک برنامه، به شکل دلخواه در خروجی نمایش دهیم. برای نمونه می‌توانیم تمام حروف را کوچک یا بزرگ کنیم (lower, upper)، اولین حرف جمله یا هر کلمه را بزرگ کنیم (capfirst, title)، بخش‌هایی از متن را حذف کنیم (cut)، یا متن طولانی را کوتاه نماییم (truncatewords, truncatechars) و با length یا length_is می‌توانیم طول متن‌ها یا رشته‌ها را بسنجیم. همچنین فیلترهایی مانند linebreaks و linenumbers به ما کمک می‌کنند متن‌های چندخطی را به صورت قالب‌بندی‌شده نمایش دهیم. به طور خلاصه، این دسته از فیلترها ابزار قدرتمندی برای نمایش حرفه‌ای و کاربرپسند داده‌های متنی در تمپلیت هستند.

{{ "hello world!"|upper }}            ┈┈┈⮞ "HELLO WORLD!"            <!-- Returns the text in upper case letters. -->
{{ "HELLO WORLD!"|lower }}            ┈┈┈⮞ "hello world!"            <!-- Returns the text in lower case letters. -->
{{ "hello world!"|title }}            ┈┈┈⮞ "Hello World!"            <!-- Upper cases the first character of each word in a text, all other characters are converted to lower case. -->
{{ "hello world!"|capfirst }}         ┈┈┈⮞ "Hello world!"            <!-- Returns the first letter in uppercase. -->
{{ "Hello World!"|slugify }}          ┈┈┈⮞ "hello-world"             <!-- Converts text into one long alphanumeric-lower-case word. -->
{{ "Hello World!"|make_list }}        ┈┈┈⮞ ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'] <!-- Converts a value into a list object. -->
{{ "Hello World!"|cut:"l" }}          ┈┈┈⮞ "Heo Word!"               <!-- Removes any specified character or phrases -->
{{ "Hello World!"|slice:":4" }}       ┈┈┈⮞ "Hell"                    <!-- Returns a specified slice of a text or object. -->
{{ "Hello World!"|truncatechars:4 }}  ┈┈┈⮞ "Hel..."                  <!-- Shortens a string into the specified number of characters. -->
{{ "Hello World!"|truncatewords:1 }}  ┈┈┈⮞ "Hello..."                <!-- Shortens a string into the specified number of words. -->
{{ "Hello World!"|wordcount }}        ┈┈┈⮞ 2                         <!-- Returns the number of words in a text. -->
{{ "Hello World!"|length }}           ┈┈┈⮞ 12                        <!-- Returns the number of items in an object, or the number of characters in a string. -->
{{ "Hello World!"|length_is:12 }}     ┈┈┈⮞ True                      <!-- Returns True if the length is the same as the specified number -->
{{ "Hello World!"|center:20 }}        ┈┈┈⮞ "    Hello World!    "    <!-- Centers the value in the middle of a specified width. -->
{{ "Hello World!"|ljust:20 }}         ┈┈┈⮞ "Hello World!        "    <!-- Left aligns the value according to a specified width -->
{{ "Hello World!"|rjust:20  }}        ┈┈┈⮞ "        Hello World!"    <!-- Right aligns the value according to a specified width -->
{{ "Hello \n World!"|linebreaks }}    ┈┈┈⮞ <p>Hello <br/> World!</p> <!-- Returns the text with <br> instead of line breaks, and <p> instead of more than one line break. -->
{{ "Hello \n World!"|linebreaksbr }}  ┈┈┈⮞ Hello <br/> World!        <!-- Returns the text with <br> instead of line breaks. -->
{{ "<b>Hello World!</b>"|striptags }} ┈┈┈⮞ Hello World!              <!-- Removes HTML tags from a text. -->
{{ "I'm Araz"|addslashes }}           ┈┈┈⮞ "I\'m Araz"               <!-- Adds a slash before any quote characters -->