Reputation: 82
Dear fellow developers,
Here's a sample code
{% set title = "Mr." %}
{% set pname = "John" %}
{% set middle_name = "Hayward" %}
{% set lname = "Citizen" %}
{% set display_name = "{{title}} {{pname}} {{middle_name}} {{lname}}" %}
<h4 class='prepared-for'><small>{{display_name}}</small></h4>
I have something like this. Please see, this display name is coming from the backend but just for the sake of simplicity, I'm setting it as a jinja variable for now.
Now having this structure, I'm getting {{title}} {{pname}} {{middle_name}} {{lname}}
but I want this to replace the values being set in the variables.
Any ideas about how I should approach this? Thanks!
Upvotes: 0
Views: 2853
Reputation: 311645
Inside of a Jinja templating context you can just refer to variables by name. You don't need to nest the {{..}}
markers. Since you're building a string, you will need to use some style of variable substitution. For example:
{% set display_name = "{} {} {} {}".format(title, pname, middle_name, lname) %}
You could get the same result by using the string concatenation operator:
{% set display_name = title ~ " " ~ pname ~ " " ~ middle_name ~ " " ~ lname %}
Or using printf
style formatting:
{% set display_name = "%s %s %s %s" % (title, pname, middle_name, lname) %}
Upvotes: 2