2011-01-12 53 views
0

你好朋友我正在编写一个Joomla模板,我想使用一个选项将静态文件放入CDN。PHP函数动态切换路径

我希望模板在模板选项面板中查找用户提到的CDN路径,如果没有输入,则它必须从默认本地文件夹中获取文件。

本地CDN文件夹是在模板文件夹的根目录:模板/ MyTemplate的/ CDN

的CDN文件夹中的结构是这样的:

- cdn 
---- css 
---- images 
---- js 

那么究竟是什么我期待的是......

我喜欢这个

$cdn_path = $doc->params->get("cdn-path","templates/myTemplate/cdn") 
要求用户输入的CDN路径3210

并通过templateDetails.xml文件获取它。现在,用户的输入是.... http://mycdn.com/cdn

在这里,我需要一个函数从用户输入(包括http://)采用绝对路径,并添加作为函数CDNPath()的值,如果用户不输入任何值,则它必须添加默认(templates/myTemplate/cdn)作为CDNPath()

function CDNPath(){ 

    <!-- What code should go here --> 

    return <!-- and here -->; 
} 

值在我的其他功能CSS,图像和js路径,我使用下面的函数

function CSSPath(){ 
    return className::CDNPath().'css/'; 
} 
function JSPath(){ 
    return className::CDNPath().'js/'; 
} 
function ImagePath(){ 
    return className::CDNPath().'images/'; 
} 

和我的模板我的文件链接为:

<link rel="stylesheet"href="<?php echo $className->CSSPath(); ?>template.css" type="text/css" media="screen" /> 
<script type="text/javascript" src="<?php echo $className->JSPath(); ?>scripts.js"></script> 

我majorly看到在这两个挑战,这是本地和远程路径应该是什么该功能的确切的代码。

回答

2

这可能让你开始..

<?php 
    // CDN url from: $doc->params->get("cdn-path","templates/myTemplate/cdn") 
    $cdn_path = "http://www.google.com/images/"; 

    // Local path, used if $cdn_path is not set 
    $local_path = "/images/"; 

    // Retrieve our path 
    function get_path() { 
     // Bring in variables that were declared outside of the function 
     global $cdn_path, $local_path; 

     // If $cdn_path has a value, return it. Otherwise, return $local_path 
     return (isset($cdn_path) ? $cdn_path : $local_path); 
    } 

    // Use get_path() in any SRC attribute to retrieve the path 
    echo '<img src="' . get_path() . 'logo.png">' . PHP_EOL; 
?>