Home > CMS Tutorial > DEDECMS > How to run php script in DEDE template

How to run php script in DEDE template

藏色散人
Release: 2019-12-26 09:25:54
Original
3363 people have browsed it

How to run php script in DEDE template

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=&#39;dede_addonimages&#39; if=&#39;aid=94&#39;} 
[field:typeid runphp=&#39;yes&#39;] 
echo @me*2; 
[/field:typeid] 
{/dede:loop} 
</body>
Copy after login

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; 
}
Copy after login

Then the template is rewritten as:

The code is as follows:

<body class="index"> 
{dede:loop table=&#39;dede_addonimages&#39; if=&#39;aid=94&#39;} 
[field:typeid function="abc(@me)" /] 
{/dede:loop} 
</body>
Copy after login

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=&#39;dede_addonimages&#39; if=&#39;aid=94&#39;} 
[field:typeid runphp=&#39;yes&#39;] 
echo $a=@me*2; 
[/field:typeid] 
{/dede:loop} 
{dede:php}var_dump($a);{/dede:php} 
</body>
Copy after login

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=&#39;dede_addonimages&#39; if=&#39;aid=94&#39;} 
[field:typeid runphp=&#39;yes&#39;] 
$GLOBALS[&#39;a&#39;]=@me*2; 
[/field:typeid] 
{/dede:loop} 
{dede:php}echo $GLOBALS[&#39;a&#39;];{/dede:php} 
</body>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template