如何在views.php中创建一个链接,该链接使用options.php中定义的链接变量

时间:2015-07-14 21:54:19

标签: php

如何在views.php中创建链接,该链接使用options.php中定义的链接变量

我在这里添加了“链接”信息...基本上只是从使用链接/按钮的另一个文件的按钮短代码复制代码

这是options.php

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

$options = array(
    'title'  => array(
        'type'   => 'text',
        'value' => '',
        'label' => __('Title', 'fw'),
        'desc'  => __('Add section title', 'fw')
    ),

    'box' => array(
        'type'  => 'addable-box',
        'value' => array(
            array(
                'title' => '',
            )
        ),
        'label' => __('Process', 'fw'),
        'desc'  => __('Add a process', 'fw'),
        'template' => '{{=title}}',
        'box-options' => array(
            'title' => array(
                'label' => __('Title','fw'),
                'desc'  => __( 'Enter the process title', 'fw' ),
                'type' => 'text'
            ),
            'link'   => array(
                'label' => __( 'Button Link', 'fw' ),
                'desc'  => __( 'Where should your button link to', 'fw' ),
                'type'  => 'text',
                'value' => '#' ),

            'desc' => array(
                'label' => __('Description', 'fw'),
                'type'   => 'textarea',
                'desc' => __('Enter the process content','fw') ),

            'icon_box' => array(
                'type'  => 'multi-picker',
                'label' => false,
                'desc'  => false,
                'picker' => array(
                    'icon_type'       => array(
                        'label'   => __( 'Icon', 'fw' ),
                        'desc'    => __( 'Choose icon type', 'fw' ),
                        'attr'    => array( 'class' => 'fw-checkbox-float-left' ),
                        'type'    => 'radio',
                        'value'   => 'awesome',
                        'choices' => array(
                            'awesome' => __( 'Font Awesome', 'fw' ),
                            'custom' => __( 'Custom Icon Class', 'fw' )
                        )
                    ),
                ),
                'choices' => array(
                    'awesome' => array(
                        'icon'          => array(
                            'type'  => 'icon',
                            'label' => __( '', 'fw' ),
                            'desc'  => __( 'Choose icon', 'fw' )
                        ),
                    ),
                    'custom' => array(
                        'icon'          => array(
                            'type'  => 'text',
                            'label' => __( '', 'fw' ),
                            'desc'  => __( 'Add custom icon class', 'fw' )
                        ),
                    )
                )
            ),

            'box_type' => array(
                'type'  => 'multi-picker',
                'label' => false,
                'desc'  => false,
                'picker' => array(
                    'message_type'  => array(
                        'type'  => 'select',
                        'value' => '',
                        'label' => __('Background', 'fw'),
                        'desc'  => __('Select process background type', 'fw'),
                        'choices' => array(
                            'color-1-default' => __('Type 1', 'fw'),
                            'color-2' => __('Type 2', 'fw'),
                            'color-3' => __('Type 3', 'fw'),
                            'color-4' => __('Type 4', 'fw'),
                            'custom' => __('Custom Type', 'fw'),
                        ),
                    ),
                ),
                'choices' => array(
                    'custom' => array(
                        'bg_color' => array(
                            'type'  => 'rgba-color-picker',
                            'value' => '',
                            'label' => __( 'Bg Color', 'fw' ),
                            'desc'  => __( 'Choose process bg color', 'fw' ),
                        ),
                        'text_color' => array(
                            'type'  => 'color-picker',
                            'value' => '',
                            'label' => __( 'Text Color', 'fw' ),
                            'desc'  => __( 'Choose process text color', 'fw' ),
                        ),
                    )
                )
            ),
        ),
    ),

    'class'          => array(
        'type'  => 'text',
        'label' => __( 'Custom Class', 'fw' ),
        'desc'  => __( 'Enter a custom CSS class', 'fw' ),
        'help'  => __( 'You can use this class to further style this shortcode by adding your custom CSS', 'fw' ),
    ),

);

这是views.php

  <?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

?>
<?php if(!empty($atts['title'])):?>
    <div class="procces-text-wrapper">
        <div class="w-container">
            <div class="tittle-wrapper">
                <h3 class="blue"><?php echo fw_theme_translate(esc_html($atts['title'])); ?></h3>
            </div>
        </div>
    </div>
<?php endif; ?>

