How to generate a random number in the smarty Template

Source: Internet
Author: User
Tags smarty template

What should I do if I want to generate a random number in the smarty template? Generate in the PHP file and assign values to the template. This is absolutely feasible, but it is too complicated. The following tips will be used to generate random numbers directly in the smarty template.

Now, if you need to generate a random number between and in the smarty template, you can write it as follows:

{math equation=rand(125,324)}

In this way, our goal is achieved. How can this problem be solved? Isn't it easy? In fact, math Function in the smarty template is mainly used here. Let's briefly explain the role and usage of the smarty math Function.

Math allows the template designer to perform mathematical expression operations in the template. any numeric type variable can be used in the expression, and the result is output at the position of the math tag. the variables used in the expression are passed to the function as parameters. They can be template variables or static values. currently, the following operators can be used: +,-,/, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans, and tan. for more information about mathematical functions, see the PHP documentation.

If the special attribute "assign" is specified, the output value of this function will be assigned to the template variable specified by assign, rather than directly output.

However, it should be noted that the math function is inefficient to execute because php's eval () function is used. it is more efficient to perform mathematical operations in PHP. Therefore, you should perform mathematical operations in PHP as much as possible and assign the results to template variables.

Smarty math Function demo

{* $ Height = 4, $ width = 5 *}

{Math equation = "x + y" x = $ height y = $ width}

Output result: 9

{* $ Row_height = 10, $ row_width = 20, # col_div # = 2, assigned in template *}

{Math equation = "height * width/division"
Height = $ row_height
Width = $ row_width
Division = # col_div #}

Output result: 100

{* You can use parenthesis http://www.phpernote.com/tag/smarty *}

{Math equation = "(x + y)/z)" x = 2 y = 10 z = 2}

Output result: 6

{* You can supply a format parameter in sprintf format *}

{Math equation = "x + y" x = 4.4444 y = 5.0000 format = "%. 2f "}

Output result: 9.44

Articles you may be interested in
  • Add the latest tag to the information in the smarty Template
  • Method for judging null array in the smarty Template
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Extension of the for loop in the smarty Template
  • The cycle table in the smarty template contains incomplete td
  • Smarty template retention variable Summary
  • How to Use constants defined by define in a program in a smarty Template
  • Smarty template variable operator Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.