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()
并在某些函数中将其传递给 Carabiner js_string()
,例如......
$custom_js = $this->load->view("js_with_php_in_it", null, true);
$this->carabiner->js_string($custom_js);
实际上,我已经通过创建一个名为 Carabiner 的方法以复杂的方式解决了这个问题,该方法from_string($type, $str)
将“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 以使其能够解析字符串?