Changeset 311

Show
Ignore:
Timestamp:
05/08/06 08:40:23 (3 years ago)
Author:
carmartin
Message:

templates: introduce get_template_element(), remove templates_draw2()

get_template_element() does the right thing when it comes to fetching template
elements.

Signed-off-by: Martin Langhoff <martin@catalyst.net.nz>

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • devel/lib/templates.php

    r291 r311  
    15301530            $template_context = addslashes($parameter['context']); 
    15311531                if (!isset($page_template_cache[$parameter['context']])) { 
    1532                     $result = get_record('template_elements','template_id',$template_id,'name',$template_context); 
     1532                    $result = get_template_element($template_id, $template_context); 
    15331533                    $page_template_cache[$parameter['context']] = $result; 
    15341534                } else { 
     
    15441544        if ($parameter['context'] === 'topmenuitem') { 
    15451545            // error_log("templates_draw pcontext " . print_r($template_element)); 
    1546         } 
    1547  
    1548     // Substitute elements 
    1549  
    1550         $functionbody = " 
    1551             \$passed = array(".var_export($parameter,true).",\$matches[1], " . $template_id . "); 
    1552             return templates_variables_substitute(\$passed); 
    1553         "; 
    1554          
    1555         // $template_element = templates_variables_substitute(array($parameter,$template_element)); 
    1556         $body = preg_replace_callback("/\{\{([A-Za-z_0-9]*)\}\}/i",create_function('$matches',$functionbody),$template_element); 
    1557          
    1558         $run_result = $body; 
    1559         return $run_result; 
    1560 } 
    1561  
    1562 function templates_draw2 () { 
    1563  
    1564     $run_result = ''; 
    1565     // Draw a page element using a specified template (or, if the template is -1, the default) 
    1566     // $parameter['template'] = the template ID, $parameter['element'] = the template element, 
    1567     // all other $parameter[n] = template elements 
    1568  
    1569     // Initialise global template variable, which contains the default template 
    1570         global $template; 
    1571          
    1572     // Initialise global template ID variable, which contains the template ID we're using 
    1573         global $template_id; 
    1574         global $page_owner; 
    1575          
    1576         global $page_template_cache; 
    1577          
    1578         if (!isset($page_template_cache)) { 
    1579             $page_template_cache = array(); 
    1580         } 
    1581          
    1582     // Get template details 
    1583         if (!isset($template_id)) { 
    1584             if (!isset($page_owner) || $page_owner == -1) { 
    1585                 $template_id = -1; 
    1586             } else { 
    1587                 // if (!isset($_SESSION['template_id_cache'][$page_owner])) { 
    1588                 if (!$template_id = get_field('users','template_id','ident',$page_owner)) { 
    1589                     $template_id = -1; 
    1590                 } 
    1591                 // } 
    1592                 // $template_id = $_SESSION['template_id_cache'][$page_owner]; 
    1593             } 
    1594         } 
    1595          
    1596     // Template ID override 
    1597         $t = optional_param('template_preview',0,PARAM_INT); 
    1598         if (!empty($t)) { 
    1599             $template_id = $t; 
    1600         } 
    1601          
    1602     // Grab the template content 
    1603         if ($template_id == -1) { 
    1604             $template_element = $template[$parameter['context']]; 
    1605         } else { 
    1606             $template_context = addslashes($parameter['context']); 
    1607                 if (!isset($page_template_cache[$template_context])) { 
    1608                     $page_template_cache[$template_context] = get_record('template_elements','template_id',$template_id,'name',$template_context); 
    1609                 } 
    1610                  
    1611                 $result = $page_template_cache[$template_context]; 
    1612                 echo "<pre>" . var_export($page_template_cache,true) . "</pre>"; 
    1613  
    1614                 if (is_array($result) && sizeof($result) > 0) { 
    1615                     $template_element = stripslashes($result[0]->content); 
    1616                 } else { 
    1617                     $template_element = $template[$parameter['context']]; 
    1618                 } 
    16191546        } 
    16201547 
     
    21072034} 
    21082035 
     2036/*** 
     2037 *** Fetches the template elements from disk or db.  
     2038 ***/ 
     2039function get_template_element ($template_id, $element_name) { 
     2040    global $CFG; 
     2041 
     2042    if ($CFG->templatestore === 'db') { 
     2043        $result = get_record('template_elements','template_id',$template_id,'name',$element_name); 
     2044    } else { 
     2045        $template_name = get_field('templates', 'name', 'ident', $template_id); 
     2046        $template_name = strtolower(clean_param($template_name, PARAM_ALPHANUM)); 
     2047        $template_file = $CFG->templatesroot . $template_name . '/' . $element_name; 
     2048         
     2049        if (! $element_content = file_get_contents($template_file)) { 
     2050            trigger_error("Problems retrieving template element from $template_file"); 
     2051        } else { 
     2052            $result = new StdClass; 
     2053            $result->content = $element_content; 
     2054            $result->ident   = $template_id; 
     2055            $result->name    = $element_name; 
     2056        } 
     2057    } 
     2058    return $result; 
     2059} 
     2060 
    21092061/*** menu_join() 
    21102062 ***  performs a join on one of