smarber
smarber

Reputation: 5074

django template white spaces into strings

In order to remove white spaces around a variable, in twig engine it is possible to do the following:

 <a class="btn-
      {{- buttonsuccess -}}
 ">my button</a>

Notice the spaces and line break around the variable buttonsuccess.

This gives:

 <a class="btn-success">my button</a>

Notice that the spaces have been removed.

Is there something similar in the template of django?

Upvotes: 0

Views: 456

Answers (1)

Gocht
Gocht

Reputation: 10256

I don't know much about twig engine, but if {{- buttonsuccess -}} is something pre-defined, in the Django you could do this:

Set a status (or whatever) variable in the view:

def myview(request):
    ...
    context.update({'status': 'success'})
    ...

And use it in template:

<a class="btn-{{ status }}>my button</a>"

That will give the same:

<a class="btn-success">my button</a>

EDIT

Front-End frameworks such as Bootstrap provide classes based on status like success or warning. So you could find usefull this in those cases. See Bootstrap Contextual classes as a example.

From docs:

<!-- On rows -->
<tr class="active">...</tr>
<tr class="success">...</tr>
<tr class="warning">...</tr>
<tr class="danger">...</tr>
<tr class="info">...</tr>

Where you could use:

<tr class="{{ status }}">...</tr>

Upvotes: 2

Related Questions