如何创建像wp_head()这样的函数?

时间:2016-05-14 05:37:23

标签: php function

如何创建wp_head()等函数?

这里我有三个功能:

  • add_css() - 将css文件调用到head标记
  • add_script() - 将javascript文件调入head标记
  • render_head() - 显示上述两项功能的结果

当我在body标记中调用函数add_css("path/to/style.css")add_script("path /to/javascript.js")时,或者通过调用render_head()在head标记中看起来像这样:

    <link href ="path/to/style.css">
    <script src ="path/to/javascript.js"></script>

我应该怎样做才能成功?

2 个答案:

答案 0 :(得分:1)

它可能更优雅,但这有效:

<?php

class Head {
    public $tohead="<!DOCTYPE html>\n  <html>\n    <head>\n";
    public function addcss($css) {
        $this->tohead .= "      <link rel='stylesheet' href='$css'>\n";
    }

    public function addscript($js) {
        $this->tohead .= "      <script type='text/javascript' src='$js'></script>\n";
    }

    public function render() {
        $this->tohead .= "    </head>\n";
    }
}

$head = new Head;
$head->addcss('/assets/css/my.css');
$head->addscript('/assets/js/my.js');
$head->render();
echo $head->tohead;

答案 1 :(得分:1)

据推测,您想要创建类似Wordpress的功能。这个问题有点宽泛,可以主要以意见为基础,所以这绝不是唯一的方法,而是众多的方法之一。 Wordpress喜欢使用全局变量,所以类似于这个可能会像你描述的那样起作用:

<强> /classes/RenderEngine.php

class RenderEngine
    {
        // These are just some containers
        private static  $renderer;
        private static  $topage;
        private static  $headElement;
        // This will save to a header array
        public static function addToHeader($value,$type)
            {
                self::$renderer[$type][]    =    $value;
            }
        // This saves to a separate array that can be used to pull
        // specific types of element
        public  static  function saveTo($val,$type)
            {
                self::$headElement[$type]   =   $val;
            }
        // This assembles the css/js arrays and implodes the layout(s)
        public static function getLayout()
            {
                if(!isset(self::$renderer))
                    self::$renderer =   array();

                ob_start();
                foreach(self::$renderer as $type => $val) {
                    if($type == 'js')
                        echo '<script type="text/javascript" src="'.implode('"></script>'.PHP_EOL.'<script type="text/javascript" src="',$val).'"></script>'.PHP_EOL;
                    elseif($type == 'css')
                        echo '<link rel="stylesheet" type="text/css" href="'.implode('" />'.PHP_EOL.'<link rel="stylesheet" type="text/css" href="',$val).'" />'.PHP_EOL;
                }
                self::$topage   =   ob_get_contents();
                ob_end_clean();

                return self::$topage;
            }
        // This will try and fetch any registered elements
        public  static  function getElement($type)
            {
                return (!empty(self::$headElement[$type]))? self::$headElement[$type] : false;
            }
    }

<强> /functions/functions.php

// General function to add the layout array(s)
function add_element($value,$type)
    {
        if(is_array($value)) {
            foreach($value as $spot)
                \RenderEngine::addToHeader($spot,$type);
        }
        else
            \RenderEngine::addToHeader($value,$type);
    }
// Uses the element array to add css
function add_css($value)
    {
        add_element($value,'css');
    }
// Uses the element array to add js
function add_script($value)
    {
        add_element($value,'js');
    }
// This adds to our general array
function add_header_title($value)
    {
        \RenderEngine::saveTo($value,'title');
    }
// This renders the header
function render_header()
    {
        // Start a buffer to cache the string
        ob_start();
        // Don't store the html in the function, rather include it
        include(__DIR__.'/../renderlib/render_header.php');
        // Save the string
        $data   =   ob_get_contents();
        // Clear the buffer
        ob_end_clean();
        // Return the layout string
        return $data;
    }

<强> /renderlib/render_header.php

<!DOCTYPE html>
<html>
<title><?php echo \RenderEngine::getElement('title'); // Retrieve stored element ?></title>
<head profile="http://www.w3.org/2005/10/profile">
<meta name="viewport" content="width=device-width">
<?php echo \RenderEngine::getLayout(); // Render the css/js ?>
</head>

使用:

// Load relevant helpers
require_once(__DIR__.'/classes/RenderEngine.php');
require_once(__DIR__.'/functions/functions.php');
// Add elements
add_css(array('/file/test/style.css','/file/test/style2.css'));
add_css('/file/test/style3.css');
add_script(array('/js/script.js','/js/script1.js'));
add_script('/js/script2.js');
add_header_title('Page Title');
// Render header
echo render_header();

给你:

<!DOCTYPE html>
<html>
<title>Page Title</title>
<head profile="http://www.w3.org/2005/10/profile">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="/file/test/style.css" />
<link rel="stylesheet" type="text/css" href="/file/test/style2.css" />
<link rel="stylesheet" type="text/css" href="/file/test/style3.css" />
<script type="text/javascript" src="/js/script.js"></script>
<script type="text/javascript" src="/js/script1.js"></script>
<script type="text/javascript" src="/js/script2.js"></script>
</head>