Hàm isset[] kiểm tra xem một biến có được đặt hay không, nghĩa là nó phải được khai báo và không phải là NULL
Hàm này trả về true nếu biến tồn tại và không phải là NULL, ngược lại trả về false
Ghi chú. Nếu nhiều biến được cung cấp, thì hàm này sẽ chỉ trả về true nếu tất cả các biến được đặt
Bắt đầu với sự lựa chọn rõ ràng nhất.
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
6 kiểm tra xem biến đã được xác định chưa và không có gì khác{# Check a variable is defined #}
{% if twigVariable is defined %}
The value could be:
0, false, null, [], {} or ''
{% else %}
Value is not defined
{% endif %}
{# Check a field is defined within an iterable #}
{% if twigVariable['fieldName'] is defined %}
...
'bộ lọc mặc định' với dự phòng trống
Bộ lọc mặc định thường được sử dụng để hiển thị giá trị dự phòng trong ngoặc nếu biến đính kèm không được xác định
Ví dụ.
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
7🔥 Điều mà tài liệu twig không đề cập là giá trị dự phòng của bộ lọc có thể để trống
- Đó là lý tưởng để kiểm tra xem có gì trống, sai hoặc không xác định không
- Nó đọc là
8 có nghĩa là bạn có thể đề phòng các lỗi không xác định như thế này{# Check a variable with an empty default filter #} {% if twigVariable|default %} Value is set and not empty {% else %} Value is either undefined, false, 0, null, "", [] or {} {% endif %} {# or check a field within an iterable #} {% if twigVariable['fieldName']|default %} ...
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
Đôi khi, việc sử dụng cú pháp bậc ba sẽ rõ ràng hơn và bộ lọc mặc định cũng hoạt động tốt ở đây
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
Bộ lọc
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
9 có các kiểm tra giống như {# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
6 kết hợp với {# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
1{# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
'bộ lọc mặc định' với dự phòng được chỉ định
Điều này là hoàn hảo để hiển thị một giá trị trên màn hình với dự phòng và với số lượng mã tối thiểu
{{ twigVariable|default['twigVariable is not defined'] }}
{# Checks value for undefined, false, null, "", [] or {}' #}
❗ Nhưng có một điểm khác biệt quan trọng so với bộ lọc mặc định trống
Nếu twigVariable được đặt là
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
2, bạn sẽ không nhận được dự phòng và thay vào đó, nó sẽ trả về một chuỗi trốngToán tử kết hợp Null
Hãy tìm hiểu thêm về nó 🤓 và tìm hiểu về toán tử hiếm khi được sử dụng nhưng mạnh mẽ này
Toán tử
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
3 tương tự như {# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
6 nhưng có một số bổ sung hữu íchBạn có thể nhanh chóng chỉ định các dự phòng nếu biến của bạn không được xác định. Nó trông như thế này
{# Check a variable with the null coalescing operator #}
{{ twigVariable ?? 'twigVariable not defined' }}
{# or check a field within an iterable #}
{{ twigVariable['fieldName'] ?? 'fieldName not defined' }}
Ví dụ trên giống với séc
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
6 dài hơn này{{ twigVariable is defined
? twigVariable
: 'twigVariable not defined' }}
Chúng tôi cũng có thể thêm dự phòng vô hạn với nhiều điều kiện
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
3{{ twigVariable ?? fallbackVariable ?? 'Variables not defined' }}
Bây giờ hãy so sánh độ dài của cú pháp ngắn đó với cú pháp tương đương bằng cách sử dụng
{# Check a variable with an empty default filter #}
{% if twigVariable|default %}
Value is set and not empty
{% else %}
Value is either undefined, false, 0, null, "", [] or {}
{% endif %}
{# or check a field within an iterable #}
{% if twigVariable['fieldName']|default %}
...
6 trong ternary{{ twigVariable is defined
? twigVariable
: [fallbackVariable is defined
? fallbackVariable
: 'twigVariable & fallbackVariable not defined'
]
}}
Chỉ để giải trí - dự phòng của bạn cũng có thể có bộ lọc
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
8. Chúng tôi không thể xâu chuỗi các dự phòng sau bộ lọc mặc định vì nó hoạt động như một bộ lọc 'bắt tất cả'{# Check a variable is defined #}
{% if twigVariable is defined %}
The value could be:
0, false, null, [], {} or ''
{% else %}
Value is not defined
{% endif %}
{# Check a field is defined within an iterable #}
{% if twigVariable['fieldName'] is defined %}
...
0Câu lệnh if với toán tử hợp nhất null
Chúng ta cũng có thể sử dụng
{# Check a variable with ternary default filter #}
{{ twigVariable|default
? 'Value is set and not empty'
: 'Value is undefined, false, 0, null, "", [] or {}'
}}
{# or check a field within an iterable #}
{{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
3 trong 'câu lệnh if' để chỉ định một lệnh nếu {# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
0 không xác địnhTrong ví dụ này, nếu
{# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
0 không được xác định, thử nghiệm sẽ chuyển sang dự phòng tiếp theo. Trong trường hợp này, nó đi vào {# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
2{# Check a variable is defined #}
{% if twigVariable is defined %}
The value could be:
0, false, null, [], {} or ''
{% else %}
Value is not defined
{% endif %}
{# Check a field is defined within an iterable #}
{% if twigVariable['fieldName'] is defined %}
...
1Thay thế toán tử hợp nhất null được cải tiến
nystudio107 đã phát hành tiện ích mở rộng Twig cho Craft 3 để vá lỗi kiểm tra có phần hạn chế của toán tử hợp nhất null
Toán tử kết hợp rỗng
3 tương tự như toán tử kết hợp rỗng{# |default checks for the same things as: #} {{ twigVariable is defined and twigVariable is not empty ? 'set' : 'unset' }}
3, nhưng cũng bỏ qua các chuỗi rỗng{# Check a variable with ternary default filter #} {{ twigVariable|default ? 'Value is set and not empty' : 'Value is undefined, false, 0, null, "", [] or {}' }} {# or check a field within an iterable #} {{ twigVariable['fieldName']|default ? 'set' : 'unset' }}
5 và cả mảng trống{# |default checks for the same things as: #} {{ twigVariable is defined and twigVariable is not empty ? 'set' : 'unset' }}
6{# |default checks for the same things as: #} {{ twigVariable is defined and twigVariable is not empty ? 'set' : 'unset' }}
Khi bạn cài đặt và kích hoạt plugin này, bạn sẽ có thể sử dụng
{# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
3 để kiểm tra chắc chắn một dự phòng phù hợp như thế này{# Check a variable is defined #}
{% if twigVariable is defined %}
The value could be:
0, false, null, [], {} or ''
{% else %}
Value is not defined
{% endif %}
{# Check a field is defined within an iterable #}
{% if twigVariable['fieldName'] is defined %}
...
2Hãy xem plugin Empty Coalesce trên Github
Lưu ý bên lề về các biến nghiêm ngặt trong Craft CMS
Trong Craft CMS 3, có một cài đặt trong
{# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
8 được gọi là {# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
9. Theo mặc định, khi {# |default checks for the same things as: #}
{{ twigVariable is defined
and twigVariable is not empty ? 'set' : 'unset' }}
9 được đặt thành đúng và lần lượt- Nếu
1 [mặc định]{{ twigVariable|default['twigVariable is not defined'] }} {# Checks value for undefined, false, null, "", [] or {}' #}
Twig sẽ không thông báo cho bạn về bất kỳ biến không xác định nào - Nếu
2{{ twigVariable|default['twigVariable is not defined'] }} {# Checks value for undefined, false, null, "", [] or {}' #}
Twig đưa ra lỗi thời gian chạy cho tất cả các biến không xác định -
3{{ twigVariable|default['twigVariable is not defined'] }} {# Checks value for undefined, false, null, "", [] or {}' #}
Một số thích tắt cài đặt này trong quá trình phát triển nhưng đó không phải là phương pháp hay nhất. Bạn nên biết về các lỗi không xác định càng sớm càng tốt. Bạn sẽ không muốn vấp phải một biến bị hỏng sau này chứ?