SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,134
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)

    Template Changing using preg_replace

    I have a template file that looks like this:
    http://cpradio.net/cpCommerce/_demo...index_v0.03.txt
    I have a variable file that stores all of the variable names and the values of replacement here:
    PHP Code:
    <?php
    #####################################################################
    ## cpradio's E-commerce Script v.1.0 by Matthew Wilkin             ##
    ##  Copyright 2002. All Rights Reserved.                         ##
    #####################################################################
    ## Do Not Edit Beyond This Point                                 ##
    #####################################################################
    // Template Variables
    $templateVariables['title']        = $config['title'];
    $templateVariables['keywords']     = $config['keywords'];
    $templateVariables['description'] = $config['description'];
    $templateVariables['stylesheet'] = "{$config['template']}style.css";
    $templateVariables['description'] = $config['description'];
    $templateVariables['printer link'] = "{$_SERVER['PHP_SELF']}?print=&" .
                        
    preg_replace("(print\=\&|print\=)","",$_SERVER['QUERY_STRING']);
    $templateVariables['main display'] = "callMainDisplay()";
    ?>
    And I have a function that is supposed to replace these variables with their values:
    PHP Code:
     // Show Template Function
    function showTemplate($file) {
    global 
    $config,$templateVariables;
     
    ## Read Template
    $template fopen("{$file}","r" );
    $contents fread($template,filesize("{$file}));
    fclose($template);
     
    while(list(
    $key,$value) = each($templateVariables)) {
    if(
    preg_match("/(\{\{".$key."\}\})/isue",$content))
    echo 
    "found<br>\n";
    else
    echo 
    "false - {\{$key}}<br>\n";
    $content preg_replace("/(\{\{".$key."\}\})/isue",$value,$content);
    }
    return 
    $content;

    However I cannot get any of this to work. Any ideas?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,134
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I am a lot closer now.
    PHP Code:
    <?php
      
    #####################################################################
      ## cpradio's E-commerce Script v.1.0 by Matthew Wilkin             ##
      ##  Copyright 2002. All Rights Reserved.                          ##
      #####################################################################
      ## Do Not Edit Beyond This Point                                   ##
      #####################################################################
      // Template Variables
     
    $templateVariables['title']        = $config['title'];
     
    $templateVariables['keywords']     = $config['keywords'];
     
    $templateVariables['description']  = $config['description'];
     
    $templateVariables['stylesheet']   = "{$config['template']}style.css";
     
    $templateVariables['description']  = $config['description'];
     
    $templateVariables['printer link'] = "{$_SERVER['PHP_SELF']}?print=&" .
                        
    preg_replace("(print\=\&|print\=)","",$_SERVER['QUERY_STRING']);
     
     
    // Template Functions
     
    $templateFunctions['main display'] = "callMainDisplay()";
    ?>
    Here is the code I am using, right now the only part that does not work is the Template Functions Variables values.
    PHP Code:
     // Show Template Function
     
    function showTemplate($file) {
      global 
    $config,$templateVariables,$templateFunctions;
      
      
    ## Read Template
      
    $template fopen("{$file}","r");
      
    $contents fread($template,filesize("{$file}"));
      
    fclose($template);
      
      while (list(
    $key,$value) = each($templateVariables))
       
    $contents preg_replace("/(\{\{".$key."\}\})/isu","$value",$contents);
      
      if (
    preg_match("/(\{\{main display\}\})/isu",$contents)) {
       
    $contents str_replace("{{main display}}","",$contents);
       
    callMainDisplay();
       echo 
    "Found!!";
      }
      
      return 
    $contents;
     } 

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,134
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I am not sure how to explain this but I will give it a shot.

    I am rewriting the template method for one of my scripts and am stuck on how to load the modules into the template.

    The template consists of specific text which needs to be replaced with variable values or function output, but unfornately I think that due to the modules containing php code in them there is no way to place their information in certain areas.

    For example: If you look at http://cpradio.net/cpCommerce/_demo/...ndex_v0.03.txt you will see how the template file is setup.

    There are a few spots you will see {{main display}} and {{call module = ..........}} These are the two text codes that need to be replaced with function output.

    The problem lies in the fact that the function output requires the use of the include() function. How can I place the include function and its echo statements where those certain pieces of text appear?

    -Matt

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm.....

    Not sure you can use INCLUDE directly as a function in the way you want it to.

    I use reg_replace() myself though from replacing XML tags either with text or a PHP function call.

    I suspect that you want to INCLUDE a function to be executed based on the content to be replaced ?

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,134
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Sorry I forgot to mention that I solved that problem using:

    Code:
    	// Show Template Function
    	function showTemplate($file) {
    		global $config,$templateVariables,$templateFunctions;
    		
    		## Read Template
    		$template = fopen("{$file}","r");
    		$contents = fread($template,filesize("{$file}"));
    		fclose($template);
    		
    		while (list($key,$value) = each($templateVariables))
    			$contents = preg_replace("/(\{\{".$key."\}\})/isu","$value",$contents);
    		
    		if (preg_match("/(\{\{main display\}\})/isu",$contents)) {
    			$contents = split("{{main display}}",$contents);
    
    			echo $contents[0];
    			callMainDisplay();
    			echo $contents[1];
    		}
    		
    		if (preg_match("/(\{\{call module \= \"[a-zA-Z0-9[:space:]]+\",\"[a-zA-Z0-9[:space:]\.]+\",\"[a-zA-Z0-9[:space:]\/]+\"\}\})/isu",$contents)) {
    			echo "FOUND!!";
    		}
    	}
    However, now I am having troubles write a preg_replace function that needs to find examples like so:
    (3 parameters)
    {{call module = "Your Account","account_information.html","sidebar/"}}
    {{call module = "Quick Search","search_quicksearch.html","sidebar/"}}
    {{call module = "Random Product","product_random.html","sidebar/"}}
    {{call module = "Site Information","document_list.html","sidebar/"}}

    or (4 parameters)

    {{call module = "Your Account","account_information.html","sidebar/","shopping_cart.html"}}
    As you can see my preg_match expression is not quite right. I wish I knew expressions better but I do not. Could anyone help me with that?

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My Reg Exp skills are limited and are not up to this task you ask - sorry 8( Though there are other members out there who know more so hang on and someone will help you.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,134
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Well I got this completed with the help of a few friends and by rethinking the process. Thanks for your help Dr Livingston.

    -Matt


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •