Bản mẫu:switch

Từ điển mở Wiktionary


Lưu ý: Xin từ nay đừng sử dụng tiêu bản này, hãy sử dụng các hàm ParserFunctions.

Câu thường hỏi[sửa]

Tiêu bản này dùng để làm gì?[sửa]

Bạn có thể sử dụng tiêu bản này để bắt chước cấu trúc switch...case...default của nhiều ngôn ngữ lập trình phổ biến.

Tôi sử dụng nó như thế nào?[sửa]

{{switch
 | GIÁ-TRỊ-ĐỂ-THỬ
 | case: foo=hello
 | case: bar=world
 | default=Không ''foo'' mà cũng không ''bar''
}}

Với GIÁ-TRỊ-ĐỂ-THỬ là tham số hoặc biến số.

Ràng buộc[sửa]

  • The variable to switch have some constraints because the value must be able to be used as a parameter name. Characters known not to work are:
    • "
    • (
    • [
    • ]
    • )
    • =
  • There is no fall-through (if you look at the code, you know why).
  • If default is not defined, the result if no value matches will be blank.

Nó hoạt động ra sao?[sửa]

It works by the principle that a parameter declaration can be defined by the value of another parameter. When we access the parameter, we don't have the name hard coded, but we relies of the value of another parameter (in this case the parameter 1). In it's simpliest form it just like {{booleq/eq2}} ({{{{{{2|}}}|}}}), but to make it more user friendly, and remove the risk trying to access parameter 1. this by add the string case: to the parameters to check for. Also there is an aditional parameter default that can be used if you want to have an action if no other parameter were choosen.

Ghi công[sửa]

Nguyễn Xuân Minh lấy mẹo hay này từ Tiêu bản:Switch ở Wikipedia tiếng Việt. Nó bắt nguồn từ Template:Switch ở Wikipedia tiếng Anh.

Xem thêm[sửa]