Yii Formatting - Yii

How Yii formatting is done?

The data is displayed in a readable format, by uing the formatter application component.

Step1 − Add the actionFormatter method to the SiteController.

Formatter view is rendered.

Step 2 − A formatter.php view file is created inside the views/site folder.

Step 3 − Visit http://localhost:8080/index.php?r=site/formatter, and the output appears as:

View File

The following formats related to date and time are supported by formatter component.

Output format
Example
date
January 01, 2016
time
16:06
datetime
January 01, 2016 16:06
timestamp
1512609983
relativeTime
1 hour ago
duration
5 minutes

Step 4 − The formatter view is modified by the code:

Step 5 − Enter http://localhost:8080/index.php?r=site/formatter in the address bar of your web browser, the output appears as:

Formatter Output

What are the Date Formats supported by Yii?

There are four date format shortcuts: short, medium, long, and full.

Step 1 − Modify the formatter view file this way.

Step 2 – Inweb browser, enter http://localhost:8080/index.php?r=site/formatter, and the output appears as:

Date Formats Output

What are the Number Formats supported by Yii?

The formatter component supports the following formats related with numbers −
Output format
Example
integer
51
decimal
105.51
percent
51%
scientific
1.050000E+2
currency
$105
size
105 bytes
shortSize
105 B

Step 1 − Modify the formatter view by:

Step 2 – Visit http://localhost:8080/index.php?r=site/formatter, and the output appears as:

Number Formats Output

What are the other Formats supported by Yii?

The other formats supported by Yii are:

  • text − The value is HTML-encoded.
  • raw − The value is outputted as is.
  • paragraphs − The value is formatted as HTML text paragraphs wrapped into the p tag.
  • ntext − The value is formatted as an HTML plain text where newlines are converted into line breaks.
  • html − The value is purified using HtmlPurifier to avoid XSS attacks.
  • image − The value is formatted as an image tag.
  • boolean − The value is formatted as a boolean.
  • url − The value is formatted as a link.
  • email − The value is formatted as a mailto-link.

Formatting a value for a specific country is determined by the formatter by using currently active locale.

The below example shows how to format date for different locales.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics