0

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 以使其能够解析字符串?

4

2 回答 2

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');

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

于 2013-02-20T08:44:20.930 回答
0

不是真正的答案,而是可以尝试的东西:

我认为如果你破解了 _get_contents 方法来检测 .php 文件扩展名,并且如果有一个 php 扩展名调用$this->CI->load->view('asset.php', true),而不是file_get_contents()你可能在中途。根据我的细读,这可能仅在您使用 minify 或 combine 时才有效。

于 2013-02-15T22:02:16.427 回答