dongpo5207 2017-06-23 21:10
浏览 8
已采纳

基于函数参数的变量内容

I'd like the have the output of the html return two possible markups based on a parameter the user can set. The parameter text_mode will be defined by the user by adding basic or advanced.

Right now, the $output is set for testing to show the basic version whereby the user's string is wrapped in a <p>. If the text_mode is set to advanced, then it's not wrapped.

<?php
function PCHiddenTextBlock( $settings = array() ) {

//---- Get Settings ----
//The functions default settings will be merged with what's passed in.

$settingsDefault = array(
    'small_heading' => '',
    'text' => '',
    'text_mode' => 'basic', //or advanced
    'color_scheme' => 'accent4',//accent4 or accent1
    'container_id' => '',
    'container_class' => 'x_extraContent',
);

$settings = array_merge($settingsDefault, $settings);


//---- Set Variables ----
//These will allow the markup build up to be as clean as possible.


//If container_id is set, prepare the attribute
$has_container_id = strlen($settings['container_id']) > 0;
$possible_container_id_attribute = ($has_container_id) ? " id='{$settings['container_id']}'" : "";

//Color scheme variables
switch($settings['color_scheme']) {
    case 'accent1':
        $gcol_color_class = 'bg-color-accent1-C';
                $color_accent_class = 'color-accent1-8';
                $hover_color_class = 'hover-color-base-4';
        break;

    default: //accent4
        $gcol_color_class = 'bg-color-accent4-D';
                $color_accent_class = 'color-accent4-A';
                $hover_color_class = 'hover-color-base-4';
}


//---- Build Output ----
//Line by line, concatenating strings with new line and tab characters.
$output  = "
<!-- Hidden Text Block -->";
$output .= "
<div class='{$settings['container_class']} gcol-1 {$gcol_color_class}'{$possible_container_id_attribute}>";
$output .= "
\t<div class='padbox-standard-content'>";
$output .= "
\t\t<h2 class='small-heading color-accent1-9'>{$settings['small_heading']}</h2>";
$output .= "
\t\t<p>{$settings['text']}</p>"; // basic version
$output .= "
\t\t<a href='' class='x_extraContentClose box-close-icon {$color_accent_class} {$hover_color_class}'></a>";
$output .= "
\t</div><!-- padbox-standard-content-->";
$output .= "
</div><!-- Hiddent Text Block -->";

//---- Return Output ----
  return $output;
}
  • 写回答

1条回答 默认 最新

  • dongzhui9936 2017-06-23 21:15
    关注

    Just use an if statement:

    if ($settings['text_mode'] == 'basic') {
        $output .= "
    \t\t<p>{$settings['text']}</p>"; // basic version
    } else {
        $output .= "
    \t\t{$settings['text']}<"; // advanced version
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)