将资产作为字符串传递给CodeIgniter Carabiner库

时间:2013-02-13 18:10:32

标签: php codeigniter carabiner

Carabiner只会接受js()css()个功能中的路径:

$this->load->library('carabiner');
$this->carabiner->js('some/file/in/script/dir');
$this->carabiner->css('some/file/in/style/dir');
$this->carabiner->display();

我希望通过在$this->load->view()中设置返回标志来返回一些脚本和样式,然后在js_string()之类的函数中将其传递给Carabiner,例如......

$custom_js = $this->load->view("js_with_php_in_it", null, true);
$this->carabiner->js_string($custom_js);

我实际上已经通过创建一个名为from_string($type, $str)的Carabiner方法以一种错综复杂的方式解决了这个问题,该方法将'js'或'css'作为第一个参数,将字符串本身作为第二个参数:

function from_string($type, $str){
    //we'll name the file using this.
    $uniq = uniqid();
    //create a temporary file in the system's /tmp directory.
    $tmpAsset = tempnam("/tmp", $uniq);
    //Carabiner requires that your scripts are relative to $config['script_dir']
    //create a symbolic link in this directory because you can't pass it absolutes.
    $this->symbolicAsset = "assets/{$type}/{$uniq}";
    //open the file named with the unique ID in /tmp and set it up for writing.
    $handle = fopen($tmpAsset, "w");
    //write the script or style to this temporary file.
    fwrite($handle, $str);
    //point the symlink at it
    symlink($tmpAsset, $this->symbolicAsset);
    //pass this directory off to $this->carabiner->css() or $this->carabiner->js()
    $this->$type($uniq);
}

然后我unlink() Carabiner的__destruct()中的临时文件......但是,出于明显的开销原因,我非常讨厌这个解决方案:我正在创建一个包含与原始内容相同内容的临时文件因为Carabiner严格的资产目录规则。有人在过去修改了Carabiner以使其能够解析字符串吗?

2 个答案:

答案 0 :(得分:1)

这是这个库中缺少的好功能,我在看到这个问题后在我的主要项目中使用了这个库我在库中做了一些修改,接受javascripts和css样式作为字符串或字符串数​​组,它不会创建临时文件将在页面中创建脚本标记和样式,这个库现在将如何工作。

javascript字符串

// script passed as string
$this->carabiner->js_string('alert("test script")'); 

// script passed as array
$this->carabiner->js_string(array('alert("test script")','alert("test script")'); 

// script passed as string with group name
$this->carabiner->js_string('alert("test script")','group_name'); 

// script passed as array with group name
$this->carabiner->js_string(array('alert("test script")','alert("test script")','group_name'); 

//load javascript along with javascript files without group name
$this->carabiner->display('js');

//load javascript along with javascript files with group name
$this->carabiner->display('group_name');

css样式字符串

// style passed as string
$this->carabiner->css_string('p{background:red}'); 

// styles passed as array
$this->carabiner->css_string(array('p{background:red}','p{background:red}'); 

// style passed as string with group name
$this->carabiner->css_string('p{background:red}','css_group_name'); 

// style passed as array with group name
$this->carabiner->css_string(array('p{background:red}','p{background:red}','css_group_name'); 

//load style along with javascript files without group name
$this->carabiner->display('css');

//load styles along with javascript files with group name
$this->carabiner->display('css_group_name');

这是我刚创建的git repo并发送了pull Git Repo

的请求

答案 1 :(得分:0)

不是一个真正的答案,但需要尝试:

我认为如果您攻击_get_contents方法来检测.php文件扩展名,并且如果有一个php扩展名调用$this->CI->load->view('asset.php', true)而不是file_get_contents(),那么您可能会在那里。从我的阅读中,这可能只有在你使用minify或combine时才有效。