Drupal's .info file doesn't load multiple js files - javascript

Although I included multiple .js files in Drupal 7's .info file as advised, only the first ones do load - up to
scripts[] = js/jquery.cycle2.min.js
but not further. Changing the order doesn't help, it seems to stop loading every time at the first cycle-js file.
What am I missing there? Or is it only possible to include max 3 .js for some reason/setting?
core = 7.x
engine = phptemplate
stylesheets[all][] = style.css
stylesheets[all][] = media.css
scripts[] = js/custom.js
scripts[] = js/jquery.cycle2.min.js
scripts[] = js/jquery.cycle2.caption2.min.js
scripts[] = js/jquery.js

You can use following function to add JS on your site:
function mytheme_preprocess_page(&$vars, $hook) {
if (true) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme1.js');
$vars['scripts'] = drupal_get_js(); // necessary in D7?
}
}

Try another way to include js file .
/your_theme/template.php
function mytheme_preprocess_page(&$vars, $hook) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/custom.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.cycle2.min.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.cycle2.caption2.min.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.js');
$vars['scripts'] = drupal_get_js();
}
Thank You.

Related

Codeigniter how to load all js files from folder

Codeigniter how to load all js files from folder, without mentioning name of file.
Example- I want to get all js file from one folder. I know the path up to folder but js files are created dynamically when build is created so do not know exact name of files to load tradional way.
Maybe this solution will going to work
$js_path = rtrim(FCPATH . 'assets/js', '/'); // FCPATH = root folder of your codeigniter project
$js_files = glob("{" . $js_path . "/*.js}", GLOB_BRACE);
for($i = 0; $i < count($js_files); $i++){
echo read_file($js_files[$i]);
}
code not tested but I hope this will solve the problem
I tried following that worked for me
$folderpath = $_SERVER['DOCUMENT_ROOT'].'/resources/common/js/';
$fileName = array();
$fileName = get_filenames($folderpath);
$jsFiles = array();
foreach($fileName as $file){
$url = 'resources/common/js/'.$file;
$jsFiles[]['js'] = base_url($url);
}
$this->load->vars($jsFiles);

Recursively generate script tags for all JavaScript files within a directory with PHP

I'm building a complex app with lots of JavaScript files in lots of sub-directories. I know I want to include them all (it won't affect performance), but I don't want to manually create a script tag for each. Given that all of my files are children of a "/js" directory, how could I dynamically generate the script tags for each with PHP? Something like this:
// first somehow recursively get all .js files, then:
foreach($files as $file) {
echo '<script src="' . $file->path . '"></script>';
}
Most elegant way is to use SPL in my opinion.
$dirIterator = new RecursiveDirectoryIterator("/path/to/js");
$iterator = new RecursiveIteratorIterator(
$dirIterator,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if($file->getExtension() == 'js') {
// You probably have to adjust the full path according to your DOC_ROOT
$url = $file->getPathname();
echo '<script src="' . $url . '"></script>';
}
}
Have a look at http://php.net/manual/en/class.splfileinfo.php to see what else you can do with $file .

Yii - Adding CDN Javascript to my flow

I have inherited someone else's PHP code & am totally clueless about the language.
I want to add Babel from CDN into my page header right after React & ReactDOM local files are loaded, however, before the app javascript files load up.
Here is the relevant chunk where the files are arranged at:
protected -> controllers -> appcontroller.php
public function includeStyles() {
$baseUrl = Yii::app()->baseUrl;
$styles = Yii::app()->getClientScript();
$styles->registerCssFile($baseUrl . '/css/DA_TemSelect.css?' . $this->version);
$styles->registerCssFile($baseUrl . '/css/styles.css?' . $this->version);
}
public function includeScripts() {
$baseUrl = Yii::app()->baseUrl;
$scripts = Yii::app()->getClientScript();
$scripts->registerScriptFile($baseUrl . '/js/libraries/jquery-ajax.js');
$scripts->registerScriptFile($baseUrl . '/js/libraries/react.js');
$scripts->registerScriptFile($baseUrl . '/js/libraries/react-dom.js');
Here is where I want the CDN Babel js file to load, i.e. right after React, however, before the following js files.
$scripts->registerScriptFile($baseUrl . '/js/aphrodite/models/Element.js?'. $this->version);
I should mention, I tried this link but to no avail.
I appear to be using Yii version 1.1.13 (as per the CHANGELOG).
Thanks,

write a file on local disk from web app [duplicate]

I am trying to create and save a file to the root directory of my site, but I don't know where its creating the file as I cannot see any. And, I need the file to be overwritten every time, if possible.
Here is my code:
$content = "some text here";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
How can I set it to save on the root?
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
If you are running PHP on Apache then you can use the enviroment variable called DOCUMENT_ROOT. This means that the path is dynamic, and can be moved between servers without messing about with the code.
<?php
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);
?>
This question has been asked years ago but here is a modern approach using PHP5 or newer versions.
$filename = 'myfile.txt'
if(!file_put_contents($filename, 'Some text here')){
// overwriting the file failed (permission problem maybe), debug or log here
}
If the file doesn't exist in that directory it will be created, otherwise it will be overwritten unless FILE_APPEND flag is set.
file_put_contents is a built in function that has been available since PHP5.
Documentation for file_put_contents
fopen() will open a resource in the same directory as the file executing the command. In other words, if you're just running the file ~/test.php, your script will create ~/myText.txt.
This can get a little confusing if you're using any URL rewriting (such as in an MVC framework) as it will likely create the new file in whatever the directory contains the root index.php file.
Also, you must have correct permissions set and may want to test before writing to the file. The following would help you debug:
$fp = fopen("myText.txt","wb");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$content);
fclose($fp);
}

List files, read files as links, php?

This is a strange request I suppose, but I have a directory full of txt files. For example:
- david_smith_interview.txt -
- beth_martin_interview.txt -
- sally_smithart_interview.txt
The contents of these text files are a link to their interview in an mp3 format, for example, if you open the file david_smith_interview.txt, it is simply this:
http://www.interviews/employees/david_smith.mp3
All of the other text files follow the same format. They are simply links to their mp3 interview.
I am trying to use something like below to list the text files:
<?php
$directory = "/employees/";
$phpfiles = glob($directory . "*.txt");
foreach($phpfiles as $phpfile)
{
echo $phpfile; // This will list the files by name
// How can I output something to reflect this:
// david_smith_interview
}
?>
So I am asking is it possible that the text file can be "read" and used as the actual link?
Any thoughts?
Replace _interview.txt with .mp3
echo "" . str_replace(".txt", "", $phpfile). "\";
Since those are .txt files you can just read them one by one to a variable and then echo the result in a for-loop.
In pseudo:
$paths fetch_paths()
$urls = array();
foreach($paths as $path)
{
$url=fopen($path);
array_push($urls,fgets(url)); // Assuming there's only one link per file and it is on one line.
}
foreach($urls as $url)
{
echo <Your formatted link here>
}

Categories

Resources