C
C
connor742015-11-06 20:05:15
Yii
connor74, 2015-11-06 20:05:15

Where to store functions used in view in Yii?

Good evening!
There is a function that processes dates from the database to display the form:
"September 10-15" or October 30 - November 2"

public function getSeminarDate($date_begin, $date_end) {
        if(Yii::$app->formatter->asDate('$date_begin', 'M') == Yii::$app->formatter->asDate('$date_end', 'M')) {  
            return Yii::$app->formatter->asDate('$date_begin', 'dd').'-'.Yii::$app->formatter->asDate('$date_end', 'dd');
        }
        else {
            return Yii::$app->formatter->asDate('$date_begin', 'dd').' '.
            Yii::$app->formatter->asDate('$date_begin', 'MMMM').'-'.
            Yii::$app->formatter->asDate('$date_end', 'dd').' '.
            Yii::$app->formatter->asDate('$date_end', 'MMMM');
        }
    }

This function takes 2 fields from the database and outputs them to a string, of the form indicated above. Where is it better to store this function and how to call it in the view?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-11-06
@connor74

1) we use twig
2) we write or use ready-made filters and / or functions. Then instead of this nonsense in controllers you will have:

<div class="workshop-date">{{ workshopDate(workshop)  }}</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question