問題描述
Smarty ‑ {IF} {/IF} 內的條件太多 (Smarty ‑ Too many conditions inside {IF} {/IF})
I've got the following problem ‑ I need to use condition {IF} {/IF} with many variables, but the whole site just goes blank when I put the code:
{if $product.id_category_default == 6 || $product.id_category_default == 9 || $product.id_category_default == 10 || $product.id_category_default == 11 || $product.id_category_default == 12 || $product.id_category_default == 13 || $product.id_category_default == 14 || $product.id_category_default == 8 || $product.id_category_default == 60 || $product.id_category_default == 35 || $product.id_category_default == 36 || $product.id_category_default == 37 || $product.id_category_default == 38 || $product.id_category_default == 39 || $product.id_category_default == 40 || $product.id_category_default == 41 || $product.id_category_default == 93 || $product.id_category_default == 31 || $product.id_category_default == 32 || $product.id_category_default == 33 || $product.id_category_default == 34 || $product.id_category_default == 94 || $product.id_category_default == 53 || $product.id_category_default == 54 || $product.id_category_default == 55 || $product.id_category_default == 56 || $product.id_category_default == 57 || $product.id_category_default == 58 || $product.id_category_default == 59 || $product.id_category_default == 95 || $product.id_category_default == 19 || $product.id_category_default == 20 || $product.id_category_default == 24 || $product.id_category_default == 21 || $product.id_category_default == 22 || $product.id_category_default == 23 || $product.id_category_default == 27 || $product.id_category_default == 28 || $product.id_category_default == 29 || $product.id_category_default == 30 || $product.id_category_default == 96 || $product.id_category_default == 97 || $product.id_category_default == 98 || $product.id_category_default == 99 || $product.id_category_default == 100 || $product.id_category_default == 51 || $product.id_category_default == 50 || $product.id_category_default == 52 || $product.id_category_default == 49 || $product.id_category_default == 101 || $product.id_category_default == 102}gardena{/if}
{if $product.id_category_default == 7 || $product.id_category_default == 15 || $product.id_category_default == 16 || $product.id_category_default == 17 || $product.id_category_default == 18 || $product.id_category_default == 61 || $product.id_category_default == 62 || $product.id_category_default == 63 || $product.id_category_default == 64 || $product.id_category_default == 65 || $product.id_category_default == 66 || $product.id_category_default == 67 || $product.id_category_default == 68 || $product.id_category_default == 69 || $product.id_category_default == 70 || $product.id_category_default == 71 || $product.id_category_default == 72 || $product.id_category_default == 73 || $product.id_category_default == 74 || $product.id_category_default == 75 || $product.id_category_default == 76 || $product.id_category_default == 77 || $product.id_category_default == 78 || $product.id_category_default == 79 || $product.id_category_default == 80 || $product.id_category_default == 81 || $product.id_category_default == 82 || $product.id_category_default == 83 || $product.id_category_default == 84 || $product.id_category_default == 85 || $product.id_category_default == 86 || $product.id_category_default == 87 || $product.id_category_default == 88 || $product.id_category_default == 89 || $product.id_category_default == 90 || $product.id_category_default == 91 || $product.id_category_default == 92}beam{/if}
Can it be caused by too many variables? Or maybe there is a way to shorten this code?
參考解法
方法 1:
It is better IMHO to move the logic into the controller and build an displayproducts array there, and just use
{if $displayproducts.gardena}gardena{/if}
Smarty templates are meant to read by a web designer sometimes ..
方法 2:
Even if this wasn't causing an error, and regardless of whether it is in the controller or the view, a long list of "magic numbers" like this should immediately be a bad "code smell". Anyone reading the code (including you!) won't know where this list originated or what it actually means, when to add or remove items from it, where else in the code needs to be kept in sync, etc etc.
You need to ask yourself what this list of IDs actually means, and why these particular categories act that way ‑ they presumably have some property in common which should be given a descriptive name regardless of the effect it has on the front end.
If id_category_default
refers to some ID in a database table, add a column to that table (e.g. is_gardena
), and retrieve that along with the item data. You can then have a simple if statement in your Smarty code like {if $product.category_is_gardena} ... {/if}
.
If that is not possible in your application, you should at the very least capture this logic into a single, well‑named and commented function, containing an array of appropriate IDs and an explanation of what they refer to. You can then register that as a Smarty modifier and use something like {if $product.id_category_default|is_gardena_category} ... {/if}
.