Create a template and use it depending conditions with PHP

时间:2019-04-17 01:39:10

标签: php

I have the following multidimensional array in PHP:

Array (
    [0] => Array (
            [type] => Fruits
            [name] => Cherry
            ...
        )
    [1] => Array (
            [type] => Vegetables
            [name] => Artichoke
            ...
        )

    [2] => Array (
            [type] => Vegetables
            [name] => Asparagus
            ...
        )
)

I need to generate a HTML depending of the type value.

So my code should be like this:

foreach ($items as $item) {
    if($item['type'] == 'Fruits') {
        // around 10 conditions to built the HTML
        // echo the HTML
    }
    else {
        // around 10 conditions to built the HTML
        // echo the HTML
    }
}

The two templates are 95% the same if it's a fruit or a vegetable.


My question: what's the best way to dot repeat the template core code? As depending if it's a fruit or vegetable I have more then 10 conditions, actually I need to repeat these conditions a second time for the other type.

Thanks.

0 个答案:

没有答案