Tiêu bản:If
Bách khoa toàn thư mở Wikipedia
Lưu ý: Xin từ nay đừng sử dụng tiêu bản này, hãy sử dụng {{qif}} hay các hàm ParserFunctions.
[sửa] Câu thường hỏi
[sửa] Tiêu bản này dùng để làm gì?
Bạn có thể sử dụng tiêu bản này để bắt chước cấu trúc if...else
của những ngôn ngữ lập trình thật.
[sửa] Tôi sử dụng nó như thế nào?
{{if |GIÁ-TRỊ-ĐỂ-THỬ |văn bản để hiển thị nếu kết quả không trống (có thể bao gồm GIÁ-TRỊ-ĐỂ-THỬ) |văn bản để hiển thị nếu nó trống }}
Cách sử dụng thứ hai – cũng tránh vấn đề dấu bằng; xem Meta):
{{if |test=GIÁ-TRỊ-ĐỂ-THỬ |then=văn bản để hiển thị nếu kết quả không trống (có thể bao gồm GIÁ-TRỊ-ĐỂ-THỬ) |else=văn bản để hiển thị nếu nó trống }}
[sửa] Nó hoạt động ra sao?
if
works by calling {{show1}}, passing to to show1
a parameter name as well as a parameter value based on the values of parameters passed to if. This depends on the specific technical feature that "(..) multiple assignments of values to the same parameter in the same template call are allowed and result in the last value being used." (see "A parameter name depending on a parameter" in m:Help).
if
takes three parameters (ignoring alternate usage for simplicity):
-
- the "test" value
{{{1}}}
[required] - the "do-if-test-not-empty" value
{{{2}}}
[required] - and the "do-if-test-is-empty" value
{{{3}}}
[optional]
- the "test" value
if
contains a call to show1
with two parameter assignments. This is implemented as (ignoring alternate usage):
{{show1|1={{{2}}}|1{{{1}}}={{{3}}}}}
show1
simply echoes the value of its parameter 1
. The call's first parameter assignment 1={{{2}}}
sets unnamed parameter 1
of show1
to the "do-if-test-not-empty" value.
Then there is a second parameter assignment: 1{{{1}}}={{{3}}}
. The name of the parameter in this assignment is "1" plus the test value. If the test value is empty, the name of the parameter is just "1", and this assignment overrides the first assignment, setting parameter "1" of show1
to the "do-if-test-is-empty" value.
For example, {{if|a|b|c}}
is expanded as {{show1|1=b|1a=c}}
which shows parameter 1 which is b, the second 1a=c
statement being ignored, since 1a is not the name of parameter 1.
{{if|b|c}}
is expanded as {{show1|1=b|1=c}}
which shows parameter 1 which is c. The first assignment 1=b
sets parameter 1 of show1
to b, but the second assignment overrides this by setting parameter 1 to c.
[sửa] Ghi công
Tổng quản lý Nguyễn Xuân Minh lấy mẹo hay này từ Template:If ở Wikipedia tiếng Anh.
[sửa] Xem thêm
- {{qif}}
- {{if defined}}
- {{switch}}