Unable to get addScriptDeclaration() to work inside a module - joomla

I have a very simple module that is loaded from a plugin via
<?php echo JHtml::_('content.prepare', '{loadposition mymodule}'); ?>
The module position is setup, and the default.php template renders the output appropriately. What doesn't work is the following:
$js = <<<FOO
(function ($) {
console.log("javascript loaded");
})(jQuery);
FOO;
$doc = JFactory::getDocument();
$doc->addScriptDeclaration($js); // no js is output in HTML here?? Why?
JModuleHelper::getLayoutPath('mod_mymodule');
The embedded javascript is not rendered with the rest of the HTML.

Related

What's good way to load header and footer view in codeigniter?

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view.
Like
<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>
Is this a good way to show header and footer in codeigniter.
Thank!
You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:
views
- header.php
- footer.php
- template.php
- home.page
In header.php you should have all your header and footer content which you wants to display on all pages.
Now in your template move all your includes.
template.php
<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>
here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:
public function home()
{
$data['meta_title'] = $this->lang->line('home_meta_title');
$data['meta_description'] = $this->lang->line('home_meta_description');
$data['meta_keywords'] = $this->lang->line('home_meta_keywords');
$data['main_content'] = 'home';
$this->load->view('template',$data);
}
$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

CKEditor filebrowser plugin

Can anyone explain with examples for everyone how to integrate CKEditor and external file browser? Where to put this code:
CKEDITOR.replace( 'editor1', {
filebrowserBrowseUrl: '/browser/browse.php',
filebrowserUploadUrl: '/uploader/upload.php'
});
And what shoud return these:
browse.php
upload.php
I think the answer for this question should be here. So many peeople asked this question, but there is no definitive guide to this plugin. SO members, maybe some of you can?
filebrowserBrowseUrl will be the url of a page that allows a user to choose a file on the server. This page will be opened in a new window.
These arguments will be added to the url CKEditor=editor&CKEditorFuncNum=2&langCode=en-gb
CKEditor is the current editor
CKEditorFuncNum is the function to call(pass this value to callFunction)
langCode is the language
in the popup window call
window.opener.CKEDITOR.tools.callFunction(CKEditorFuncNum, 'img_url');
to set the selected image and optionally window.close(); to close the window.
e.g.
<?php
$files = glob('/images/directory/*.{jpg,png,gif,bmp}', GLOB_BRACE);
if (count($files) > 0){
echo 'Select a file <ul>';
foreach ($files as $file) {
echo '<li><button onclick="select(this)" data-name="'.$file.'">.basename($file).</button></li>';
}
echo '</ul>';
}
?>
<script>
function select(el){
window.opener.CKEDITOR.tools.callFunction(<?php echo (int)$_GET['CKEditorFuncNum'] ?>, el.getAttribute('data-name'));
window.close();
}
</script>
filebrowserUploadUrl will be the url that handles the image upload.
Its just a standard file upload handler except you run some js on the page.
and same arguments are added to the url as with filebrowserBrowseUrl.
<?php
... handle the upload
?>
<script>
window.parent.CKEDITOR.tools.callFunction(<?php echo (int)$_GET['CKEditorFuncNum'], ', ', json_encode($uploaded_image_url), ', ', json_encode($message_on_failure) ?>);
</script>";
Simple Demo http://jsfiddle.net/mowglisanu/f2ztp/show/ (upload doesn't actually work though)
http://jsfiddle.net/mowglisanu/f2ztp/
http://jsfiddle.net/mowglisanu/GuA6s/show/
http://phpfiddle.org/api/run/dv4-e70

How to close a Colorbox from a CodeIgniter Controller

I want to close Colorbox from a controller. I used this code but it will not work:
<?= "$(document).ready(function() { $.colorbox.close(); });"; ?>
To me they are two options:
a)(Easy) inside your controller print js , example:
echo "<script type='text/javascript'> parent.$.fn.colorbox.close(); </script>";
but if you after redirects with redirect("yoursite"); codeigniter will show error , the other option is:
b) you need create in view a simple .php for example close_colorbox.php , inside write this code:
if (isset($script)) { echo $script; }
After in your controller include the next code:
$data['script'] = " <script type='text/javascript'> window.top.location.reload(); parent.$.fn.colorbox.close(); </script> ";
$this->load->vars($data);
$this->load->view('close_colorbox');
*note: window.top.location.reload(); is optional if you want reload the parent content

can I use joomla's onAfterRender for a module rather than a plugin?

I want to insert some code to Joomla when any page is loaded.
For this I created a module that inserts code.
I am trying to use
<?php
// $Id: helper.php
defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.environment.response' );
class modInsertCode
{
function onAfterRender($params)
{
$code = 'some code';
$documentbody = JResponse::getBody();
$documentbody = str_replace ("</body>", $code." </body>", $documentbody);
JResponse::setBody($documentbody);
return true;
}
}
?>
but JResponse::getBody(); returns an empty string. Any ideas, solutions of fixes to this code?
Thank you,
You have to do it using a plugin, you won't be able to do it using a module because the HTML response has not been generated by the time the code of the module gets executed.
I hope it helped!
I know this is a bit old but for future reference this can be done with jQuery:
$doc = JFactory::getDocument();
$js = 'jQuery(document).ready( function() {
jQuery("#module'.$module->id.'").appendTo(document.body);
})';
$doc->addScriptDeclaration($js);
This is assuming that you have wrapped the content in you module in something like the following, including the module id to support multiple instances of the module.
<div id="module<?php echo $module->id; ?>"> Your content </div>

Zend Framework: View variable in layout script is always null

I set a view variable in someAction function like this:
$this->view->type = "some type";
When I access this variable inside layout script like this:
<?php echo $this->type ?>
it prints nothing. What's wrong?
My application.ini settings related to layout
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout" ; changed 'default' to 'layout'
Edit
This thread suggests the alternate solution, but looking for solution to above problem. And this was working in Zend 1.6.2. I just upgraded to 1.10 and it stopped working.
Edit
If I set this view var inside any _init Bootstrap function, it works.
If you want to assign something to your layout you have to go an other way:
// get the layout instance
$layout = Zend_Layout::getMvcInstance();
// assign fooBar as Name to the layout
$layout->name = 'fooBar';
I believe the layout view object and the action view object are separate instances of the Zend_View class.
I think this is the correct way to pass variables from the controller to the layout:
/**
* Controller action
*/
public function indexAction()
{
$this->_helper->layout()->assign('myName', 'John Doe');
}
and then in your layout script you can access the variables by referencing the layout object like this:
<html>
<body>
<?php echo $this->layout()->myName; ?>
</body>
</html>
Do you have the following entry in your application.ini file?
resources.view[] =
So, you can initialize the view with no options and use it through:
<?php echo $this->type ?>

Resources