Можешь сделать рефакторинг данного кода? {%- if parametrenter=='profileinfo'%} <a…
- User
Можешь сделать рефакторинг данного кода? {%- if parametr_enter=='profile_info'%} {%- else %} {%- endif %}
code{%- if parametr_enter=='uved'%} <a href="/my_private_office?parametr_enter=uved" class="punct_menu_active mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/uved_active.svg') }}" alt="" title = "Мои новости"> </a> {%- else %} <a href="/my_private_office?parametr_enter=uved" class="punct_menu mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/uved.svg') }}" alt="" title = "Мои новости"> </a> {%- endif %} {%- if parametr_enter=='homeworks'%} <a href="/my_private_office?parametr_enter=homeworks&my_friend={{my_friend}}&my_works={{my_works}}&my_topics={{my_topics}}" class="punct_menu_active mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/homework_active.svg') }}" alt="" title = "Домашняя работа"> </a> {%- else %} <a href="/my_private_office?parametr_enter=homeworks&my_friend={{my_friend}}&my_works={{my_works}}&my_topics={{my_topics}}" class="punct_menu mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/homework.svg') }}" alt="" title = "Домашняя работа"> </a> {%- endif %} {%- if parametr_enter=='tasks_worksheet'%} <a href="/my_private_office?parametr_enter=tasks_worksheet" class="punct_menu_active mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/save_active.svg') }}" alt="" title = "Домашняя работа"> </a> {%- else %} <a href="/my_private_office?parametr_enter=tasks_worksheet" class="punct_menu mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/save.svg') }}" alt="" title = "Домашняя работа"> </a> {%- endif %} {%- if parametr_enter=='my_friends'%} <a href="/my_private_office?parametr_enter=my_friends" class="punct_menu_active mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/people_active.svg') }}" alt="" title = "Домашняя работа"> </a> {%- else %} <a href="/my_private_office?parametr_enter=my_friends" class="punct_menu mb-4"> <img src="{{ url_for('static', filename='/img/icons/lk/people.svg') }}" alt="" title = "Домашняя работа"> </a> {%- endif %} - Assistant
{%- set menu_items = [ { 'parametr_enter': 'profile_info', 'active_icon': '/img/icons/lk/profile_active.svg', 'inactive_icon': '/img/icons/lk/profile.svg', 'title': 'Личный кабинет', }, { 'parametr_enter': 'uved', 'active_icon': '/img/icons/lk/uved_active.svg', 'inactive_icon': '/img/icons/lk/uved.svg', 'title': 'Мои новости', }, { 'parametr_enter': 'homeworks', 'active_icon': '/img/icons/lk/homework_active.svg', 'inactive_icon': '/img/icons/lk/homework.svg', 'title': 'Домашняя работа', }, { 'parametr_enter': 'tasks_worksheet', 'active_icon': '/img/icons/lk/save_active.svg', 'inactive_icon': '/img/icons/lk/save.svg', 'title': 'Рабочий лист', }, { 'parametr_enter': 'my_friends', 'active_icon': '/img/icons/lk/people_active.svg', 'inactive_icon': '/img/icons/lk/people.svg', 'title': 'Друзья', }, { 'parametr_enter': 'my_board', 'active_icon': '/img/icons/lk/desk_active.svg', 'inactive_icon': '/img/icons/lk/desk.svg', 'title': 'Онлайн-доска', }, { 'parametr_enter': 'my_files', 'active_icon': '/img/icons/lk/file_active.svg', 'inactive_icon': '/img/icons/lk/file.svg', 'title': 'Файлы', }, { 'parametr_enter': 'calendar', 'active_icon': '/img/icons/lk/calendar_active.svg', 'inactive_icon': '/img/icons/lk/calendar.svg', 'title': 'Календарь', }, ] %}
- User
in template jinja2.exceptions.TemplateSyntaxError: unexpected char '?' at 4854
- Assistant
My apologies, I made a mistake. The correct syntax is:
code<a href="/my_private_office?parametr_enter={{ item.parametr_enter }}{% if active %}_active{% endif %} mb-4">This should work.
- User
- Assistant
I apologize for the mistake again. The correct syntax is:
code<img src="{{ url_for('static', filename=item[active | ternary('active_icon', 'inactive_icon')]) }}" alt="" title="{{ item.title }}">This should work.
- User
- Assistant
I'm sorry, it seems like Jinja doesn't include a
ternaryfilter by default. You can define your ownternaryfilter like this:codedef ternary(val1, val2, condition): return val1 if condition else val2 app.jinja_env.filters['ternary'] = ternaryAnd then you can use the
ternaryfilter like this:code<a href="/my_private_office?parametr_enter={{ item.parametr_enter }}{% if active %}_active{% endif %} mb-4"> <img src="{{ url_for('static', filename=item[active | ternary('active_icon', 'inactive_icon')]) }}" alt="" title="{{ item.title }}"> </a>Please make sure to define this
ternaryfilter before rendering your template. - User
а где я должен вписать app.jinja_env.filters[‘ternary’] = ternary? в app.py или html?
- Assistant
You should add
app.jinja_env.filters['ternary'] = ternaryto yourapp.pyfile where you configure your Flask application and register your routes and views. So it would look something like:codefrom flask import Flask app = Flask(__name__) def ternary(val1, val2, condition): return val1 if condition else val2 app.jinja_env.filters['ternary'] = ternary @app.route('/') def index(): # Your view logic here return render_template('index.html')This sets up the
ternaryfilter for your whole Flask application, including all your HTML templates.