I am adding js form my block but it's not working - block

Here is my code , anyone can you please solve my problem
<?php
class Company_Module_Block_Custom extends Mage_Core_Block_Template {
public function _prepareLayout() {
$this->getLayout()->getBlock('head')->addJs('jquery/myjs.js');
return parent::_prepareLayout();
}
}

If the head block is already rendered then it has no effect. You have to make sure the js is added to the head after loadLayout() has been called in the controller action and before you call renderLayout() in the same action.
https://magento.stackexchange.com/questions/4984/add-javascript-file-to-head-for-create-block#answer-4992

Related

Phalcon\Mvc\View\Simple::render() in mailer causing WSOD

I have a simple controller action that creates a Guest record and renders a template.
// First bind the form to our Guest:
$form->bind( $_POST, $guest );
// Validate and save, or show error messages
if( $form->isValid($_POST, $guest) ) {
if( $guest->save($_POST) ) {
$this->view->setMainView( 'confirm' );
}
}
This works fine before I add any mailer stuff. However, when I add an event handler inside the Guest model which happens to render a template, the controller renders a WHITE SCREEN OF DEATH instead of my confirm template.
In Guest model:
public function afterCreate() {
return GuestMailer::sendEmailConfirmation( $this );
}
In GuestMailer class:
public static function sendEmailConfirmation( $guest ) {
// create/configure $email message
$view = $guest->getDI()->get('simpleView');
$view->render( // Works without this call...
'confirmation_email',
array( 'guest' => $guest )
);
$content = $view->getContent();
$email->content( $content );
return $email->send();
}
Note that when I remove the above call to render(), the confirm template is rendered successfully.
I thought components in Phalcon were supposed to be highly decoupled? Why is rendering a completely different template causing my controller's view to get messed up? How can I avoid this?
I think this problem is caused by a peculiar configuration of the templating service, in a normal workflow it doesn't causes issues, they appears when you need to render "manually" a template as in your case, you can refer to this PhalconPHP forum discussion linked, in particular the answer refered by the link anchor:
http://forum.phalconphp.com/discussion/109/manually-render-separate-file-template-#C12015

Yii Framework Captcha conflict with beforeAction() function

I have app in Yii and i extend all classes from some base controller and i have these code in it :
protected function beforeAction($action)
{
$this->setglobalvariable();
return parent::beforeAction($action);
}
as i just understand , these code prevent the Captcha to show , because when i delete it , the captcha shows up ! the captcha function is :
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
'minLength'=>2,
'maxLength'=>3,
'width'=>60,
),
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page'=>array(
'class'=>'CViewAction',
),
);
}
So how could i use beforeAction and captcha in same time ?
The confilict is in your structure , Show us more code . put the program in fresh yii and test it.
beforeAction function , Do not have any conflict with other Yii methods or functions.
The probelm is in your code.
Obviously there is some code in your Controller::setglobalvariables() method that conflicts with the captcha's code.
The CCaptachAction::run() method uses $_GET parameters. Are you somehow resetting $_GET ?
Can you show us the code ?

How to avoid rendering entire page when using CGridView via AJAX

When creating any ajax request in yii CGridView like (pagination, filtering, ...etc) the result of request will render whole page, how can i avoid that?
I tried to use renderPartial for view but it doesn't work. if this is the solution, how can i do it?
I just need to render the table of GridView not whole page.
Please advice.
In controller:
$this->layout = false;
For me works also $this->renderPartial() in controller instead of $this->render()
if(Yii::app()->request->isAjaxRequest()) $this->renderPartial('view');
else $this->render('view');
U can create class:
class Controller extends CController {
public function beforeAction($action) {
if(Yii::app()->request->isAjaxRequest) $this->layout = false;
return parent::beforeAction($action);
}
}

How to use the var_dump output directly from the controller?

