<tfoot id='aDn0J'></tfoot>
<i id='aDn0J'><tr id='aDn0J'><dt id='aDn0J'><q id='aDn0J'><span id='aDn0J'><b id='aDn0J'><form id='aDn0J'><ins id='aDn0J'></ins><ul id='aDn0J'></ul><sub id='aDn0J'></sub></form><legend id='aDn0J'></legend><bdo id='aDn0J'><pre id='aDn0J'><center id='aDn0J'></center></pre></bdo></b><th id='aDn0J'></th></span></q></dt></tr></i><div id='aDn0J'><tfoot id='aDn0J'></tfoot><dl id='aDn0J'><fieldset id='aDn0J'></fieldset></dl></div>

<small id='aDn0J'></small><noframes id='aDn0J'>

    • <bdo id='aDn0J'></bdo><ul id='aDn0J'></ul>

    1. <legend id='aDn0J'><style id='aDn0J'><dir id='aDn0J'><q id='aDn0J'></q></dir></style></legend>

      1. 替换 php 文件中的 {{string}}

        时间:2023-07-31

        <small id='j45WD'></small><noframes id='j45WD'>

            <tfoot id='j45WD'></tfoot>

              <tbody id='j45WD'></tbody>
          • <legend id='j45WD'><style id='j45WD'><dir id='j45WD'><q id='j45WD'></q></dir></style></legend>
              • <bdo id='j45WD'></bdo><ul id='j45WD'></ul>
                <i id='j45WD'><tr id='j45WD'><dt id='j45WD'><q id='j45WD'><span id='j45WD'><b id='j45WD'><form id='j45WD'><ins id='j45WD'></ins><ul id='j45WD'></ul><sub id='j45WD'></sub></form><legend id='j45WD'></legend><bdo id='j45WD'><pre id='j45WD'><center id='j45WD'></center></pre></bdo></b><th id='j45WD'></th></span></q></dt></tr></i><div id='j45WD'><tfoot id='j45WD'></tfoot><dl id='j45WD'><fieldset id='j45WD'></fieldset></dl></div>

                • 本文介绍了替换 php 文件中的 {{string}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在我的一个类方法中包含一个文件,并且在该文件中包含 html + php 代码.我在该代码中返回一个字符串.我明确地写了 {{newsletter}} 然后在我的方法中我做了以下事情:

                  I'm including a file in one of my class methods, and in that file has html + php code. I return a string in that code. I explicitly wrote {{newsletter}} and then in my method I did the following:

                  $contactStr = include 'templates/contact.php';
                  $contactStr = str_replace("{{newsletter}}",$newsletterStr,$contactStr);
                  

                  但是,它不会替换字符串.我这样做的唯一原因是当我尝试将变量传递给包含的文件时,它似乎无法识别它.

                  However, it's not replacing the string. The only reason I'm doing this is because when I try to pass the variable to the included file it doesn't seem to recognize it.

                  $newsletterStr = 'some value';
                  $contactStr = include 'templates/contact.php';
                  

                  那么,如何实现字符串替换方法?

                  So, how do I implement the string replacement method?

                  推荐答案

                  您可以使用 PHP 作为模板引擎.不需要 {{newsletter}} 结构.

                  You can use PHP as template engine. No need for {{newsletter}} constructs.

                  假设您在模板文件中输出一个变量 $newsletter.

                  Say you output a variable $newsletter in your template file.

                  // templates/contact.php
                  
                  <?php echo $newsletter; ?>
                  

                  要替换变量,请执行以下操作:

                  To replace the variables do the following:

                  $newsletter = 'Your content to replace';
                  
                  ob_start();        
                  include('templates/contact.php');
                  $contactStr = ob_get_clean();
                  
                  echo $contactStr;
                  
                  // $newsletter should be replaces by `Your content to replace`
                  

                  通过这种方式,您可以构建自己的模板引擎.

                  In this way you can build your own template engine.

                  class Template
                  {
                      protected $_file;
                      protected $_data = array();
                  
                      public function __construct($file = null)
                      {
                          $this->_file = $file;
                      }
                  
                      public function set($key, $value)
                      {
                          $this->_data[$key] = $value;
                          return $this;
                      }
                  
                      public function render()
                      {
                          extract($this->_data);
                          ob_start();
                          include($this->_file);
                          return ob_get_clean();
                      }
                  }
                  
                  // use it
                  $template = new Template('templates/contact.php');
                  $template->set('newsletter', 'Your content to replace');
                  echo $template->render();
                  

                  最好的一点是:您可以立即在模板中使用条件语句和循环(完整的 PHP).

                  The best thing about it: You can use conditional statements and loops (full PHP) in your template right away.

                  使用它以获得更好的可读性:https://www.php.net/manual/en/control-structures.alternative-syntax.php

                  Use this for better readability: https://www.php.net/manual/en/control-structures.alternative-syntax.php

                  这篇关于替换 php 文件中的 {{string}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在非对象上调用成员函数 query() 下一篇:php preg_replace regex 替换两个字符串之间的字符串

                  相关文章

                  最新文章

                  1. <small id='yRnR5'></small><noframes id='yRnR5'>

                    • <bdo id='yRnR5'></bdo><ul id='yRnR5'></ul>
                  2. <i id='yRnR5'><tr id='yRnR5'><dt id='yRnR5'><q id='yRnR5'><span id='yRnR5'><b id='yRnR5'><form id='yRnR5'><ins id='yRnR5'></ins><ul id='yRnR5'></ul><sub id='yRnR5'></sub></form><legend id='yRnR5'></legend><bdo id='yRnR5'><pre id='yRnR5'><center id='yRnR5'></center></pre></bdo></b><th id='yRnR5'></th></span></q></dt></tr></i><div id='yRnR5'><tfoot id='yRnR5'></tfoot><dl id='yRnR5'><fieldset id='yRnR5'></fieldset></dl></div>

                    <tfoot id='yRnR5'></tfoot>

                      <legend id='yRnR5'><style id='yRnR5'><dir id='yRnR5'><q id='yRnR5'></q></dir></style></legend>