Skip to content

Fix String Concatenation #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,24 @@ In both cases, the underscore (`_`) in the string `"this_is_a_string"` is replac

### **Combine strings: Use concatenation**

Combining strings in Jinja is achieved similarly to Python. Strings can be concatenated using the `+` operator.
Combining strings in Jinja is achieved similarly to Python. Strings can be concatenated using either the `~` or `+` operators.

```django
{%- set string1 = "hello" -%}
{%- set string2 = "world" -%}
{{ string1~string2 }}
```

```django
{% raw %}
{%- set string1 = "hello" -%}
{%- set string2 = "world" -%}
{% endraw %}
{{ string1 + string2 }}
```

In this example, the strings `"hello"` and `"world"` are concatenated to form the output: `"helloworld"`.

Note that in Jinja, the `~` operator is the preferred operator for this function.

### **Conclusion**

Mastering string manipulation techniques in Jinja templating is essential for creating dynamic and well-formatted templates. By understanding how to split strings, replace characters, and concatenate strings, developers can process data effectively, leading to enhanced user experiences and streamlined workflows. Incorporate these techniques into your Jinja templates to optimize your data processing capabilities and improve the readability of your templates.