Replacing {{string}} within php file

前端 未结 6 2029
栀梦
栀梦 2020-12-08 17:08

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 th

相关标签:
6条回答
  • 2020-12-08 17:28

    maybe a bit late, but I was looking something like this.

    The problem is that include does not return the file content, and easier solution could be to use file_get_contents function.

    $template = file_get_contents('test.html', FILE_USE_INCLUDE_PATH);
    
    $page = str_replace("{{nombre}}","Alvaro",$template);
    
    echo $page;
    
    0 讨论(0)
  • 2020-12-08 17:29

    no, don't include for this. include is executing php code. and it's return value is the value the included file returns - or if there is no return: 1.

    What you want is file_get_contents():

    // Here it is safe to use eval(), but it IS NOT a good practice.
    $contactStr = file_get_contents('templates/contact.php');
    eval(str_replace("{{newsletter}}", $newsletterStr, $contactStr));
    
    0 讨论(0)
  • 2020-12-08 17:31

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

    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();
    

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

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

    0 讨论(0)
  • 2020-12-08 17:32

    Use output_buffers together with PHP-variables. It's far more secure, compatible and reusable.

    function template($file, $vars=array()) {
        if(file_exists($file)){
            // Make variables from the array easily accessible in the view
            extract($vars);
            // Start collecting output in a buffer
            ob_start();
            require($file);
            // Get the contents of the buffer
            $applied_template = ob_get_contents();
            // Flush the buffer
            ob_end_clean();
            return $applied_template;
        }
    }
    
    $final_newsletter = template('letter.php', array('newsletter'=>'The letter...'));
    
    0 讨论(0)
  • 2020-12-08 17:34

    based on @da-hype

    <?php
    $template = "hello {{name}} world! {{abc}}\n";
    $data = ['name' => 'php', 'abc' => 'asodhausdhasudh'];
    
    if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
        foreach ($m[1] as $i => $varname) {
            $template = str_replace($m[0][$i], sprintf('%s', $data[$varname]), $template);
        }
    }
    
    
    echo $template;
    ?>
    
    0 讨论(0)
  • 2020-12-08 17:39

    This is a code i'm using for templating, should do the trick

      if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
          foreach ($m[1] as $i => $varname) {
            $template = str_replace($m[0][$i], sprintf('%s', $varname), $template);
          }
        }
    
    0 讨论(0)
提交回复
热议问题