Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP简单模板引擎/函数_Php_Templates_Preg Replace - Fatal编程技术网

PHP简单模板引擎/函数

PHP简单模板引擎/函数,php,templates,preg-replace,Php,Templates,Preg Replace,我需要创建一个简单的模板引擎;我不能使用Twig或Smarty等,因为项目中的设计师需要能够将她的HTML复制/粘贴到模板中,而无需任何配置、混乱或混乱。这一定很容易 因此,我创建了一些东西,通过将她的内容放在{{{content}}{{content}}标记之间,让她能够做到这一点 我唯一的问题是,我想确保如果她在标签中使用了多个空格——或者没有空格——它不会中断;i、 e.{{CONTENT}或{{{CONTENT}} 我在下面所做的就是实现这一点,但我担心这可能是矫枉过正。有人知道简化这个

我需要创建一个简单的模板引擎;我不能使用Twig或Smarty等,因为项目中的设计师需要能够将她的HTML复制/粘贴到模板中,而无需任何配置、混乱或混乱。这一定很容易

因此,我创建了一些东西,通过将她的内容放在
{{{content}}
{{content}}
标记之间,让她能够做到这一点

我唯一的问题是,我想确保如果她在标签中使用了多个空格——或者没有空格——它不会中断;i、 e.
{{CONTENT}
{{{CONTENT}}

我在下面所做的就是实现这一点,但我担心这可能是矫枉过正。有人知道简化这个函数的方法吗

function defineContent($tag, $string) {

    $offset = strlen($tag) + 6;

    // add a space to our tags if none exist
    $string = str_replace('{{'.$tag, '{{ '.$tag, $string);
    $string = str_replace($tag.'}}', $tag.' }}', $string);

    // strip consecutive spaces
    $string = preg_replace('/\s+/', ' ', $string);

    // now that extra spaces have been stripped, we're left with this
    // {{ CONTENT }} My content goes here {{ !CONTENT }}

    // remove the template tags
    $return = substr($string, strpos($string, '{{ '.$tag.' }}') + $offset);
    $return = substr($return, 0, strpos($return, '{{ !'.$tag.' }}'));

    return $return;
}

// here's the string
$string  = '{{     CONTENT  }} My content   goes here  {{ !CONTENT   }}';

// run it through the function
$content = defineContent('CONTENT', $string);

echo $content;

// gives us this...
My content goes here
编辑 最终为所有感兴趣的人创建了回购协议


我建议查看模板范围内的变量提取。 与替换方法相比,它更易于维护,开销更小,而且通常更易于设计者使用。在它的基本形式中,它只是PHP变量和短标记

它取决于您生成的哪一侧,例如表及其行(或完整的内容块)-它可能只是
;)设计师的工作量越少,你的工作量就越大。或者只提供一些呈现示例和帮助,因为复制/粘贴示例应该始终有效,即使对于未经培训的设计师也是如此

模板

该模板只是HTML混合了
-整洁

src/Templates/Article.php

<html>
 <body>
 <h1><?=$title?></h1>
 <div><?=$content?></div>
 </body>
</html>
...

// initalize
$view = new View;

// assign
$view->data['title'] = 'The title';
$view->data['content'] = 'The body';

// render
$view->render(dirname(__DIR__) . '/Templates/Article.php');
class View
{
    /**
     * Set data from controller: $view->data['variable'] = 'value';
     * @var array
     */
    public $data = [];

    /**
     * @var sting Path to template file.
     */ 
    function render($template)
    {
        if (!is_file($template)) {
            throw new \RuntimeException('Template not found: ' . $template);
        }

        // define a closure with a scope for the variable extraction
        $result = function($file, array $data = array()) {
            ob_start();
            extract($data, EXTR_SKIP);
            try {
                include $file;
            } catch (\Exception $e) {
                ob_end_clean();
                throw $e;
            }
            return ob_get_clean();
        };

        // call the closure
        echo $result($template, $this->data);
    }
}
查看/templaterender

这里的核心函数是
render()
。模板文件包含在内,变量提取在闭包中进行,以避免任何变量冲突/范围问题

src/View.php

<html>
 <body>
 <h1><?=$title?></h1>
 <div><?=$content?></div>
 </body>
</html>
...

// initalize
$view = new View;

// assign
$view->data['title'] = 'The title';
$view->data['content'] = 'The body';

// render
$view->render(dirname(__DIR__) . '/Templates/Article.php');
class View
{
    /**
     * Set data from controller: $view->data['variable'] = 'value';
     * @var array
     */
    public $data = [];

    /**
     * @var sting Path to template file.
     */ 
    function render($template)
    {
        if (!is_file($template)) {
            throw new \RuntimeException('Template not found: ' . $template);
        }

        // define a closure with a scope for the variable extraction
        $result = function($file, array $data = array()) {
            ob_start();
            extract($data, EXTR_SKIP);
            try {
                include $file;
            } catch (\Exception $e) {
                ob_end_clean();
                throw $e;
            }
            return ob_get_clean();
        };

        // call the closure
        echo $result($template, $this->data);
    }
}

我建议看一下模板范围内的变量提取。 与替换方法相比,它更易于维护,开销更小,而且通常更易于设计者使用。在它的基本形式中,它只是PHP变量和短标记

它取决于您生成的哪一侧,例如表及其行(或完整的内容块)-它可能只是
;)设计师的工作量越少,你的工作量就越大。或者只提供一些呈现示例和帮助,因为复制/粘贴示例应该始终有效,即使对于未经培训的设计师也是如此

模板

该模板只是HTML混合了
-整洁

src/Templates/Article.php

<html>
 <body>
 <h1><?=$title?></h1>
 <div><?=$content?></div>
 </body>
</html>
...

// initalize
$view = new View;

// assign
$view->data['title'] = 'The title';
$view->data['content'] = 'The body';

// render
$view->render(dirname(__DIR__) . '/Templates/Article.php');
class View
{
    /**
     * Set data from controller: $view->data['variable'] = 'value';
     * @var array
     */
    public $data = [];

    /**
     * @var sting Path to template file.
     */ 
    function render($template)
    {
        if (!is_file($template)) {
            throw new \RuntimeException('Template not found: ' . $template);
        }

        // define a closure with a scope for the variable extraction
        $result = function($file, array $data = array()) {
            ob_start();
            extract($data, EXTR_SKIP);
            try {
                include $file;
            } catch (\Exception $e) {
                ob_end_clean();
                throw $e;
            }
            return ob_get_clean();
        };

        // call the closure
        echo $result($template, $this->data);
    }
}
查看/templaterender

这里的核心函数是
render()
。模板文件包含在内,变量提取在闭包中进行,以避免任何变量冲突/范围问题

src/View.php

<html>
 <body>
 <h1><?=$title?></h1>
 <div><?=$content?></div>
 </body>
</html>
...

// initalize
$view = new View;

// assign
$view->data['title'] = 'The title';
$view->data['content'] = 'The body';

// render
$view->render(dirname(__DIR__) . '/Templates/Article.php');
class View
{
    /**
     * Set data from controller: $view->data['variable'] = 'value';
     * @var array
     */
    public $data = [];

    /**
     * @var sting Path to template file.
     */ 
    function render($template)
    {
        if (!is_file($template)) {
            throw new \RuntimeException('Template not found: ' . $template);
        }

        // define a closure with a scope for the variable extraction
        $result = function($file, array $data = array()) {
            ob_start();
            extract($data, EXTR_SKIP);
            try {
                include $file;
            } catch (\Exception $e) {
                ob_end_clean();
                throw $e;
            }
            return ob_get_clean();
        };

        // call the closure
        echo $result($template, $this->data);
    }
}

具体回答您的问题:

我唯一的问题是,我想确保如果她在标记中使用多个空格,或者没有空格,它不会中断

我在下面所做的就是实现这一点,但我担心这可能是矫枉过正。有人知道简化这个函数的方法吗

function defineContent($tag, $string) {

    $offset = strlen($tag) + 6;

    // add a space to our tags if none exist
    $string = str_replace('{{'.$tag, '{{ '.$tag, $string);
    $string = str_replace($tag.'}}', $tag.' }}', $string);

    // strip consecutive spaces
    $string = preg_replace('/\s+/', ' ', $string);

    // now that extra spaces have been stripped, we're left with this
    // {{ CONTENT }} My content goes here {{ !CONTENT }}

    // remove the template tags
    $return = substr($string, strpos($string, '{{ '.$tag.' }}') + $offset);
    $return = substr($return, 0, strpos($return, '{{ !'.$tag.' }}'));

    return $return;
}

// here's the string
$string  = '{{     CONTENT  }} My content   goes here  {{ !CONTENT   }}';

// run it through the function
$content = defineContent('CONTENT', $string);

echo $content;

// gives us this...
My content goes here

。。。功能中唯一“慢”的部分是
preg\u replace
。使用,以略微提高速度。否则,别担心。没有神奇的PHP命令来执行您希望执行的操作。

明确回答您的问题:

我唯一的问题是,我想确保如果她在标记中使用多个空格,或者没有空格,它不会中断

我在下面所做的就是实现这一点,但我担心这可能是矫枉过正。有人知道简化这个函数的方法吗

function defineContent($tag, $string) {

    $offset = strlen($tag) + 6;

    // add a space to our tags if none exist
    $string = str_replace('{{'.$tag, '{{ '.$tag, $string);
    $string = str_replace($tag.'}}', $tag.' }}', $string);

    // strip consecutive spaces
    $string = preg_replace('/\s+/', ' ', $string);

    // now that extra spaces have been stripped, we're left with this
    // {{ CONTENT }} My content goes here {{ !CONTENT }}

    // remove the template tags
    $return = substr($string, strpos($string, '{{ '.$tag.' }}') + $offset);
    $return = substr($return, 0, strpos($return, '{{ !'.$tag.' }}'));

    return $return;
}

// here's the string
$string  = '{{     CONTENT  }} My content   goes here  {{ !CONTENT   }}';

// run it through the function
$content = defineContent('CONTENT', $string);

echo $content;

// gives us this...
My content goes here

。。。功能中唯一“慢”的部分是
preg\u replace
。使用,以略微提高速度。否则,别担心。没有神奇的PHP命令来完成您想要做的事情。

我认为她可以简单地了解忽略什么(您的循环、条件等…+您可以使用其他语法,使事情更简单:)嗯,您根本不需要这个。。。但是,那只是我…:)@尽管这超出了
内容
标记的范围,但她需要访问页面的许多部分,这些部分都有许多标记,如果我用太多的PHP将其弄乱,她会失去她的东西。:)我认为她可以简单地学会忽略什么(你的循环、条件等…+你可以使用其他语法,让事情变得更简单:)嗯,你根本不需要这个。。。但是,那只是我…:)@尽管这超出了
内容
标记的范围,但她需要访问页面的许多部分,这些部分都有许多标记,如果我用太多的PHP将其弄乱,她会失去她的东西。:)