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,
Related
We made a modulized app in laravel 5.0, So we want to get our css and js files from another directory (non public directory) such as a module directory.
We searched the stacks and couldn't find any solution. How can we do that?
Thanks
hint: the asset() function doesn't work except in public directory.
You have two options:
1.You can setup a route to serve files from your directory.
Route::get('assets/{filename}', function ($filename){
$path = 'path_to_your_folder/'.$filename;
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
2.Make a symbolic link between the directories.
ln -s /path/to/your_assets_folder /path/to/public/assets
Got it from: this similar question
Use url function url('your_address')
When I'm using controller function with parameters the rendered view just seems to forget every included .js files.
public function view($id = null) {
if(!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if(!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
If I take parameters away and put variable '$id = 1' on function the view with postID 1 renders okay in 'posts/view'.
I included javascript files to default.ctp in traditional way:
echo "script type='text/javascript' SRC='../js/jquery-1.9.1.min.js'></script>";);
(it includes '<' but this text editor won't me type it for safety reasons I guess)
I don't have knowledge about 'js helpers' of cakePHP. Can't I use javascript in traditional way?
Site renders okay in every other view (e.g. posts/add) and .js files are included in source code of 'posts/view/1'
The problem
You're using relative paths to the javascript;
<script src='../js/jquery-1.9.1.min.js'></script>
In this url, ../ means '1 directory up from the current location`, so when you're currently visiting this URL;
http://mysite.com/home/
Then your browser will correctly try to load the script from;
http://mysite.com/js/jquery-1.9.1.min.js
However, if you're visiting this url;
http://mysite.com/home/and/some/more/
Then the browser will look for the JavaScript here:
http://mysite.com/home/and/some/js/jquery-1.9.1.min.js
How to fix the problem
Use absolute paths for all 'assets' (CSS, JavaScript, Images);
src='/js/jquery-1.9.1.min.js'
Output the script-tags using CakePHP Helpers (after all, that's what they are meant for: to simplify your work :), e.g. echo $this->Html->script('jquery-1.9.1.min');
I am currently working on a project where i wanna implement jQuery to cakephp 2.0.
I have followed the guide at:
http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html
i.e. I have downloaded jquery-1.8.1.js (also tried the .min.js file) and put it in app/webroot/js.
In the default layout file i have added
echo $this->Html->script('jquery');
and just before the end of the body tag i have added
echo $this->Js->writeBuffer();
In my controller i have added
public $helpers = array('Js' => array('Jquery'));
When i reload my page and check the source code I see that the link to the jQuery file works correctly.
But when I try to add a simple script (just adding an alert) like this (in the view file):
$alert = $this->Js->alert('Hey there');
nothing happens...
Any kind person out there that might have any suggestion to what I do wrong?
I have spent hours looking looking at the internet and following different guides but still can get a simple thing as an alert working.
According to CakePHP 2.0 documentation: "by default, alert does not buffer, and returns the script snippet."
So by default:
echo $this->Js->alert('Hey there'); // outputs alert("Hey there");
To override this behaviour and add the script to the buffer:
echo $this->Js->alert('Hey there', true);
To write the buffer (commonly right before </body>):
echo $this->Js->writeBuffer();
As an alternative you can use scriptBlock:
$jscript = "alert('Hey there!');";
echo $this->Html->scriptBlock($jscript, array('inline'=>false));
Is it possible to make a static RSS feed file that can be moved from server to server without change? It appears that relative links are not fully supported in RSS, but the latest info I've found is quite old; something javascripty would work in HTML, but not in RSS XML.
Background:
I'm working with an HTML publishing project that generates static RSS feed files for some lists of resources. To update, you'd re-publish the static file to the same location. One export option is to save to your filesystem and then transfer to the server manually, but for RSS feeds we're currently requiring the destination URL to be entered on export.
In the script that generates your RSS, you could do something like this:
<?php
// example: $_SERVER['HTTP_HOST'] = 'mysite.com';
$mysite = 'http://' . $_SERVER['HTTP_HOST'];
// the page you're linking to
$thislink = 'mypage.html';
/* code that generates your RSS */
// output the link
echo '<a href="' . $mysite . '/' . $thislink . '">';
/* more code that generates your RSS */
?>
Output:
<a href="http://mysite.com/mypage.html">
CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see changes made to these files without requiring the user to clear their browser cache. Also want a solution that works well with a version control system such as Subversion.
Some solutions I have seen involve adding a version number to the end of the file in the form of a query string.
Could use the SVN revision number to automate this for you: ASP.NET Display SVN Revision Number
Can you specify how you include the Revision variable of another file? That is in the HTML file I can include the Revision number in the URL to the CSS or Javascript file.
In the Subversion book it says about Revision: "This keyword describes the last known revision in which this file changed in the repository".
Firefox also allows pressing CTRL+R to reload everything on a particular page.
To clarify I am looking for solutions that don't require the user to do anything on their part.
I found that if you append the last modified timestamp of the file onto the end of the URL the browser will request the files when it is modified. For example in PHP:
function urlmtime($url) {
$parsed_url = parse_url($url);
$path = $parsed_url['path'];
if ($path[0] == "/") {
$filename = $_SERVER['DOCUMENT_ROOT'] . "/" . $path;
} else {
$filename = $path;
}
if (!file_exists($filename)) {
// If not a file then use the current time
$lastModified = date('YmdHis');
} else {
$lastModified = date('YmdHis', filemtime($filename));
}
if (strpos($url, '?') === false) {
$url .= '?ts=' . $lastModified;
} else {
$url .= '&ts=' . $lastModified;
}
return $url;
}
function include_css($css_url, $media='all') {
// According to Yahoo, using link allows for progressive
// rendering in IE where as #import url($css_url) does not
echo '<link rel="stylesheet" type="text/css" media="' .
$media . '" href="' . urlmtime($css_url) . '">'."\n";
}
function include_javascript($javascript_url) {
echo '<script type="text/javascript" src="' . urlmtime($javascript_url) .
'"></script>'."\n";
}
Some solutions I have seen involve adding a version number to the end of the file in the form of a query string.
<script type="text/javascript" src="funkycode.js?v1">
You could use the SVN revision number to automate this for you by including the word LastChangedRevision in your html file after where v1 appears above. You must also setup your repository to do this.
I hope this further clarifies my answer?
Firefox also allows pressing CTRL + R to reload everything on a particular page.
In my opinion, it is better to make the version number part of the file itself e.g. myscript.1.2.3.js. You can set your webserver to cache this file forever, and just add a new js file when you have a new version.
When you release a new version of your CSS or JS libraries, cause the following to occur:
modify the filename to include a unique version string
modify the HTML files which reference the library to point at the versioned file
(this is usually a pretty simple matter for a release script)
Now you can set the Expires for the CSS/JS to be years in the future. Whenever you change the content, if the referencing HTML points to a new URI, browsers will no longer use the old cached copy.
This causes the caching behavior you want without requiring anything of the user.
I was also wondering how to do this, when I found grom's answer. Thanks for the code.
I struggled with understanding how the code was supposed to be used. (I don't use a version control system.) In summary, you include the timestamp (ts) when you call the stylesheet. You're not planning on changing the stylesheet often:
<?php
include ('grom_file.php');
// timestamp on the filename has to be updated manually
include_css('_stylesheets/style.css?ts=20080912162813', 'all');
?>