How to run php script in DEDE template?
It is often necessary to directly process the underlying fields of the dede database. If there is no corresponding function in dede, then we have to use other methods to achieve it. As the title says, run the php script. and php variables, there is a good example below, interested friends can refer to
Recommended learning: 梦Weavercms
When using dede templates, often It will be necessary to directly process the underlying fields of the dede database. If there is no corresponding function in dede, we often need to find a way to deal with it.
Example: I want to take out the typeid field of a record in the data table addonimages, and then output the result of typeid multiplied by 2 in the browser. (Note: The typeid value here is 6)
At first I wrote this:
The code is as follows:
<body class="index"> {dede:loop table='dede_addonimages' if='aid=94'} [field:typeid runphp='yes'] echo @me*2; [/field:typeid] {/dede:loop} </body>
The browser output is: 12 6
There is an extra 6 here. I think the reason is that [field:typeid] will execute the internal php statement first. When it reaches the line [/field:typeid], it will call the internal function and return directly [ field:typeid] The content of the underlying template, if you want to directly output 12, you can only add a custom function in the /include/extend.fuc.php file.
The code is as follows:
function abc($val){ return $val*2; }
Then the template is rewritten as:
The code is as follows:
<body class="index"> {dede:loop table='dede_addonimages' if='aid=94'} [field:typeid function="abc(@me)" /] {/dede:loop} </body>
The output result is: 12
In addition, Note that the variables in two pieces of PHP code in the same template are not common, that is to say, the scope of a certain variable in a piece of PHP code is limited to the short code.
Example:
The code is as follows:
<body class="index"> {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} </body>
The output result is: 12 6 NULL
If you want to use the above php script in the subsequent php script variables, I came up with a temporary solution, which is to use global variables to solve this problem.
The code is as follows:
<body class="index"> {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} </body>
The output result is: 6 12 (because there is no echo in [field:typeid], so 6 is output directly)
The above is the detailed content of How to run php script in DEDE template. For more information, please follow other related articles on the PHP Chinese website!