<?php if(!empty($atts['box'])):?>
    <div class="process-work <?php echo esc_attr($atts['class']);?>">
        <?php foreach($atts['box'] as $box): ?>
            <?php
                //get box type
                $box_class = ($box['box_type']['message_type'] == 'custom') ? '' : $box['box_type']['message_type'];

                //custom type colors
                $bg_color = empty($box_class) ? 'style="background-color:'.$box['box_type']['custom']['bg_color'].';"' : '';
                $text_color = empty($box_class) ? 'style="color:'.$box['box_type']['custom']['text_color'].'"' : '';

                //get arrow type
                if($box_class == 'color-2')
                    $arrow = '<div class="arrow-proccess"></div>';
                elseif($box_class == 'color-3')
                    $arrow = '<div class="arrow-proccess color-2"></div>';
                elseif($box_class == 'color-4')
                    $arrow = '<div class="arrow-proccess color-3"></div>';
                else
                    $arrow = '';

                //get icon
                $icon = ($box['icon_box']['icon_type'] == 'awesome') ? $box['icon_box']['awesome']['icon'] : $box['icon_box']['custom']['icon'];

            ?>




            <div class="procces-wrapper <?php echo esc_attr($box_class);?>" data-ix="process-effect" <?php echo ($bg_color);?>>
                <?php echo do_shortcode($arrow); ?>
                <div>

                    <?php if(!empty($icon)) : ?>
                        <div class="procc-ico" data-ix="move-titte-procces" <?php echo ($text_color);?>>
                            <div class="w-embed"><i class="<?php echo esc_attr($icon);?>"></i>
                            </div>
                        </div>
                    <?php endif;?>


                    <?php if(!empty($box['title'])):?>
                        <div class="process-title" data-ix="move-titte-procces">
                            <h4 class="h-process" <?php echo ($text_color);?>><?php echo fw_theme_translate(esc_html($box['title']));?></h4>
                        </div>
                    <?php endif;?>


                    <?php if(!empty($box['desc'])):?>
                        <div class="sp-process" data-ix="move-process-text">
                            <p class="white" <?php echo ($text_color);?>><?php echo fw_theme_translate(esc_html($box['desc']));?></p>
                        </div>
                    <?php endif;?>

                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif;?>

我想制作标题部分....一个可点击的链接,指的是options.php中定义的'链接'。

 <?php if(!empty($box['title'])):?>
                        <div class="process-title" data-ix="move-titte-procces">
                            <h4 class="h-process" <?php echo ($text_color);?>><?php echo fw_theme_translate(esc_html($box['title']));?></h4>
                        </div>
                    <?php endif;?>

非常感谢任何指导。谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你试图建立一个模板系统,如果我是对的,这就是你的答案: 1-创建一个查看view.php文件内部的函数,然后使用定义的单词转换指定的单词。 2-使用require_once或include_once将函数包含到文件中。 我为你制作了这段代码: 3 - 创建一个具有任何名称的文件,然后将你的stufe写成一个像这样的数组:

      <?php

$lang=array(
"lang"=>"ar",
"direction"=>"rtl",
"date_type"=>"hjri",
"site_title"=>"my site name",
"translator"=>"your name ",
"version"=>"0.1");

然后使用以下函数将其包含在函数文件中:

      function replace_word_with_array_element($file,$template){

//the file -which has the words array to replace -
        require_once($file);
      //we assign the file -which has the template html and etc - to variable named $tpl
        $tpl= file_get_contents($template);

 //my array name is $lang so i'll work with this name
$patt_rep=$lang;
   if($patt_rep !==false){
//this function make an array with the original array key values
            $patt = array_keys($patt_rep);
//create an empty array called $pattern to save the form that when it find the replacement will be done ! 
            $pattern =array();
//looping the $patt array -array with the original array key values-
            foreach($patt as $p){
          //add the pattern into every keys of the array - the pattern is {word to be replaced}
                $pattern[] ="/{".$p."}/";
            }//end foreach
         //active this if you want to see the new array
            //print_r($pattern);

    //we create  an array and we call it $repalcement to save the original array values into it.
            $repalcement=array_values($patt_rep);
            //active this if you want to see the new array
            //print_r($repalcement);
           //The Most important part of code is this function that replace the pattern with my words 
//(the pattern array , the words array , the string that has our template)
            $tpl=preg_replace($pattern, $repalcement,$tpl);

           //the function  will return our template as a string
            return $tpl;


      }//end of the function 

现在在你的view.php文件中只包含该函数,然后像这样回显它:

      echo replace_word_with_array_element("lang.php","header.php");
      echo replace_word_with_array_element("lang.php","content.php");
      echo replace_word_with_array_element("lang.php","footer.php");

并且头文件将是这样的:

<!doctype html>
        <html>
            <head>
                <meta charset="utf-8"/>
                <title>{site_title}</title>
                <link rel="stylesheet" href="{style}">
            </head>
            <body class="main_admin_body">
                <div id="admin_wrapper">
                    <!--the content START here!-->