[Smarty]Change the variable of numbers and dates.

There are many situations where you have to format variables on the template side. I want to make image management in three digits, such as 001,002, or file name. Do you want the score display to show max's number of digits? I would like to introduce the variable modifiers that can be used on the […]

There are many situations where you have to format variables on the template side.
I want to make image management in three digits, such as 001,002, or file name.
Do you want the score display to show max's number of digits?
I would like to introduce the variable modifiers that can be used on the Smarty template side.

string_format — Changing variable numbers

Suppose the contents of a variable called $num were 2.4362.

{$num}
{$num|string_format: "%.2f"}
{$num|string_format:"%03d"}
{$num|string_format:"%02d"}
{$num|string_format:"%d"}

Actual view

2.4362
2.43
002
02
2

I think that the usage which puts 0 on the head is considerably used.The rest is when you make it an integer.
This is useful, for example, when you want to calculate a number and make it an integer.
The %.2f or the back is a type specifier.
Reference ⇒http://php.net/sprintf

Type specifier

% - percent character.No arguments are required.
b - Treats the argument as an integer and represents it as a two-decimal line.
c - Treats the argument as an integer and represents it as a character of its ASCII value.
d - Treats the argument as an integer and represents it as a decimal number.
e - Treat arguments as scientific notation (e.g. 1.2e+2). Precision specifiers represent the number of decimal places in PHP 5.2.1 and later. Earlier versions meant the number of digits (one smaller value) of the significant number.
Same as E - %e, but use uppercase letters (for example, 1.2E+2).
f - Treats the argument as a double and represents it as a floating-point number.
F - Treats the argument as float and represents it as a floating-point number (locale-independent). Available in PHP 4.3.10 and PHP 5.0.3 and later.
g - %e and %f abbreviations.
G - %E and %f abbreviations.
o - Treats the argument as an integer and represents it as an 8-decimal number.
s - Treats the argument as a string and represents it.
u - Treats the argument as an integer and represents it as an unsigned 10-decimal number.
x - Treats the argument as an integer and represents it as a 16-decimal (in lowercase).
X - Treats the argument as an integer and represents it as a 16-decimal (in uppercase).

date_format — Shaping dates

Shape the date or time.smarty.now is a reserved variable that represents the current time.

{$smarty.now|date_format:"%H:%M:%S"}
{$smarty.now|date_format:"%H time%M minutes"}

These are displayed as follows.

09:45:30
09:45

The display format is specified in the "%H:%M:%S" part.
Reference ⇒http://www.smarty.net/docsv2/ja/language.modifier.date.format.tpl

date_format conversion specifier

%a - The name of the shortened day of the week based on the current locale
%A - The name of the full day of the week based on the current locale
%b - the name of the shortened month based on the current locale
%B - The name of the full month based on the current locale
%c - Appropriate date and time representation based on the current locale
%C - century (divided by 100 years, rounded to integers.00-99)
%d - 10 decimal dates (01-31)
Same as %D - %m/%d/%y
%e - A monthly date in 10 decimal sIf the date is single digits, precede it with a space.('1' to '31')
%g - the last two digits of the Year[00,99]
%G - Year[0000,9999]
Same as %h - %b.
%H - time at 10 decimals in 24-hour display (00-23)
%I - time at 10 decimals of 12-hour display (ranging from 01 to 12)
%j - 10 decimal dates per year (001-366)
%k - Put a space in the first digit of the time displayed in 24 hours (0 to 23)
%l - 12-hour display time to put a space in the first digit (range 1 to 12)
%m - Express month in decimal numbers (01-12)
%M - express edits in decimals
%n - Newline character
%p - 'am' or 'pm' at a specified time, or a string corresponding to the current locale
Time expressed in %r - a.m. and p.m. notation
%R - 24-hour time
%S - seconds expressed in decimal s
%t - Tab character
%T - current time.%H: %M: equal to %S.
%u - The range in the day of the week in 10-deci[1,7]mal notation.One is Monday.
%U - what is the second week in a year or expressed in decimals.Count the first Sunday of the year as the first day of the first week.
%V - ISO 8601:1988 is in the range of 01 to 53 in the week number of the current year. 1 is the first week and the week has a minimum of four days in the current year.The week starts on Monday.
%w - Represents the day of the week as a decimal number.Sunday is zero.
%W - expressed in 10 decimals what is the second week in the current year. Count the first Monday of the year as the first day of the first week.
%x - Represents a date other than time based on the current locale.
%X - Represents the time excluding the date based on the current locale.
%y - Represents the year as a decimal number except the part of the century.(Range from 00 to 99)
%Y - Representing the year including the century in decimal s
%Z - time zone or its name or abbreviation
%% - character literal '%'

There are a lot of variable modifiers, so I'll introduce only useful things.
Next time, I'll introduce cat or something.

Smarty Recommended Books

Smarty had only old books, but the following are recommended for getting started.
Smarty PHP Template Programming and Applications


Leave a Reply 0

Your email address will not be published.

17 − eight =