Home >CMS Tutorial >DEDECMS >How to use if for multi-branch judgment in DEDE template
How to use if in DEDE template for multi-branch judgment?
When we use dede as a template, We often encounter situations where different content needs to be displayed separately based on a certain conditional judgment, which is the multi-branch judgment in the dede template. For example, typically, we need to display different risk prompt icons according to the different values of the risk level field risklevel in the risk management database table. As shown in Figure 1.
The icons in front of the investment project names in the red box represent different risk levels. There are 4 risk levels: AA, D, E, and HR. What is saved in the database table is one of these four level values, as shown in Figure 2.
Our risk level field risklevel can get its value in the list template, because we have set this field in the content model to allow it to be included in the list template value.
As shown in Figure 3.
In this way, we can easily get the value of the risk level through
{dede:listpagesize='10'} [field:risklevel/] {/dede:list}
in the list template.
But the prompt icons corresponding to our different risk levels are located in the picture directory, and the names of these prompt picture files have no rules, so we need to use if judgment to set different values according to the different risk levels. The prompt image path.
So how should we judge?
You need to use the [field:array] tag, so how should we judge it?
We first want to introduce to you a very useful dede tag: [field:array] tag, which is the abbreviation of the tag {dede:field name='array' /}, which is used in lists Inside the tag, for example: {dede:list} tag, {dede: arclist} and other tags.
We know that inside the list tag we can get the value of a database table field in the form of [field:field name/], then when we want to make branch judgments, for example: based on risk Different level values result in different icon paths. At this time, we must use the [field:array] tag. The [field:array] tag represents a record in the database table obtained by query. The specific judgment code is as follows:
[field:array runphp='yes'] if(@me['risklevel']=="HR")@me="HR.png"; else if(@me['risklevel']=="D")@me="D.png"; else if(@me['risklevel']=="AA")@me="AA.png"; [/field:array] {/dede:list}
It means that if the value of the risk level field obtained by the query is HR, then HR.png will be output. If the value of the risk level field obtained by the query is D, then D.png will be output. If the value of the risk level field obtained by the query is AA, then AA.png will be output. HR.png here is just a string representing the location of the image file. You can set it to any image path. The above code It is a branch conditional output statement. @me represents the output variable. When you first enter the label field:array, @me represents a record in the database, and its data type is an array. We judge based on the value of risklevel, so as to @me is set to different values. When exiting the tag field:array, the value of @me will be displayed in the original position.
The function of this tag is very powerful. It is suitable for our most common scenarios. When we need to perform if branch judgment based on specific conditions to display different content in the template page, use the field:array tag. It will bring us great convenience.
The above is the detailed content of How to use if for multi-branch judgment in DEDE template. For more information, please follow other related articles on the PHP Chinese website!