How to run php scripts and php variables in the DEDE Template

Source: Internet
Author: User
When DEDE templates are used to run php scripts and php variables, the underlying fields of the dede database are often processed directly, if there is no corresponding function in dede, we often need to solve it. For example, I want to retrieve the typeid field of a record in the data table addonimages, and then output the result of multiplying typeid by 2 in the browser. (Note: The typeid & #20540 DEDE template shows how to run php scripts and use php variables.

When using the dede template, it is often necessary to directly process the underlying fields of the dede Database. if there is no corresponding function in the dede, we often need to work out a solution.

For example, I want to retrieve the typeid field of a record in the data table addonimages, and then output the result of multiplying typeid by 2 in the browser. (Note: The typeid value here is 6)

At first I wrote this:

{dede:loop table='dede_addonimages' if='aid=94'}[field:typeid runphp='yes']echo @me*2;[/field:typeid]{/dede:loop}

Browser output: 12 6

I think the reason is that [field: typeid] will first execute the internal php statement. when it runs to the line [/field: typeid, it will call internal functions and directly return the content of the [field: typeid] underlying template. if you want to output 12 directly, you can only at/include/extend. fuc. add custom functions to the php file.

function abc($val){    return $val*2;}
Then rewrite the template:

{dede:loop table='dede_addonimages' if='aid=94'}[field:typeid function="abc(@me)" /]{/dede:loop}
Output result: 12

In addition, the variables in the two php codes in the same template are not universal. that is to say, the scope of a variable in a php code is limited to the short code.

Example:

{dede:loop table='dede_addonimages' if='aid=94'}    [field:typeid runphp='yes']    echo $a=@me*2;    [/field:typeid]{/dede:loop}{dede:php}var_dump($a);{/dede:php}
Output result: 12 6 NULL

If you want to use the variables in the above php script in the subsequent php script, I come up with a temporary solution, that is, using global variables to solve this problem.

{dede:loop table='dede_addonimages' if='aid=94'}    [field:typeid runphp='yes']    $GLOBALS['a']=@me*2;    [/field:typeid]{/dede:loop}{dede:php}echo $GLOBALS['a'];{/dede:php}
Output result: 6 12 (because [field: typeid] does not contain echo, 6 is output directly)






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.