I want to include JavaScript file in my view.
And I want to render it together and not as script file (registerScriptFile).
How can I do that?
Just renderFile() it would render any file, remember to wrap it with <script> tag.
Related
I wish to overwrite (not override), a css file with the click of a button. Currently my button simply downloads the css file instead of writing it to a path on my server. I want to write my style.css file to /templates/mypath/style.css and overwrite whenever the button is clicked. NOTE: I already have the button part working - Just need some ideas to overwrite the file to a path.
Why am I doing this?
I have built a css customizer where user can modify css from Joomla admin panel (for simplicity - lets just call it php based admin page). The user can then click APPLY CSS button to write the new css file to the server path which will then take over the styling of the website.
Here is my code:
BUTTON - HTML
Get CSS
I still need some jQuery because the code below does the trick to generate the css for me and I cannot probably convert that to php:
My JQuery Code:
$("a[download='uikit.css']").on("click", function(e) {
downloadCSS($(this), $style);
});
....
....
a.attr("href", $url.createObjectURL(new Blob([css], {type: "application/force-download"})));
Please help me to write PHP function to achieve this.
This is an example of what my button does - http://getuikit.com/docs/customizer.html - The Get CSS button at the bottom dowloads the css file but I would like to overwrite it to a path.
If you have the CSS loaded into a form simply POST the form content via Ajax to a php script on your server and use file_put_contents('/path/to/style.css',$_POST['css']) to write/overwrite the file.
Alternately, you could just submit the form to the php script and post the data that way.
I am trying to display the contents of a text file inside a div in my view. I found that in php
<?php
$myfilename = "mytextfile.txt";
if(file_exists($myfilename)){
echo file_get_contents($myfilename);
}
?>
this code can be used . Is there any such methods in Yii to display the contents inside a text file?
Update
This file is stored under components folder. It cannot be included inside the views section.
readfile()
file_get_contents
You can try these two functions. :)
There are renderFile() method in CController class.
Put your text file somewhere at views/files and use that method like
$this->renderFile('/files/filename.txt');
I'm trying to modify an existing module to insert collapsible div.
my javascript functions are in the file 'my_module.js' and look like that :
my_module.js
function myFunction1(param){
...
}
function myFunction2(paramA, paramB){
...
}
I added my_module.js in module file using drupal_add_js and then I don’t know what to do next!
<?php
drupal_add_js(drupal_get_path('module', 'my_module') . '/my_module.js');
// -----> I want to call myFunction2 here !!
?>
The website has Drupal core 6.24.
thx
Just continue with my_module.js as normal javascript file. You can try to insert div by using JQuery.
$("#foo").append("<div id='my_collapsible'>hello world</div>")
And by JQuery attach to #my_collapsible some actions you want (make it collapsible).
That method should actually work.
Have you:
Checked the file paths are correct
Checked in firebug/webkit inspector that the JS file is being loaded?
I have a navigation menu inside a CakePHP element file (views/elements/nav_default.ctp).
The file is included inside another element that is the header (views/elements/header_default.ctp) which is then included in the layout file (views/layouts/default.ctp).
I am trying to tell Cake to load a js file (webroot/js/mega_drop.js) from within the nav element like so:
<?php
$this->addScript('mega_drop');
?>
It does not get included. I looked at the documentation for addScript which just says:
Adds content to the internal scripts
buffer. This buffer is made available
in the layout as $scripts_for_layout.
This method is helpful when creating
helpers that need to add javascript or
css directly to the layout. Keep in
mind that scripts added from the
layout, or elements in the layout will
not be added to $scripts_for_layout.
This method is most often used from
inside helpers, like the Javascript
and Html Helpers.
The key part:
Keep in mind that scripts added from the layout, or elements in the layout will not be added to $scripts_for_layout.
So how do I do it then?
I guess I could add a <script src="/js/mega_drop.js"></script> to the default.ctp layout. That doesn't feel right though as it would tightly tie the layout and the element together.
Whats the CakePHP best practice way to do this?
addScript() does not load a file; it adds actual code to the $scripts_for_layout variable. The idea being that the layout is a good, common place to load your JavaScript files and code. That way you can output all the code in one location - in the head block or at the end - either way it's together. So if you are in a situation where you've got JavaScript code in the view, rather than output it inline, you can pass it up to the layout.
The best way to load a script file is with the HTML Helper- echo $this->Html->script("script or array('of', 'scripts')"); With that in mind, you could $this->set('scripts', 'mega_drop'); in the element and then call the Html Helper with that $scripts variable from the layout.
The problem with that: it won't work if your nav_default.ctp is called from the layout. $this->set() works inside of a view (or an element called from a view) because the View is rendered before the Layout. If you are calling your element from the layout, then it is too late to be setting viewVars for use in the layout. The best thing to do is set() the scripts variable from the Controller and use a if(isset($scripts)) { echo $this->Html->script($scripts); } in the layout.
Correct and valid 1.3.x CakePHP 2.0 Dev is from example.ctp file:
$this->addScript($this->Javascript->link('tab_enabler'));
$this->addScript($this->Html->css('jquery.tabs'));
This is an example of how to properly include CSS and JS files from the view and adding in the variable $scripts_for_layout to not generate validation error with the W3C as it is not correct to add the link to a css file in <BODY></BODY>
try
$this->Html->script('mega_drop', $inline=false);
in your element without the echo.
The Second parameter says to add it to the $scripts_for_layout variable.
You should be able to do this in your element, so that the javascript is only included when the element is.
I'm using symfony 1.4, I wrote a piece of js code to use in a template, and I want to put it in a JS separated file because I'll use it many times in the code.
I added the JS to the template using:
<?php use_javascript('mi_js') ?>
This templates has some ajax calls that refresh zones of the view with renderPartial method. This new zones also use the JS code, so I need to add this code in the partial view.
But if add:
<?php use_javascript('mi_js') ?>
in the partial, then it doesn't work.
To get this work I have to put all the JS code in the partial, like:
<script type="text/javascript">/*<![CDATA[*/
$('.mi_class').click(function() {
var a = $(this).parent();
...
As I told I don't want to do this.
Any idea what can I do? Any template method to do this?
Thanks in advance.
Alejandro G.
The reason why you have to put the code in the partial is the following:
When you use use_javascript('mi_js') then the (path to the) JS file gets added to the sfResponse object. Then, when the templates get rendered, all the JS files get included into the layout file via get_javascripts().
But now as you only render the partial and send the results back via Ajax, the JS files get not included.
I suggest to put your code into a function and add it to the header of your HTML file. Then in the partials you call:
<script type="text/javascript">/*<![CDATA[*/
$('.mi_class').click(the_new_function())
(Maybe you have to define parameters, I don't know).
You can attach event handlers to future DOM elements via jQuery's live() method. Another way is to bind handlers directly after loading your partial.
How about adding the JS file in a normal way to the parent page that utilises the partial, instead of the partial itself.
If the partial is called in lots of places, I'd just add it to the application's or modules' view.yml file.