class IndexController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$custom = "Custom variable";
var_dump($custom);
}
}
How to display the result not using the variables in the template?
P.S. The result of the Echo function is also suppressed. I understand that this is the wrong approach, but it is a quick way to debug the variables.
if you don't see output from controller check if in your template file you have this line:
{{ content() }}
you can use php's var_dump in any place of your code:
var_dump($var);exit;
exit; is to stop anything what happens after this line.
You can also dump your vars in volt's template with volt's function:
{{dump(var)}}
dump() is same as var_dump()
Here are some more useful volt functions:
http://docs.phalconphp.com/en/latest/reference/volt.html#functions
There is an implicit rendering level in the controller, in the first view that is rendered, you must call the getContent() method:
<div class="controller-output"><?php echo $this->getContent(); ?></div>
Or in Volt:
{{ content() }}
Ok, thx twistedxtra for the tip!
In my case, I use Twig.
To resolve my issue I've added a feature to Twig:
$function = new \Twig_SimpleFunction('content', function() use($view) {
return $view->getContent();
});
$this->_twig->addFunction($function);
Now it can be used in templates:
{{ content()|raw }}
Based on your above code , i understand that your required to execute the $custom value.
There are 2 ways as follow
1 - You can write var_dump($custom); and after that put die(); so that after it no code can be executed.
You can write echo $custom , for execute the value of $custom. But you have to stop script execution after it.
May be some times it happen that code as been written but due to template or view file execution it will overwrite your code. You must check the source code does anything printed above tag that you have written in controller.
May this will help you........:)
I know I'm a little late, but, just call
exit;
after your var_dump()
You can totally disable view in the action:
class IndexController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$custom = "Custom variable";
$this->view->disable();
var_dump($custom);
}
}
Or even use own debug method:
class IndexController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$custom = "Custom variable";
$this->debug($custom);
}
public function debug($data)
{
$this->view->disable();
var_dump($data);
}
}
Phalcon\Mvc\View\Engine\Twig() change to:
https://gist.github.com/4690638
and use:
{{condent()|raw}}
{{linkTo('#', 'title')|raw}}
This is my fork ;-)
An even simpler approach would be to set "$this->view->disable()" just above your var_dump expression when using volt. Maybe this would also work with other template engines.
Why not use good old
echo "<pre>" . print_r($custom,TRUE) . "</pre>";
Prints nice and ordered array. Have to add it works from both Controllers and Views. In case of Controller, output is placed on top of Controller's view.

How does one add a 'plain text node' to a zend form?

I'm trying to add a plain text node in a zend form - the purpose is to only dispay some static text.
The problem is - im not aware of any such way to do it.
I have used 'description' but that HAS to be attached to a form element.
Is there any way to simply display some text as part of a form? Zend considers everything as a form element so I cannot just print it out.
Eg:
The following will test your ability on so and so.
.
.
.
etc...
Any thoughts?
Zend has a form note view helper (Zend_View_Helper_FormNote), which you can use to add text.
Just create a new form element (/application/forms/Element/Note.php):
class Application_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
In your form:
$note = new Application_Form_Element_Note(
'test',
array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
Adding a hidden element with non-escaped description does the thing.
$form->addElement('hidden', 'plaintext', array(
'description' => 'Hello world! Check it out',
'ignore' => true,
'decorators' => array(
array('Description', array('escape'=>false, 'tag'=>'')),
),
));
Works perfectly. It is still attached to an element, which is, however, not rendered this way.
Code taken from: http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/
There might be a better way, but I created a paragraph by using a custom form element and view helper. Seems like alot of code for something so simple. Please let me know if you've found a more simplistic way to do it.
//From your form, add the MyParagraph element
$this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
class Zend_Form_Element_MyParagraph extends Zend_Form_Element
{
public $helper = 'myParagraph';
public function init()
{
$view = $this->getView();
}
}
class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
public function init() {
}
public function myParagraph() {
$html = '<p>hello world</p>';
return $html;
}
}
A little late but thought I'd throw it in anyway for the benefit of the community.
Aine has hit the nail on the head. FormNote is what you need if you want to use text in Zend_Form. However, you can use it without needing to extend Zend_Form_Element_Xhtml. See example below:
$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
->helper = 'formNote';
Note that you can use both text and html with the formNote helper.
This functionality is built into Zend via Zend_Form_Element_Note.
$note = new Zend_Form_Element_Note('forgot_password');
$note->setValue('Forgot Password?');
I faced the same problem and decided is better not to use Zend_Form at all, but to use directly view helpers (like Ruby on Rails does) and validate on the model.
This one-liner works for me:
$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));